一、jenkins配置
在jenkins里面配置好ant版本
构建完成后执行shell脚本重启tomcat
二、build.xml配置
- 从官网http://ant.apache.org/bindownload.cgi获取最新的tar包。
- 上传至服务器目录,解压tar包即可
tar –xvf apache-ant-1.9.2-bin.tar
注:使用javacc编译存在一些条件无法编译的情况,可以使用eclipse的编译器jdt进行编译。找到
jdtCompilerAdapter.jar
org.eclipse.jdt.compiler.tool_1.0.100.v_B79_R37x.jar
org.eclipse.jdt.core_3.7.3.v20120119-1537.jar
org.eclipse.jdt.debug.ui_3.6.1.v20111006_r372.jar
上传至ant目录的lib下。在build.xml申明下使用jdt即可
<property name="build.compiler"value="org.eclipse.jdt.core.JDTCompilerAdapter">
<?xml version="1.0" encoding="UTF-8"?>
<project name="testApp" default="deploy" basedir=".">
<property environment="env" />
<property name="webapp.name" value="testApp" />
<property name="catalina.home" value="/home/testApp/tomcat8087" />
<property name="dist.dir" value="${basedir}/dist" />
<property name="ant.dir" value="/usr/local/apache-ant-1.9.9" />
<property name="webRoot.dir" value="${basedir}/WebRoot" />
<property name="src.dir" value="${basedir}/src" />
<property name="config.dir" value="${basedir}/conf" />
<property name="lib.dir" value="${webRoot.dir}/WEB-INF/lib" />
<property name="build.dir" value="${basedir}/build" />
<!-- 使用eclipse jdt进行编译,而不使用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>
<!-- 删除之前的目录结构 -->
<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>
<!-- 创建目录结构 -->
<target name="init" depends="clear" description="创建初始化目录结构">
<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.7" target="1.7" deprecation="true" debug="true"
encoding="UTF-8" classpathref="project.classpath">
<compilerarg line="-Xlint:unchecked" />
<!-- <classpath refid="project.classpath" /> -->
</javac>
<copy todir="${build.dir}/classes">
<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="**/*.ftl" />
<include name="**/*.xls" />
<include name="**/*.ttc" />
</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包, 名称默认为 项目名 -->
<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>
<fileset dir="${config.dir}">
<include name="***.*" />
</fileset>
</war>
<echo message="end war..." />
</target>
<!-- copy war包 tomcat的deploy目录 -->
<target name="deploy" depends="war" description="部署项目">
<echo message="begin deploy..." />
<copy file="${dist.dir}/${webapp.name}.war" todir="${catalina.home}/webapps" />
<echo message="end deploy..." />
</target>
</project>
参考:
http://www.voidcn.com/blog/wangyajin333/article/p-26662.html
http://www.zving.com/c/2014-05-30/258250.shtml
http://blog.csdn.net/db308950372/article/details/9225539
http://blog.sina.com.cn/s/blog\_b0d01b5b0102w6va.html