前面基本上把整个配置过程都完整地串起来了,包括可能遇到的难点,按照那个套路应该可以配置好自动打包发布的功能。简单总结下我的学习过程,以及遇到问题是怎样解决的。
准备一个项目源码
刚开始在github和码云上搜索有没有现成的练习项目,很可惜,没有找到,所以只能自己创建一个简单的项目,当然,如果在公司有源码浏览权限的话可以直接使用公司git服务器上的项目来练习,可惜我们公司并没有给测试人员开放源码权限。
我特意下载了一个Intellij IDEA来新建java工程(呵呵,杀鸡用牛刀~~),Ant的build.xml文件也是用Intellij IDEA来生成的,只需做一些小改动就能用。
1.git知识基础
因为没有公司源码权限,所以需要借助github或者码云来管理这个HelloWorld项目,用码云托管项目倒没有花费我太多时间,因为刚好前一段时间学习了git的基本操作,很顺利地就把源码push到了码云上面。
学会使用git的话,也可以在后面验证jenkins是否真的自动部署了,可以在本地git仓库把程序代码改动一下,然后push到远程仓库,在jenkins构建一下,再访问HelloWorld看看是否发生变化
可以看到新构建的任务changes那里显示便是git commit提交时加的注释(附:廖雪峰git教程地址)
2.Ant的build.xml文件
这里也花了很多时间,其实到现在也还没完全了解,感兴趣的小伙伴可以看看:ant教程(不过据说现在用ant打包的比较少了,都在用maven了,jenkins也可以构建maven项目的,不过我还没研究)
利用Ant打包时,注意指定target,target表示是一个个的待执行的任务,例如我在本地打包时
build.xml文件一般放在java项目的工程目录的最外层(当然文件中具体定义的相对路径有关)
jenkins做的其实就是调用Ant,然后利用build.xml文件实现打包功能
我用的build.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<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 'helloworld' 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 'HelloWorld:war' 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 'HelloWorld:war exploded' 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>