天天看点

Jenkins结合Svn Maven Ant 实现自动部署

备忘用的, 以便之后可以快速配置

首先是要把 Maven 和 Ant 部署到 Jenkins 所在的服务器. 这里就不细说了, 唯一注意的就是在运行 Ant 时会用到 jdt 的 jar 文件, 需要提前放到 lib 中.

Jenkins 的安装和配置:

自动安装完成之后:

/usr/lib/jenkins/jenkins.war WAR 包

/etc/sysconfig/jenkins 配置文件 (修改端口等)

/var/lib/jenkins/ 默认的 JENKINS_HOME 目录

/var/log/jenkins/jenkins.log Jenkins 日志文件

安装完成之后启动 jenkins

此时就可以通过默认端口 (8080) 或者设置的端口访问 Jenkins 了, 在初始安装插件时直接选择推荐安装即可.

额外的插件安装:

  • Maven Integration plugin
  • Publish Over SSH

全局工具配置需要把 Jdk,Maven,Ant 的路径设置上, 在系统配置中的 [SSH Servers] 选项中可以对 ssh 进行配置 (可设置多个)

[构建后操作] 添加 [ Send build artifacts over SSH]:

  • Source files war 文件所在位置
  • Remove prefix 要截取的前缀
  • Remote directory 接受 war 文件的位置
  • Exec command 可以执行一些命令行操作

如果不是 maven 项目就需要用到 ant 来参与构建

Ant 的 build.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 定义一个工程,默认任务为 warFile -->
<project name="saashr" default="warFile" basedir=".">

    <!--接受命令行传入的参数-->
    <property name="env" value="${env}"/>
    <property name="database" value="${database}"/>
    <property name="tomcatHome" value="${tomcatHome}"/>

    <!-- 定义属性 -->
    <property name="warFileName" value="saashr.war"/>
    <property name="src" value="${basedir}/src"/>
    <property name="syssrc" value="${basedir}/syssrc"/>
    <property name="classpath" value="${basedir}/build/classes"/>
    <property name="tomcat.home" value="${tomcatHome}"/>
    <!-- 由于代码有泛型,必须使用 jdt编译,引入4个eclipse的jar即可 -->
    <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>

    <!-- 定义路径,编译 java 文件时用到的 jar 包。 -->
    <path id="project.lib">
        <!--servlet相关jar-->
        <fileset dir="${tomcat.home}/lib">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${basedir}/WebContent/WEB-INF/lib">
            <include name="**/*.jar"/>
        </fileset>
    </path>

    <!-- 定义任务,清空任务:清空原有的 class 文件,创建新的 build 路径。 -->
    <target name="clean">
        <delete dir="${classpath}"/>
        <mkdir dir="${classpath}"/>
    </target>

    <target name="copyBaseConfig" description="把基础配置文件copy到build路径中" depends="clean">
        <copy todir="${classpath}">
            <fileset dir="${basedir}/src">
                <include name="**/**.*"/>
                <exclude name="**/*.jar"/>
                <exclude name="**/*.class"/>
                <exclude name="a_little_config_ceshi*.txt"/>
                <exclude name="*config.properties"/>
            </fileset>
        </copy>
    </target>

    <target name="copyVariableConfig" description="把可变配置文件copy到build路径中" depends="copyBaseConfig">
        <copy todir="${classpath}">
            <fileset dir="${basedir}/src">
                <include name="a_little_config_ceshi_${database}.txt"/>
                <include name="${env}_config.properties"/>
            </fileset>
        </copy>
    </target>

    <target name="rename" description="修改可变配置文件的名称" depends="copyVariableConfig">
        <move file="${classpath}/a_little_config_ceshi_${database}.txt" tofile="${classpath}/a_little_config_ceshi.txt"/>
        <move file="${classpath}/${env}_config.properties" tofile="${classpath}/config.properties"/>
    </target>

    <target name="compile" description="编译文件" depends="rename">
        <javac destdir="${classpath}" encoding="utf-8" source="1.7" target="1.7" nowarn="true" includeantruntime="on">
            <compilerarg value="-Xlint:unchecked"/>
            <compilerarg value="-Xlint:deprecation"/>
            <src path="${src}"/>
            <src path="${syssrc}"/>
            <classpath refid="project.lib"/>
        </javac>
    </target>

    <!-- 定义默认任务,将 class 文件集合成 jar 包。 -->
    <target name="warFile" description="生成war文件" depends="compile">
        <!-- 删除原有 war 包。 -->
        <delete dir="${basedir}/${warFileName}"/>
        <!-- 建立新 war 包。 -->
        <war destfile="${basedir}/${warFileName}" webxml="${basedir}/WebContent/WEB-INF/web.xml">
            <!-- 将非 jar 和非 class 文件拷贝到 war 包的对应路径下。 -->
            <fileset dir="${basedir}/WebContent">
                <include name="**/**.*"/>
                <exclude name="**/*.jar"/>
                <exclude name="**/*.class"/>
            </fileset>
            <!-- 将 jar 和 class 文件拷贝到 war 包的对应路径下。 -->
            <lib dir="${basedir}/WebContent/WEB-INF/lib"/>
            <classes dir="${classpath}"/>
        </war>
    </target>
</project>
           

在[构建]中添加[Invoke Ant]项:

properties中加入想要传递的参数

env=pro

database=100

tomcatHome=/usrshare/tomcat