使用jenkins并发布应用到tomcat

jenkins的介绍及安装请自行百度,本文重点介绍如何使用jenkins,并自动发布web应用到tomcat中.

1 . 创建项目

打开jenkins --> 新建 --> 填写item名称,就是项目名称,这里我选择构建一个自由风格的软件项目 -->保存

2. 配置构建步骤

 在配置页 --> ① 源码管理 我用的svn,就选subversion了,填入项目的url , 首次使用jenkins需要配置svn账号密码,配置方法根据下方的提示。

   ② 构建触发器 ,我这里设置每小时构建一次

  ③ 配置构建步骤 , 这里使用ant进行构建,选择ant 版本-图中选择的是ant1.9.5 , 填写tagets 这里的main 即对应build.xml的main target ,这样就会使用项目里的              build.xml就行构建了。构建完了,需要将war包发布到tomcat 里, 再增加一个shell 步骤,ok.

  

  

点击 立即构建 ,就可以看到构建成功了,打开网页也可以看到项目了。

jenkins 使用比较简单,关键在于配置ant build.xml ,之前没有接触过。web应用编译需要servlet-api.jar ,考一个tomcat下的放到项目的lib下就可以编译通过了。

附上build.xml 比较粗糙,(⊙﹏⊙)b:

<?xml version="1.0" encoding="utf-8"?>
<!-- WARNING: Eclipse auto-generated file.
              Any modifications will be overwritten.
              To include a user specific buildfile here, simply create one in the same
              directory with the processing instruction <?eclipse.ant.import?>
              as the first entry and export the buildfile again. -->
<project basedir="." default="main" name="XX">
    <target name="main" depends="complie, compress" description="Main target">
       <echo>Building war file.</echo>
    </target>
    
    <property environment="env"/>
   
    <property name="debuglevel" value="source,lines,vars"/>
    
    <property name="target" value="1.7"/>
    <property name="source" value="1.7"/>
    
    <target name="init">
    <property name="build" value="build"></property>
    <property name="src" value="src"></property>
        <delete dir="${build}" />
        <mkdir dir="${build}"/>
        <mkdir dir="${build}\WEB-INF"/>
        <mkdir dir="${build}\WEB-INF\classes"/>
        <copy todir="${build}">
            <fileset dir="${basedir}\WebContent">
                <include name="WEB-INF/**" />
                <include name="META-INF/**" />
                <include name="api/**" />
                <include name="manage/**" />
            </fileset>
        </copy>
    </target>
    
    <!--定义项目编译的时候,以来的lib包的路径-->  
   <path id="project.class.path">  
       <!-- <pathelement path="${classpath}" /> -->  
       <fileset dir="${basedir}/WebContent/WEB-INF/lib">  
           <include name="**/*.jar" />  
       </fileset>  
   </path>
    
    <target name="complie" depends="init">
        <javac srcdir="${basedir}/src" destdir="${build}/WEB-INF/classes">
             <classpath refid="project.class.path" />  
        </javac>
        <copy todir="${build}/WEB-INF/classes">
            <fileset dir="${basedir}/src">
                <include name="**/**.xml" />
            </fileset>
        </copy>
    </target>
    
    <target name="compress" depends="complie">
        <war warfile="${build}/XX.war" webxml="${build}/WEB-INF/web.xml">
            <lib dir="${build}/WEB-INF/lib"/>
            <classes dir="${build}/WEB-INF/classes"/>
            <fileset dir="${build}"/>
        </war>
    </target>
     
</project>
原文地址:https://www.cnblogs.com/fzqm/p/4586057.html