Jenkins+Ant+Java+Junit+SVN使用總結__tomcat

來源:互聯網
上載者:User

一、環境準備 Jenkins: 到官網下載jenkins.war包:http://jenkins-ci.org/ 安裝方法有兩種: 把下載下來的jenkins.war包放到檔案夾下,如C:\jenkins,然後開啟命令列視窗並進到該目錄下,執行java -jar jenkens.war命令,當提示:“Jenkins is fully up and running”時,表示啟動成功,這時在瀏覽器視窗輸入:http://localhost:8080/ 就可到jenkins的首頁。 如果有tomcat,把jenkins.war包放在tomcat的webapps檔案夾下,啟動tomcat時會自動啟動jenkins,這時通過http://localhost:8080/jenkins就 可以訪問jenkins的首頁了。 ANT:

      下載ant並配置ANT_HOME,官網:http://ant.apache.org/。

  3、Junit:

      下載junit.jar包,沒用過的可參考:http://blog.csdn.net/lengyuhong/article/details/5815017

  4、SVN:

      1、用本地硬碟當SVN倉庫:http://wenku.baidu.com/view/12b02f6a011ca300a6c39081.html

      2、SVN伺服器搭建和使用:http://www.cnblogs.com/xiaobaihome/tag/SVN/ (推薦用此種方法,後面有原因)

二、項目代碼:

  環境準備好了之後就可開始寫代碼、單元測試案例以及ANT用來構建的build.xml檔案,這些內容在上一篇ANT task之Junit、JunitReport有講過,這裡不細講:

1、Java代碼:

package com.glen.he;public class ComplexCalculation {    public int Division(int a,int b){                return (a/b);            }    public int Multiply(int a,int b){                return (a*b);            }}

package com.glen.he;public class SimpleCalculation {    public int Add(int a,int b){                return (a+b);            }    public int Subtration(int a,int b){        return(a-b);    }    }

2、單元測試代碼:  

package com.glen.he;import com.glen.he.ComplexCalculation;import static org.junit.Assert.*;import org.junit.Test;public class ComplexCalculationTest {        ComplexCalculation cc = new ComplexCalculation();        @Test    public void DivisionTest() {                int c = cc.Division(100, 5);                assertEquals(20, c);            }    @Test    public void MultiplyTest() {                int c = cc.Multiply(100, 5);                assertEquals(500, c);            }}

package com.glen.he;import com.glen.he.SimpleCalculation;import static org.junit.Assert.*;import org.junit.Test;public class SimpleCalculationTest {    SimpleCalculation sc = new SimpleCalculation();        @Test    public void AddTest() {                int c = sc.Add(3, 5);                    assertEquals(8, c);            }        @Test    public void SubtrationTest() {                int c = sc.Subtration(20, 5);                    assertEquals(15, c);            }}

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、點擊儲存,然後選擇立即構建,執行結果:  

 

 

 

 

參考資料:

http://hi.baidu.com/janice515/item/3272fe9b99eb4cc8b6253101 

http://blog.csdn.net/lengyuhong/article/details/5828770

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.