上一篇我们了解了去部署一个maven项目,但是这在我们开发中是不够的,框架多样,遇到非maven项目,你再使用上节的内容就有点不够用了,我们今天看看怎么使用ant去部署一个非maven项目;
首先我们了解一下ant是做什么的:
1.Ant是apache软件基金会jakarta目录中的一个子项目:
我们都知道项目每次改动都要重新编译,打包,测试,一直重复这样的工作,ant本身就是这样一个流程脚本引擎,用于自动化调用程序完成项目的编译,打包,测试等工作;每个ant脚本(build.xml)中设置了一系列任务(target)。
具有以下特点:
1).跨平台性:ant是纯java语言编写的,大家都知道java就是跨平台性的;
2).操作简单:ant是由一个内置任务和可选任务组成的,ant运行时需要一个xml文件,就是构建文件,ant通过调用target树,就可以执行各种task,每个task实现了特定的接口对象,由于ant构建文件时xml格式的文件,容易维护和书写,结构简单清晰;
3).ant可以集成到开发环境中,
2.我们使用的工具eclipse,sts等工具都可以生成一个build.xml;我们首先看看怎么生成这个
1).找到我们要配置build.xml的项目右击—》export
然后一直下一步,就会在项目根路径下生成一个build.xml文件;如下:我们大概看一下这儿xml了解ant,下面我们来手动编写我们自己需要的xml,适应于各个环境的;
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. --><project basedir="." default="build" name="ttp">
<property environment="env"/>
<property name="ECLIPSE_HOME" value="E:/RuanJianBao/Ouye/sts-3.7.1.RELEASE/"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.6"/>
<property name="source" value="1.6"/>
<path id="Web App Libraries.libraryclasspath">
<pathelement location="WebContent/WEB-INF/lib/activation-1.1.1.jar"/>
<pathelement location="WebContent/WEB-INF/lib/activemq-core-5.7.0.jar"/>
<pathelement location="WebContent/WEB-INF/lib/aopalliance-1.0.jar"/>
<pathelement location="WebContent/WEB-INF/lib/aspectjweaver-1.7.3.jar"/>
<pathelement location="WebContent/WEB-INF/lib/axis-ant.jar"/>
<pathelement location="WebContent/WEB-INF/lib/axis-schema.jar"/>
<pathelement location="WebContent/WEB-INF/lib/axis.jar"/>
<pathelement location="WebContent/WEB-INF/lib/bccs-api-3.0.1.jar"/>
<pathelement location="WebContent/WEB-INF/lib/bcprov-jdk14-136.jar"/>
<pathelement location="WebContent/WEB-INF/lib/cas-client-core-3.2.1.jar"/>
<pathelement location="WebContent/WEB-INF/lib/cglib-nodep-2.1_3.jar"/>
<pathelement location="WebContent/WEB-INF/lib/commons-beanutils-1.8.3.jar"/>
<pathelement location="WebContent/WEB-INF/lib/commons-codec-1.10.jar"/>
<pathelement location="WebContent/WEB-INF/lib/commons-codec-1.6.jar"/>
<pathelement location="WebContent/WEB-INF/lib/commons-collections-3.2.jar"/>
<pathelement location="WebContent/WEB-INF/lib/commons-dbcp-1.4.jar"/>
<pathelement location="WebContent/WEB-INF/lib/commons-discovery-0.2.jar"/>
<pathelement location="WebContent/WEB-INF/lib/commons-fileupload-1.2.jar"/>
<pathelement location="WebContent/WEB-INF/lib/commons-httpclient-3.0.jar"/>
<pathelement location="WebContent/WEB-INF/lib/commons-io-1.2.jar"/>
<pathelement location="WebContent/WEB-INF/lib/commons-io-1.3.2.jar"/>
<pathelement location="WebContent/WEB-INF/lib/commons-lang-2.4.jar"/>
<pathelement location="WebContent/WEB-INF/lib/commons-lang3-3.3.2.jar"/>
<pathelement location="WebContent/WEB-INF/lib/commons-logging-1.0.4.jar"/>
<pathelement location="WebContent/WEB-INF/lib/commons-logging-1.1.1.jar"/>
<pathelement location="WebContent/WEB-INF/lib/commons-net-1.4.1.jar"/>
<pathelement location="WebContent/WEB-INF/lib/commons-pool-1.5.4.jar"/>
<pathelement location="WebContent/WEB-INF/lib/dfft.jar"/>
<pathelement location="WebContent/WEB-INF/lib/dom4j-1.6.1.jar"/>
<pathelement location="WebContent/WEB-INF/lib/ezmorph-1.0.1.jar"/>
<pathelement location="WebContent/WEB-INF/lib/fastjson-1.2.6.jar"/>
<pathelement location="WebContent/WEB-INF/lib/fel.jar"/>
<pathelement location="WebContent/WEB-INF/lib/freemarker-2.3.8.jar"/>
<pathelement location="WebContent/WEB-INF/lib/geronimo-j2ee-management_1.0_spec-1.0.jar"/>
<pathelement location="WebContent/WEB-INF/lib/geronimo-jms_1.1_spec-1.1.1.jar"/>
<pathelement location="WebContent/WEB-INF/lib/geronimo-jta_1.0.1B_spec-1.0.1.jar"/>
<pathelement location="WebContent/WEB-INF/lib/gson-1.5.jar"/>
<pathelement location="WebContent/WEB-INF/lib/httpclient-4.4.1.jar"/>
<pathelement location="WebContent/WEB-INF/lib/httpcore-4.4.1.jar"/>
<pathelement location="WebContent/WEB-INF/lib/httpmime-4.4.1.jar"/>
<pathelement location="WebContent/WEB-INF/lib/jackson-annotations-2.4.0.jar"/>
<pathelement location="WebContent/WEB-INF/lib/jackson-core-2.4.2.jar"/>
<pathelement location="WebContent/WEB-INF/lib/jackson-core-lgpl-1.9.13.jar"/>
<pathelement location="WebContent/WEB-INF/lib/jackson-databind-2.4.2.jar"/>
<pathelement location="WebContent/WEB-INF/lib/jackson-mapper-lgpl-1.9.13.jar"/>
<pathelement location="WebContent/WEB-INF/lib/jaxrpc.jar"/>
<pathelement location="WebContent/WEB-INF/lib/jcommon-1.0.12.jar"/>
<pathelement location="WebContent/WEB-INF/lib/jfreechart-1.0.9.jar"/>
<pathelement location="WebContent/WEB-INF/lib/json-lib-2.2.3-jdk13.jar"/>
<pathelement location="WebContent/WEB-INF/lib/jstl-1.2.jar"/>
<pathelement location="WebContent/WEB-INF/lib/jta-1.0.1B.jar"/>
<pathelement location="WebContent/WEB-INF/lib/log4j-1.2.12.jar"/>
<pathelement location="WebContent/WEB-INF/lib/log4j-over-slf4j-1.6.1.jar"/>
<pathelement location="WebContent/WEB-INF/lib/mail-1.4.7.jar"/>
<pathelement location="WebContent/WEB-INF/lib/mailapi.jar"/>
<pathelement location="WebContent/WEB-INF/lib/mongo-Java-driver-2.13.0.jar"/>
<pathelement location="WebContent/WEB-INF/lib/mybatis-3.2.3.jar"/>
<pathelement location="WebContent/WEB-INF/lib/mybatis-spring-1.2.1.jar"/>
<pathelement location="WebContent/WEB-INF/lib/mysql-connector-java-5.1.26.jar"/>
<pathelement location="WebContent/WEB-INF/lib/ojdbc-14-10.2.0.4.0.jar"/>
<pathelement location="WebContent/WEB-INF/lib/opensaml1-1.1.jar"/>
<pathelement location="WebContent/WEB-INF/lib/pinyin4j-2.5.0.jar"/>
<pathelement location="WebContent/WEB-INF/lib/plusoft.jar"/>
<pathelement location="WebContent/WEB-INF/lib/poi-3.10.1-20140818.jar"/>
<pathelement location="WebContent/WEB-INF/lib/poi-3.6-20091214.jar"/>
<pathelement location="WebContent/WEB-INF/lib/poi-examples-3.10.1-20140818.jar"/>
<pathelement location="WebContent/WEB-INF/lib/poi-excelant-3.10.1-20140818.jar"/>
<pathelement location="WebContent/WEB-INF/lib/poi-ooxml-3.10.1-20140818.jar"/>
<pathelement location="WebContent/WEB-INF/lib/poi-ooxml-3.6-20091214.jar"/>
<pathelement location="WebContent/WEB-INF/lib/poi-ooxml-schemas-3.10.1-20140818.jar"/>
<pathelement location="WebContent/WEB-INF/lib/poi-ooxml-schemas-3.6-20091214.jar"/>
<pathelement location="WebContent/WEB-INF/lib/poi-scratchpad-3.10.1-20140818.jar"/>
<pathelement location="WebContent/WEB-INF/lib/poi-scratchpad-3.6-20091214.jar"/>
<pathelement location="WebContent/WEB-INF/lib/quartz-1.6.0.jar"/>
<pathelement location="WebContent/WEB-INF/lib/saaj.jar"/>
<pathelement location="WebContent/WEB-INF/lib/servlet-api.jar"/>
<pathelement location="WebContent/WEB-INF/lib/slf4j-api-1.6.6.jar"/>
<pathelement location="WebContent/WEB-INF/lib/slf4j-log4j12-1.7.5.jar"/>
<pathelement location="WebContent/WEB-INF/lib/slf4j-simple-1.7.7.jar"/>
<pathelement location="WebContent/WEB-INF/lib/spring-aop-3.1.4.RELEASE.jar"/>
<pathelement location="WebContent/WEB-INF/lib/spring-asm-3.1.4.RELEASE.jar"/>
<pathelement location="WebContent/WEB-INF/lib/spring-aspects-3.1.4.RELEASE.jar"/>
<pathelement location="WebContent/WEB-INF/lib/spring-beans-3.1.4.RELEASE.jar"/>
<pathelement location="WebContent/WEB-INF/lib/spring-context-3.1.4.RELEASE.jar"/>
<pathelement location="WebContent/WEB-INF/lib/spring-context-support-3.1.4.RELEASE.jar"/>
<pathelement location="WebContent/WEB-INF/lib/spring-core-3.1.4.RELEASE.jar"/>
<pathelement location="WebContent/WEB-INF/lib/spring-expression-3.1.4.RELEASE.jar"/>
<pathelement location="WebContent/WEB-INF/lib/spring-jdbc-3.1.4.RELEASE.jar"/>
<pathelement location="WebContent/WEB-INF/lib/spring-jms-3.2.8.RELEASE.jar"/>
<pathelement location="WebContent/WEB-INF/lib/spring-security-cas-3.1.4.RELEASE.jar"/>
<pathelement location="WebContent/WEB-INF/lib/spring-security-core-3.1.4.RELEASE.jar"/>
<pathelement location="WebContent/WEB-INF/lib/spring-security-web-3.1.4.RELEASE.jar"/>
<pathelement location="WebContent/WEB-INF/lib/spring-tx-3.1.4.RELEASE.jar"/>
<pathelement location="WebContent/WEB-INF/lib/spring-web-3.1.4.RELEASE.jar"/>
<pathelement location="WebContent/WEB-INF/lib/spring-webmvc-3.1.4.RELEASE.jar"/>
<pathelement location="WebContent/WEB-INF/lib/standard.jar"/>
<pathelement location="WebContent/WEB-INF/lib/stax-api-1.0.1.jar"/>
<pathelement location="WebContent/WEB-INF/lib/wsdl4j-1.6.2.jar"/>
<pathelement location="WebContent/WEB-INF/lib/xalan-2.7.0.jar"/>
<pathelement location="WebContent/WEB-INF/lib/xbean-spring-4.5.jar"/>
<pathelement location="WebContent/WEB-INF/lib/xmlbeans-2.6.0.jar"/>
<pathelement location="WebContent/WEB-INF/lib/xmlsec-1.4.3.jar"/>
<pathelement location="WebContent/WEB-INF/lib/xwork-2.0.4.jar"/>
</path>
<path id="EAR Libraries.libraryclasspath"/>
<path id="Persisted container [org.apache.ivyde.eclipse.cpcontainer.IVYDE_CONTAINER/?project=iframework-management&ivyXmlPath=ivy.xml&confs=default&ivySettingsPath=&loadSettingsOnDemand=false&ivyUserDir=&propertyFiles= for project [ttp]].libraryclasspath"/>
<path id="Apache Tomcat v7.0 [Apache Tomcat v7.0].libraryclasspath">
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/annotations-api.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/catalina-ant.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/catalina-ha.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/catalina-tribes.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/catalina.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/ecj-4.2.2.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/el-api.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/jasper-el.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/jasper.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/jsp-api.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/servlet-api.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/tomcat-api.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/tomcat-coyote.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/tomcat-dbcp.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/tomcat-i18n-es.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/tomcat-i18n-fr.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/tomcat-i18n-ja.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/tomcat-jdbc.jar"/>
<pathelement location="E:/tomcat/tomcat-7.0/tomcat-7.0/lib/tomcat-util.jar"/>
</path>
<path id="ttp.classpath">
<pathelement location="build/classes"/>
<path refid="Web App Libraries.libraryclasspath"/>
<path refid="EAR Libraries.libraryclasspath"/>
<path refid="Persisted container [org.apache.ivyde.eclipse.cpcontainer.IVYDE_CONTAINER/?project=iframework-management&ivyXmlPath=ivy.xml&confs=default&ivySettingsPath=&loadSettingsOnDemand=false&ivyUserDir=&propertyFiles= for project [ttp]].libraryclasspath"/>
<pathelement location="WebContent/WEB-INF/lib/commons-codec-1.10.jar"/>
<pathelement location="WebContent/WEB-INF/lib/fel.jar"/>
<path refid="Apache Tomcat v7.0 [Apache Tomcat v7.0].libraryclasspath"/>
</path>
<target name="init">
<mkdir dir="build/classes"/>
<copy includeemptydirs="false" todir="build/classes">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
<copy includeemptydirs="false" todir="build/classes">
<fileset dir="resources">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="build/classes"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="build/classes" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<src path="resources"/>
<classpath refid="ttp.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
</project>
3.编写自己的build.xml,我们编写这个初衷是把重复的事情交给计算机去做,释放人力;
我们看一下下面这个我们自己写的,可以看到都是有每个target标签,我们可以把每个target标签任务是一步,我们想一下打成自己想要的包会有几步:
a.创建自己想要的打包后的目录,
b.javac编译java文件为class文件;并且把对应配置文件放到打包后对应的路径下
c.编译成后我们就是打包成war包
d.有了war包后就是部署了
所以以下其实就是这么一个过程,我们来看看部分含义吧
<project basedir="." default="public" name="ttp"></project>
ant所有的内容都必须在这里面;basedir:工作的根目录,.表示当前目录;default表示默认要做的事;name就是你给项目取得名字;
<target name="prepare" depends="init" description="创建打包需要的路径,拷贝源文件到打包路径下">
<mkdir dir="${build.src}"/>
</target>
我们把我们想要做的每件事,可以说是每一步定义成一个target,有一个名字name;depends是它所依赖的target,在执行自己时他会首先执行所依赖的target执行以后才会执行自己;description是这个target的描述,用来做什么
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="public" name="ttp">
<target name="init" description="设置初始化打 war 包需要的路径变量">
<property name="name" value="ttp"/>
<property name="src" value="${basedir}/src"/>
<property name="lib" value="${basedir}/WebContent/WEB-INF/lib"/>
<property name="webapp" value="${basedir}/WebContent"/>
<property name="resource" value="${basedir}/resources/"/>
<property name="build.encoding" value="UTF-8" />
<property name="build.src" value="${basedir}/temp/ttp/src"/>
<property name="build.classes" value="${basedir}/temp/ttp/WEB-INF/classes"/>
<property name="buildwar.webapp" value="${basedir}/temp/ttp"/>
<property name="build.javadocs" value="${basedir}/temp/doc"/>
<property name="jar.dest" value="${basedir}/temp/jar"/>
<property name="war.dest" value="${basedir}/temp/war"/>
<!-- <property name="tomcat.home" value="/opt/tomcat"/> -->
<path id="classpath">
<fileset dir="${lib}">
<include name="**/*.jar"/>
</fileset>
<fileset dir="E:/tomcat/tomcat-7.0/tomcat-7.0/lib">
<include name="**/*.jar"/>
</fileset>
<fileset dir="E:/jdk/jdk1.7.0_80/jdk1.7.0_80/jre/lib">
<include name="**/*.jar"/>
</fileset>
</path>
</target>
<target name="prepare" depends="init" description="创建打包需要的路径,拷贝源文件到打包路径下">
<echo>mkdir base start</echo>
<mkdir dir="${build.src}"/>
<mkdir dir="${build.classes}"/>
<mkdir dir="${buildwar.dest}"/>
<mkdir dir="${jar.dest}"/>
<mkdir dir="${war.dest}"/>
<echo>mkdir base end AND star copy ${basedir}</echo>
<copy todir="${build.src}">
<fileset dir="${src}"/>
</copy>
<copy todir="${build.classes}">
<fileset dir="${resource}"/>
</copy>
<copy todir="${buildwar.webapp}">
<fileset dir="${webapp}"/>
</copy>
</target>
<target name="build" depends="prepare" description="编译 java 文件,拷贝 properties 属性配置文件到编译后的路径下">
<echo message="开始编译" />
<javac srcdir="${build.src}" destdir="${build.classes}"
executable="E:/jdk/jdk1.7.0_80/jdk1.7.0_80/bin/javac"
includeantruntime="false"
encoding="${build.encoding}"
target="1.7"
source="1.7"
>
<classpath refid="classpath"/>
</javac>
</target>
<target name="cpmapper" depends="build" description="将mapper对应的xml文件拷入相应classes的mapper类目录">
<copy todir="${build.classes}/com/telegnosis/iframework/management/persistence">
<fileset dir="${src}/com/telegnosis/iframework/management/persistence">
<include name="*.xml"/>
</fileset>
</copy>
<copy todir="${build.classes}/com/telegnosis/iframework/sqlMapper">
<fileset dir="${src}/com/telegnosis/iframework/sqlMapper">
<include name="*.xml"/>
</fileset>
</copy>
<copy todir="${build.classes}/com/telegnosis/iframework/distance/dao">
<fileset dir="${src}/com/telegnosis/iframework/distance/dao">
<include name="*.xml"/>
</fileset>
</copy>
<copy todir="${build.classes}/com/telegnosis/iframework/deviceCar/dao">
<fileset dir="${src}/com/telegnosis/iframework/deviceCar/dao">
<include name="*.xml"/>
</fileset>
</copy>
<copy todir="${build.classes}/com/telegnosis/iframework/dispatch/dao">
<fileset dir="${src}/com/telegnosis/iframework/dispatch/dao">
<include name="*.xml"/>
</fileset>
</copy>
<copy todir="${build.classes}/com/telegnosis/iframework/intoStms/dao">
<fileset dir="${src}/com/telegnosis/iframework/intoStms/dao">
<include name="*.xml"/>
</fileset>
</copy>
<copy todir="${build.classes}/com/telegnosis/iframework/operateLog/mapper">
<fileset dir="${src}/com/telegnosis/iframework/operateLog/mapper">
<include name="*.xml"/>
</fileset>
</copy>
<copy todir="${build.classes}/com/telegnosis/iframework/credit/dao">
<fileset dir="${src}/com/telegnosis/iframework/credit/dao">
<include name="*.xml"/>
</fileset>
</copy>
<copy todir="${build.classes}/com/telegnosis/iframework/userGradeSum/dao">
<fileset dir="${src}/com/telegnosis/iframework/userGradeSum/dao">
<include name="*.xml"/>
</fileset>
</copy>
<copy todir="${build.classes}">
<fileset dir="${src}">
<include name="*.xml"/>
<include name="*.properties"/>
</fileset>
</copy>
</target>
<target name="antwar" depends="cpmapper" description="打 war 包,不将 java 文件打入包内">
<delete dir="${build.src}"/>
<war warfile="${war.dest}/ttp.war" webxml="${buildwar.webapp}/WEB-INF/web.xml">
<lib dir="${buildwar.webapp}/WEB-INF/lib"/>
<classes dir="${build.classes}"/>
<fileset dir="${buildwar.webapp}"/>
</war>
</target>
<target name="public" depends="clean,antwar" description="打包编译开始,并清除war包">
<!-- <copy todir="${tomcat.home}/webapps">
<fileset dir="${war.dest}">
<include name="**/*.war"/>
</fileset>
</copy> -->
</target>
<target name="clean" depends="init" description="清除打包用临时文件">
<echo message="清理war包"></echo>
<delete dir="${war.dest}"/>
</target>
</project>
这就是一个基本的build.xml我们最重要的脚本有了,就该部署jenkins了
部署jenkins实现一键发布;
构建过程和上一篇基本一致,选择自由风格项目构建;只有一步在第四步构建—添加构建步骤选择:invoke Ant;如果没有这个选项同样先去安装插件;然后选择在系统配置中ant即可;然后就可以构建;
对于jenkins来说不仅仅可以部署项目,还可以做自动化单元测试;生成自己想要的报告;jenkins功能强大;自己可以做一些研究,欢迎留言沟通