???????這兩天抽空,本想用jenkins實(shí)現(xiàn)公司產(chǎn)品的自動(dòng)話部署,無(wú)奈構(gòu)建過程中報(bào)路徑問題,項(xiàng)目目錄層級(jí)較復(fù)雜,排查無(wú)果。請(qǐng)教開發(fā)同事,結(jié)果被噴測(cè)試不用知道這些。額……好吧。本人才疏學(xué)淺,但這個(gè)部署問題不解決,甚感夜不能寐。我只能使用最簡(jiǎn)單的helloword來(lái)做實(shí)驗(yàn),所幸成功了。僅作為對(duì)jenkins懷有一腔熱血初入門的朋友一個(gè)參考。大牛請(qǐng)繞行,不喜勿噴。
???????說(shuō)明:本文使用jenkins進(jìn)行項(xiàng)目的自動(dòng)部署,tomcat在本地,遠(yuǎn)程部署后續(xù)再研究。
環(huán)境準(zhǔn)備
(1)安裝JDK,配置環(huán)境變量。jdk我用的是1.8x
(2)安裝ANT,配置環(huán)境變量。ant我用的是1.9x
(3)安裝Tomcat,我用的是7.0
???????這三者我都是下載的綠色免安裝的,所以直接配置即可。其中ant的版本最好是高于jdk的版本,我當(dāng)時(shí)使用的是ant1.8x,結(jié)果構(gòu)建時(shí)報(bào)錯(cuò)。換成1.9x后就好了。

jdk配置好后,通過命令 java -version 來(lái)查看是否成功,顯示如圖代表成功;

ant的檢查直接輸入 ant命令檢查是否成功,顯示如圖代表成功;

全局工具配置
在jenkins中點(diǎn)擊【系統(tǒng)管理】-【全局工具配置】,進(jìn)行jdk、ant的配置。

jdk配置如圖

ant配置如圖

新建任務(wù)
1、在jenkins中點(diǎn)擊【新建任務(wù)】,按圖填寫后點(diǎn)擊【確定】按鈕;

2、在【源碼管理】下面配置代碼庫(kù),用于拉取svn中的helloWorld項(xiàng)目,我使用的是svn,配置如圖所示;

3、設(shè)置構(gòu)建步驟,分為三步(停止tomcat服務(wù)、執(zhí)行build構(gòu)建、啟動(dòng)tomcat服務(wù)),設(shè)置完成保存;

執(zhí)行tomcat啟動(dòng)/停止命令前提是,需要先將tomcat作為windows的服務(wù)。
使用cmd命令進(jìn)入tomcat安裝路徑的bin目錄下,執(zhí)行service.bat install
Invoke ant 中選擇已經(jīng)在全局工具配置中設(shè)置好的ant,ant執(zhí)行過程中會(huì)自動(dòng)去找項(xiàng)目下默認(rèn)的build.xml文件。所以build.xml文件要提前寫好,放到項(xiàng)目的根目錄下

bulild.xml文件內(nèi)容
<project name="helloWorld" default="deploy" basedir=".">
<property environment="env" />
<property name="webapp.name" value="helloWorld" />
<property name="catalina.home" value="G:\apache-tomcat-7.0.59-64" />
<property name="dist.dir" value="${basedir}/dist" /><!--ant打完war包放置的位置 -->
<property name="ant.dir" value="G:\workSoft\apache-ant-1.9.7" />
<property name="webRoot.dir" value="${basedir}/WebContent" />
<property name="src.dir" value="${basedir}/src" />
<!-- <property name="config.dir" value="${basedir}/config" /> -->
<property name="lib.dir" value="${webRoot.dir}/WEB-INF/lib" />
<!-- 如果使用eclipse+ant打包時(shí),則打包后的war包可以放到項(xiàng)目的這個(gè)目錄-->
<property name="build.dir" value="${basedir}/war" />
<!-- 使用eclipse jdt進(jìn)行編譯,而不使用JDK編譯 -->
<!-- <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter" /> -->
<!-- 初始化classpath -->
<path id="project.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
<!-- 添加tomcat類路徑 -->
<fileset dir="${catalina.home}/lib">
<include name="*.jar" />
</fileset>
<!-- ant lib包 -->
<fileset dir="${ant.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<!-- get the source compile classpath in a printable form -->
<pathconvert pathsep="${line.separator}| |-- "
property="echo.path.compile"
refid="project.classpath">
</pathconvert>
<!-- show classpath jars -->
<target name="print_classpath">
<echo message="|-- compile classpath"/>
<echo message="| |"/>
<echo message="| |-- ${echo.path.compile}"/>
</target>
<!-- 刪除之前的目錄結(jié)構(gòu) -->
<target name="clear" description="清理舊文件">
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" />
<delete file="${catalina.home}/webapps/${webapp.name}.war" />
<delete dir="${catalina.home}/webapps/${webapp.name}" />
</target>
<!-- 創(chuàng)建目錄結(jié)構(gòu) -->
<target name="init" depends="clear" description="創(chuàng)建初始化目錄結(jié)構(gòu)">
<mkdir dir="${build.dir}/classes" />
<mkdir dir="${dist.dir}" />
</target>
<!-- 編譯java -->
<target name="compile" depends="init" description="編譯java文件">
<echo message="begin compile..." />
<javac srcdir="${src.dir}" destdir="${build.dir}/classes"
includeantruntime="false" nowarn="on"
source="1.6" target="1.6" deprecation="true" debug="true"
encoding="UTF-8" classpathref="project.classpath">
<compilerarg line="-Xlint:unchecked" />
<!-- <classpath refid="project.classpath" /> -->
</javac>
<copy todir="${build.dir}">
<fileset dir="${src.dir}">
<include name="**/*.xml" />
<include name="**/*.properties" />
<include name="**/*.sql" />
</fileset>
<!-- <fileset dir="${config.dir}">
<include name="**/*.xml" />
<include name="**/*.properties" />
<include name="**/*.sql" />
</fileset> -->
</copy>
<echo message="end compile..." />
</target>
<!-- 將class文件打成 jar包 -->
<!--
<target name="pack" depends="compile">
<jar jarfile="${build.dir}/${webapp.name}.jar">
<fileset dir="${build.dir}/classes">
<include name="**/*.class"/>
</fileset>
</jar>
</target>
-->
<!-- 打成war包, 名稱默認(rèn)為 項(xiàng)目名 -->
<target name="war" depends="compile" description="將工程打成war包">
<echo message="begin war..." />
<war destfile="${dist.dir}/${webapp.name}.war" basedir="${webRoot.dir}"
webxml="${webRoot.dir}/WEB-INF/web.xml">
<lib dir="${lib.dir}" />
<classes dir="${build.dir}/classes" />
<fileset dir="${webRoot.dir}">
<include name="***.*" />
</fileset>
</war>
<echo message="end war..." />
</target>
<!-- copy war包 tomcat的deploy目錄 -->
<target name="deploy" depends="war" description="部署項(xiàng)目">
<echo message="begin deploy..." />
<copy file="${dist.dir}/${webapp.name}.war" todir="${catalina.home}/webapps" />
<!--從dist.dir中拷貝war包到tomcat的webapps下 -->
<echo message="end deploy..." />
</target>
</project>
4、點(diǎn)擊【立即構(gòu)建】按鈕開始構(gòu)建,構(gòu)建完成后,控制臺(tái)打印信息如圖所示。

然后在瀏覽器中輸入:http://localhost:8080/helloWorld/,頁(yè)面顯示正確。
