使用Apache ant 1.9.6編譯打包Web專案
阿新 • • 發佈:2019-02-15
環境:
1,OS:Windows7
2,JDK:jdk1.7
3,Apache Ant:apache-ant-1.9.6
步驟:
1,解壓apache-ant-1.9.6-bin.zip
2,新增環境變數ANT_HOME,比如C:\apache-ant-1.9.6-bin
3,Path變數新增ant的bin目錄,比如%ANT_HOME%\bin
目標流程:
1,刪除web project web-info目錄下的classes目錄;刪除.war檔案
2,建立web project web-info目錄下的classes目錄
3,複製src下的非java檔案
4,編譯生成java的class檔案
5,打包成.war檔案
Ant build.xml檔案
<?xml version="1.0" encoding="UTF-8"?> <project name="webprjtest" default="createwebwarfile" basedir="F:/"> <property name="webproject.base" value="${basedir}/webtestproject/" ></property> <property name="webproject.src" value="${webproject.base}/src/" ></property> <property name="webproject.root" value="${webproject.base}/WebRoot/" ></property> <property name="webproject.lib" value="${webproject.root}/WEB-INF/lib/"></property> <property name="webproject.classes" value="${webproject.root}/WEB-INF/classes/"></property> <property name="appname" value="${ant.project.name}" ></property> <property name="warfile" value="${basedir}/${appname}.war" ></property> <!-- J2EE lib--> <property name="tomcat.lib" value="C:/tomcat/lib/"></property> <target name="del"> <delete dir="${webproject.classes}"></delete> <delete file="${warfile}"></delete> </target> <target name="md"> <mkdir dir="${webproject.classes}"/> </target> <path id="classpath"> <fileset dir="${webproject.lib}"> <include name="*.jar"/> </fileset> <fileset dir="${tomcat.lib}"> <include name="*.jar"/> </fileset> </path> <property name="classpath" refid="classpath"></property> <target name="buildjava" description="Build java files and Copy other files in src ."> <javac srcdir="${webproject.src}" destdir="${webproject.classes}" classpath="${classpath}" encoding="utf-8" debug="true" deprecation="true"> </javac> </target> <target name="cp"> <copy todir="${webproject.classes}"> <fileset dir="${webproject.src}"> <exclude name="**/*.java"/> </fileset> </copy> </target> <target name="createwebwarfile" depends="del,md,cp,buildjava" description="Create War file from Web-Project ."> <war destfile="${warfile}" webxml="${webproject.root}/WEB-INF/web.xml"> <fileset dir="${webproject.root}" > <include name="**" /> </fileset> </war> </target> </project>
===================================================================================
環境:
1,OS:Windows7
2,JDK:jdk1.8
3,Apache Ant:apache-ant-1.9.6
4,Tomcat8.5.3
目標:
對JavaWeb專案編譯、打包、並在tomcat中執行
程式碼:
<?xml version="1.0" encoding="UTF-8"?> <!-- ====================================================================== ====================================================================== --> <project name="monitor" default="default" basedir="D:/workspace_ElipseJEE_mars2"> <description> Web Project built with JDK8 and run on Tomcat8.5.3 </description> <record action="start" append="no" loglevel="info" name="e:/antbuildwar.log"/> <property name="webprj.base" value="${basedir}/monitor/"></property> <property name="webprj.src" value="${webprj.base}/src/"></property> <property name="webprj.root" value="${webprj.base}/WebRoot/"></property> <property name="webprj.lib" value="${webprj.root}/WEB-INF/lib/"></property> <property name="webprj.classes" value="${webprj.root}/WEB-INF/classes/"></property> <property name="appname" value="${ant.project.name}"></property> <property name="warfile" value="${basedir}/${appname}.war"></property> <!-- tomcat --> <property name="tomcat.home" value="F:/software/develop/apache-tomcat-8.5.3_win32/"></property> <property name="tomcat.lib" value="${tomcat.home}/lib/"></property> <property name="tomcat.webappfoler" value="${tomcat.home}/webapps/"></property> <!-- ================================= target: default ================================= --> <target name="default" depends="startuptomcat8.5" description="description"> </target> <!-- Delete old *.class folder and .war file --> <target name="delclasses"> <delete dir="${webprj.classes}"></delete> <delete file="${warfile}"></delete> </target> <!-- Create classes dir --> <target name="md" > <mkdir dir="${webprj.classes}"/> </target> <!-- 需要引用的jar包 --> <path id="classpathset"> <fileset dir="${webprj.lib}"> <include name="*.jar"/> </fileset> <fileset dir="${tomcat.lib}"> <include name="*.jar"/> </fileset> </path> <property name="classpath" refid="classpathset"></property> <!-- - - - - - - - - - - - - - - - - - target: buildjava,編譯 - - - - - - - - - - - - - - - - - --> <target name="buildjava"> <javac verbose="yes" listfiles="yes" classpath="${classpath}" srcdir="${webprj.src}" destdir="${webprj.classes}" fork="java$$javac.exe" source="1.8" target="1.8" encoding="utf-8" debug="on" deprecation="on" > <compilerarg value="-Xlint"/> </javac> </target> <!-- - - - - - - - - - - - - - - - - - target: cp,複製其他一些非java檔案 - - - - - - - - - - - - - - - - - --> <target name="cp"> <copy todir="${webprj.classes}"> <fileset dir="${webprj.src}"> <exclude name="**/*.java"/> </fileset> </copy> </target> <!-- - - - - - - - - - - - - - - - - - target: createwarfile - - - - - - - - - - - - - - - - - --> <target name="createwarfile" depends="delclasses,md,cp,buildjava"> <war destfile="${warfile}" webxml="${webprj.root}/WEB-INF/web.xml"> <fileset dir="${webprj.root}"> <include name="**"/> </fileset> </war> </target> <!-- - - - - - - - - - - - - - - - - - target: cpwar - - - - - - - - - - - - - - - - - --> <target name="cpwar" depends="createwarfile"> <copy todir="${tomcat.webappfoler}" file="${warfile}" overwrite="yes"></copy> </target> <!-- - - - - - - - - - - - - - - - - - target: startuptomcat8.5, 執行外部命令, 如果exec中沒有dir屬性設定,那麼tomcat不啟動成功,報CATALINA_HOME沒設定 - - - - - - - - - - - - - - - - - --> <target name="startuptomcat8.5" depends="cpwar"> <exec executable="cmd.exe" dir="${tomcat.home}"> <arg line="/c ${tomcat.home}/bin/startup.bat"/> </exec> </target> </project>