使用Apache ant 1.9.6编译打包Web项目

时间:2022-08-03 18:39:16

环境:

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>