jenkins之從0到1利用Git和Ant插件打war包並自動部署到tomcat(第五話):總結以及build.xml文件

前面基本上把整個配置過程都完整地串起來了,包括可能遇到的難點,按照那個套路應該能夠配置好自動打包發佈的功能。簡單總結下個人學習過程,以及遇到問題是怎樣解決的。html

準備一個項目源碼java

剛開始在github和碼雲上搜索有沒有現成的練習項目,很惋惜,沒有找到,因此只能本身建立一個簡單的項目,固然,若是在公司有源碼瀏覽權限的話能夠直接使用公司git服務器上的項目來練習,惋惜咱們公司並無給測試人員開放源碼權限。 我特地下載了一個Intellij IDEA來新建java工程(呵呵,殺雞用牛刀~~),Ant的build.xml文件也是用Intellij IDEA來生成的,只需作一些小改動就能用。git

1.git知識基礎

由於沒有公司源碼權限,因此須要藉助github或者碼雲來管理這個HelloWorld項目,用碼雲託管項目倒沒有花費我太多時間,由於恰好前一段時間學習了git的基本操做,很順利地就把源碼push到了碼雲上面。 學會使用git的話,也能夠在後面驗證jenkins是否真的自動部署了,能夠在本地git倉庫把程序代碼改動一下,而後push到遠程倉庫,在jenkins構建一下,再訪問HelloWorld看看是否發生變化 github

能夠看到新構建的任務changes那裏顯示即是git commit提交時加的註釋(附:廖雪峯git教程地址web

2.Ant的build.xml文件

這裏也花了不少時間,其實到如今也還沒徹底瞭解,感興趣的小夥伴能夠看看:ant教程(不過聽說如今用ant打包的比較少了,都在用maven了,jenkins也能夠構建maven項目的,不過我還沒研究) 利用Ant打包時,注意指定target,target表示是一個個的待執行的任務,例如我在本地打包時api

build.xml文件通常放在java項目的工程目錄的最外層(固然文件中具體定義的相對路徑有關)服務器

jenkins作的其實就是調用Ant,而後利用build.xml文件實現打包功能less

我用的build.xml內容以下: <?xml version="1.0" encoding="UTF-8"?> <project name="HelloWorld" default="all">dom

<property file="build.properties"/>
  <!-- Uncomment the following property if no tests compilation is needed -->
  <!-- 
  <property name="skip.tests" value="true"/>
   -->
  
  <!-- Compiler options -->
  
  <property name="compiler.debug" value="on"/>
  <property name="compiler.generate.no.warnings" value="off"/>
  <property name="compiler.args" value=""/>
  <property name="compiler.max.memory" value="700m"/>
  <patternset id="ignored.files">
    <exclude name="**/*.hprof/**"/>
    <exclude name="**/*.pyc/**"/>
    <exclude name="**/*.pyo/**"/>
    <exclude name="**/*.rbc/**"/>
    <exclude name="**/*.yarb/**"/>
    <exclude name="**/*~/**"/>
    <exclude name="**/.DS_Store/**"/>
    <exclude name="**/.git/**"/>
    <exclude name="**/.hg/**"/>
    <exclude name="**/.svn/**"/>
    <exclude name="**/CVS/**"/>
    <exclude name="**/__pycache__/**"/>
    <exclude name="**/_svn/**"/>
    <exclude name="**/vssver.scc/**"/>
    <exclude name="**/vssver2.scc/**"/>
  </patternset>
  <patternset id="library.patterns">
    <include name="*.egg"/>
    <include name="*.jar"/>
    <include name="*.ear"/>
    <include name="*.swc"/>
    <include name="*.war"/>
    <include name="*.zip"/>
    <include name="*.ane"/>
  </patternset>
  <patternset id="compiler.resources">
    <exclude name="**/?*.java"/>
    <exclude name="**/?*.form"/>
    <exclude name="**/?*.class"/>
    <exclude name="**/?*.groovy"/>
    <exclude name="**/?*.scala"/>
    <exclude name="**/?*.flex"/>
    <exclude name="**/?*.kt"/>
    <exclude name="**/?*.clj"/>
    <exclude name="**/?*.aj"/>
  </patternset>
  
  <!-- JDK definitions -->
  
  <property name="jdk.bin.1.7" value="${jdk.home.1.7}/bin"/>
  <path id="jdk.classpath.1.7">
    <fileset dir="${jdk.home.1.7}">
      <include name="jre/lib/charsets.jar"/>
      <include name="jre/lib/deploy.jar"/>
      <include name="jre/lib/ext/access-bridge-64.jar"/>
      <include name="jre/lib/ext/dnsns.jar"/>
      <include name="jre/lib/ext/jaccess.jar"/>
      <include name="jre/lib/ext/localedata.jar"/>
      <include name="jre/lib/ext/sunec.jar"/>
      <include name="jre/lib/ext/sunjce_provider.jar"/>
      <include name="jre/lib/ext/sunmscapi.jar"/>
      <include name="jre/lib/ext/zipfs.jar"/>
      <include name="jre/lib/javaws.jar"/>
      <include name="jre/lib/jce.jar"/>
      <include name="jre/lib/jfr.jar"/>
      <include name="jre/lib/jfxrt.jar"/>
      <include name="jre/lib/jsse.jar"/>
      <include name="jre/lib/management-agent.jar"/>
      <include name="jre/lib/plugin.jar"/>
      <include name="jre/lib/resources.jar"/>
      <include name="jre/lib/rt.jar"/>
    </fileset>
  </path>
  
  <property name="project.jdk.home" value="${jdk.home.1.7}"/>
  <property name="project.jdk.bin" value="${jdk.bin.1.7}"/>
  <property name="project.jdk.classpath" value="jdk.classpath.1.7"/>
  
  
  <!-- Application Server Libraries -->
  <!-- Register Custom Compiler Taskdefs -->
  <property name="javac2.home" value="${idea.home}/lib"/>
  <path id="javac2.classpath">
    <fileset dir="${javac2.home}">
      <include name="javac2.jar"/>
      <include name="jdom.jar"/>
      <include name="asm-all*.jar"/>
      <include name="jgoodies-forms.jar"/>
    </fileset>
  </path>
  <target name="register.custom.compilers">
    <taskdef name="javac2" classname="com.intellij.ant.Javac2" classpathref="javac2.classpath"/>
    <taskdef name="instrumentIdeaExtensions" classname="com.intellij.ant.InstrumentIdeaExtensions" classpathref="javac2.classpath"/>
  </target>
  
  <!-- Modules -->
  
  
  <!-- Module HelloWorld -->
  
  <dirname property="module.HelloWorld.basedir" file="${ant.file}"/>
  
  
  <property name="module.jdk.home.HelloWorld" value="${project.jdk.home}"/>
  <property name="module.jdk.bin.HelloWorld" value="${project.jdk.bin}"/>
  <property name="module.jdk.classpath.HelloWorld" value="${project.jdk.classpath}"/>
  
  <property name="compiler.args.HelloWorld" value="-encoding UTF-8 -source 7 -target 7 ${compiler.args}"/>
  
  <property name="HelloWorld.output.dir" value="${module.HelloWorld.basedir}/web/WEB-INF/classes"/>
  <property name="HelloWorld.testoutput.dir" value="${module.HelloWorld.basedir}/web/WEB-INF/classes"/>
  
  <path id="HelloWorld.module.bootclasspath">
    <!-- Paths to be included in compilation bootclasspath -->
  </path>
  
  <path id="HelloWorld.module.production.classpath">
    <path refid="${module.jdk.classpath.HelloWorld}"/>
    <fileset dir="${basedir}/web/WEB-INF/lib">
      <patternset refid="library.patterns"/>
    </fileset>
  </path>
  
  <path id="HelloWorld.runtime.production.module.classpath">
    <pathelement location="${HelloWorld.output.dir}"/>
    <fileset dir="${basedir}/web/WEB-INF/lib">
      <patternset refid="library.patterns"/>
    </fileset>
  </path>
  
  <path id="HelloWorld.module.classpath">
    <path refid="${module.jdk.classpath.HelloWorld}"/>
    <pathelement location="${HelloWorld.output.dir}"/>
    <fileset dir="${basedir}/web/WEB-INF/lib">
      <patternset refid="library.patterns"/>
    </fileset>
  </path>
  
  <path id="HelloWorld.runtime.module.classpath">
    <pathelement location="${HelloWorld.output.dir}"/>
    <fileset dir="${basedir}/web/WEB-INF/lib">
      <patternset refid="library.patterns"/>
    </fileset>
  </path>
  
  
  <patternset id="excluded.from.module.HelloWorld">
    <patternset refid="ignored.files"/>
  </patternset>
  
  <patternset id="excluded.from.compilation.HelloWorld">
    <patternset refid="excluded.from.module.HelloWorld"/>
  </patternset>
  
  <path id="HelloWorld.module.sourcepath">
    <dirset dir="${module.HelloWorld.basedir}">
      <include name="src"/>
    </dirset>
  </path>
  
  
  <target name="compile.module.HelloWorld" depends="compile.module.HelloWorld.production,compile.module.HelloWorld.tests" description="Compile module HelloWorld"/>
  
  <target name="compile.module.HelloWorld.production" depends="register.custom.compilers" description="Compile module HelloWorld; production classes">
    <mkdir dir="${HelloWorld.output.dir}"/>
    <javac2 destdir="${HelloWorld.output.dir}" debug="${compiler.debug}" nowarn="${compiler.generate.no.warnings}" memorymaximumsize="${compiler.max.memory}" fork="true" executable="${module.jdk.bin.HelloWorld}/javac">
      <compilerarg line="${compiler.args.HelloWorld}"/>
      <bootclasspath refid="HelloWorld.module.bootclasspath"/>
      <classpath refid="HelloWorld.module.production.classpath"/>
      <src refid="HelloWorld.module.sourcepath"/>
      <patternset refid="excluded.from.compilation.HelloWorld"/>
    </javac2>
    
    <copy todir="${HelloWorld.output.dir}">
      <fileset dir="${module.HelloWorld.basedir}/src">
        <patternset refid="compiler.resources"/>
        <type type="file"/>
      </fileset>
    </copy>
  </target>
  
  <target name="compile.module.HelloWorld.tests" depends="register.custom.compilers,compile.module.HelloWorld.production" description="compile module HelloWorld; test classes" unless="skip.tests"/>
  
  <target name="clean.module.HelloWorld" description="cleanup module">
    <delete dir="${HelloWorld.output.dir}"/>
    <delete dir="${HelloWorld.testoutput.dir}"/>
  </target>
  
  <target name="init" description="Build initialization">
    <!-- Perform any build initialization in this target -->
  </target>
  
  <target name="clean" depends="clean.module.HelloWorld, clean.artifact.HelloWorld:war_exploded" description="cleanup all"/>
  
  <target name="build.modules" depends="init, clean, compile.module.HelloWorld" description="build all modules"/>
  
  <target name="init.artifacts">
    <property name="artifacts.temp.dir" value="${basedir}/output"/>
    <property name="artifact.output.helloworld" value="${basedir}/out/artifacts/helloworld"/>
    <property name="artifact.output.HelloWorld:war" value="${basedir}/out/artifacts/HelloWorld_war"/>
    <property name="artifact.output.HelloWorld:war_exploded" value="${basedir}/out/artifacts/HelloWorld_war_exploded"/>
    <mkdir dir="${artifacts.temp.dir}"/>
    <property name="temp.jar.path.helloworld.war" value="${artifacts.temp.dir}/helloworld.war"/>
    <property name="temp.jar.path.HelloWorld_war.war" value="${artifacts.temp.dir}/HelloWorld_war.war"/>
  </target>
  
  <target name="clean.artifact.HelloWorld:war_exploded" description="clean HelloWorld:war exploded artifact output">
    <delete dir="${artifact.output.HelloWorld:war_exploded}"/>
  </target>
  
  <target name="artifact.helloworld" depends="init.artifacts" description="Build &#39;helloworld&#39; artifact">
    <property name="artifact.temp.output.helloworld" value="${artifacts.temp.dir}/helloworld"/>
    <!--mkdir dir="${artifact.temp.output.helloworld}"/-->
    <war destfile="${temp.jar.path.helloworld.war}">
      <zipfileset dir="${basedir}/web"/>
    </war>
    <!--copy file="${temp.jar.path.helloworld.war}" tofile="${artifact.temp.output.helloworld}/helloworld.war"/-->
  </target>
  
  <!--target name="artifact.HelloWorld:war" depends="init.artifacts, artifact.HelloWorld:war_exploded" description="Build &#39;HelloWorld:war&#39; artifact">
    <property name="artifact.temp.output.HelloWorld:war" value="${artifacts.temp.dir}/HelloWorld_war"/>
    <mkdir dir="${artifact.temp.output.HelloWorld:war}"/>
    <war destfile="${temp.jar.path.HelloWorld_war.war}">
      <zipfileset dir="${artifact.output.HelloWorld:war_exploded}"/>
    </war>
    <copy file="${temp.jar.path.HelloWorld_war.war}" tofile="${artifact.temp.output.HelloWorld:war}/HelloWorld_war.war"/>
  </target-->
  
  <!--target name="artifact.HelloWorld:war_exploded" depends="init.artifacts, compile.module.HelloWorld" description="Build &#39;HelloWorld:war exploded&#39; artifact">
    <mkdir dir="${artifact.output.HelloWorld:war_exploded}"/>
    <copy todir="${artifact.output.HelloWorld:war_exploded}">
      <fileset dir="${basedir}/web"/>
    </copy>
    <mkdir dir="${artifact.output.HelloWorld:war_exploded}/WEB-INF"/>
    <copy file="${basedir}/web/WEB-INF/web.xml" tofile="${artifact.output.HelloWorld:war_exploded}/WEB-INF/web.xml"/>
    <mkdir dir="${artifact.output.HelloWorld:war_exploded}/WEB-INF"/>
    <mkdir dir="${artifact.output.HelloWorld:war_exploded}/WEB-INF/classes"/>
    <copy todir="${artifact.output.HelloWorld:war_exploded}/WEB-INF/classes">
      <fileset dir="${HelloWorld.output.dir}"/>
    </copy>
  </target-->
  
  <target name="build.all.artifacts" depends="artifact.helloworld" description="Build all artifacts">
    <mkdir dir="${artifact.output.helloworld}"/>
    <copy todir="${artifact.output.helloworld}">
      <fileset dir="${artifact.temp.output.helloworld}"/>
    </copy>
    <mkdir dir="${artifact.output.HelloWorld:war}"/>
    <copy todir="${artifact.output.HelloWorld:war}">
      <fileset dir="${artifact.temp.output.HelloWorld:war}"/>
    </copy>
    
    <!-- Delete temporary files -->
    <delete dir="${artifacts.temp.dir}"/>
  </target>
  
  <target name="all" depends="build.modules, build.all.artifacts" description="build all"/>
</project>
相關文章
相關標籤/搜索