Jenkins+Ant+Java+Junit+SVN

1. Environmental preparation

  1. Jenkins:
    1. Download the jenkins.war package from the official website: http://jenkins-ci.org/
    2. There are two installation methods:
      1. Put the downloaded jenkins.war package in a folder, such as C:\jenkins, then open the command line window and enter the directory, execute the java -jar jenkens.war command, when prompted: "Jenkins is fully up and running ", it means the startup is successful, then enter: http://localhost:8080/ in the browser window  to go to the home page of jenkins.
      2. If you have tomcat, put the jenkins.war package in the webapps folder of tomcat. When you start tomcat, jenkins will be automatically started. At this time, you can access the home page of jenkins through http://localhost:8080/jenkins .
  2. ANT:

      Download ant and configure ANT_HOME, official website: http://ant.apache.org/ .

  3、Junit:

      Download the junit.jar package, you can refer to the unused ones: http://blog.csdn.net/lengyuhong/article/details/5815017

  4、SVN:

      1. Use the local hard disk as the SVN warehouse: http://wenku.baidu.com/view/12b02f6a011ca300a6c39081.html

      2. SVN server construction and use: http://www.cnblogs.com/xiaobaihome/tag/SVN/  ( this method is recommended, there are reasons behind )

Second, the project code:

  After the environment is ready, you can start writing code, unit test cases, and the build.xml file that ANT uses to build. These contents have been covered in the previous ANT task Junit and JunitReport , and I will not go into details here:

1. Java code:

  ComplexCalculation.java
  SimpleCalculation.java

2. Unit test code:  

  ComplexCalculationTest.java
  SimpleCalculationTest.java

3、build.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project name="AntDemo" default="junit" basedir=".">
    <!-- =================================================================== -->
    <!-- 变量设置  -->
    <!-- =================================================================== -->

    <!-- 源代码src路径 -->
    <property name="src.path" value="src/java"/>
    <!-- 单元测试代码路径 -->
    <property name="test.path" value="src/test"/>
    <!-- 编译文件class路径 -->
    <property name="build.path" value="build"/>
    <!-- jar包路径 -->
    <property name="dist.path" value="dist"/>
    <!-- lib包路径 -->
    <property name="lib.path" value="lib"/>
    <!-- 生成报告junit4.xml路径 -->
    <property name="report.path" value="report"/>
        
    <!-- =================================================================== -->
    <!-- 设置classpath -->
    <!-- =================================================================== -->
    <path id="compile.path">        
        <fileset dir="${lib.path}">
            <include name="**/*.jar"/>
        </fileset>
        
        <pathelement path="${build.path}"/>
    </path>     

    <!-- 初始化 -->
    <target name="init">        
        <mkdir dir="${build.path}"/>
        <mkdir dir="${report.path}"/>
        <mkdir dir="${dist.path}"/>
    </target>
    
    <!-- =================================================================== -->
    <!-- 清除历史编译class -->
    <!-- =================================================================== -->
    <target name="clean" description="clean">        
        <delete dir="${build.path}"/>
        <delete dir="${report.path}"/>
        <delete dir="${dist.path}"/>
    </target>

    <!-- =================================================================== -->
    <!-- 编译测试文件,初始化目录 -->
    <!-- =================================================================== -->
    <target name="compile" depends="init">
        <javac srcdir="${src.path}" destdir="${build.path}"  classpathref="compile.path" includeantruntime="true"/>
        <javac srcdir="${test.path}" destdir="${build.path}"  classpathref="compile.path" includeantruntime="true"/>
    </target>      
         
    <!-- =================================================================== -->
    <!-- 执行测试案例 -->
    <!-- =================================================================== -->
    <target name="junit" depends="compile">                
        <junit printsummary="true" fork="true">        
             <formatter type="xml" usefile="true"/>        
            
             <classpath refid="compile.path"/>        
            
            <batchtest fork="on" todir="${report.path}" haltonfailure="no">
                <fileset dir="${build.path}">
                    <include name="**/*Test.class"/>
                </fileset>
            </batchtest>                 
         </junit>        
     </target>
    
    <target name="junit-report" depends="junit">        
        <!-- 产生单元测试报表文档 -->
        <junitreport todir="${report.path}">
            <fileset dir="${report.path}">
                <include name="TEST-*.xml" />
            </fileset>
            
            <report format="frames" todir="${report.path}" />
        </junitreport>
    </target>

    <target name="make-jar" depends="compile" description="make jar file">
          <jar jarfile="${dist.path}/AntDemo.jar">
               <fileset dir="${build.path}">

                <!--除去test文件-->
                <exclude name="**/*Test.class"/>
               </fileset>
          </jar>
     </target>    
    
</project>
复制代码

 

三、配置Jenkins:

  PS:Jenkins可以通过master/slave来支持分布式的job运行,本文运行在master,即Jenkins所在的机器。

  1、打开jenkins首页,新建一个job,输入Item名称,选择 构建一个自由风格的软件项目,点击"OK"  

 

  2、在 源码管理 那里,选择Subversion,在Repository URL后面,输入你的SVN地址。  

      PS:Repository URL使用本地磁盘当仓库这种方法后来我在其它机器上试验时,发现老是报错:svn: E180001: Unable to open an ra_local session to URL。一时没有找到解决办法,大家如果也碰到此问题,可以搭建SVN服务器来管理源代码,我试了,挺好使的。

 

  3、在 构建 那里也可以有两种做法:

   I、选择Execute Windows batch command,在输入框输入如下命令(这里我选择的是这个方法):

      set path=C:\ANT_HOME\Apache-Ant-1.7.0\bin;path  把ant的安装目录添加到path

      ant junit                        执行junit task      

   II、方法I比较麻烦,如果我们设置好了ANT_HOME,可以选择Invoke Ant,然后在targets里面指定我们build.xml里的task name。       

  4、点击保存,然后选择立即构建,执行结果:  

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326645644&siteId=291194637