ANT指令build.xml[ANT运行的配置文件]命令集
1.ANT示例1.0(将指定路径下的文件复制到指定的其他位置)
<?xml version="1.0" encoding="GBK"?>
<project [ Project根标签]name[ name –项目名称
(3) basedir property覆盖。如果属性和basedir property都没有设定,就使用buildfile文件的父目录。 ]="测试脚本" default[default –当没有指定target时使用的缺省target]=”copyfile” basedir[basedir –用于计算所有其他路径的基路径。该属性可以被]="." >
<target name="copyfile">
<copy file="d:/a.txt" todir="e:/Temp" overwrite="true" />
</target>
</project>
2.ANT示例1.1(编译、运行项目antTest)
<?xml version="1.0" encoding="UTF-8" ?>
<project name="antTest" default="run" basedir=".">
<property [配置文件中的变量]name="src" value="src" />
<property name="dest" value="classes" />
<property name="hello_jar" value="hello1.jar[ANT打包jar的名称。如打war包:hello1.war]" />
<target[ANT执行命令,初始化命令.这里mkdir创建目录命令] name="init">
<mkdir dir="${dest}" />
</target>
<target name="compile" depends="init"[在执行该命令前执行init命令]>
<javac srcdir[创建scr目录,即classes]="${src}" destdir="${dest}" />
</target>
<target name="build" depends="compile">
<jar jarfile="${hello_jar}[jar文件生成位置]" basedir="${dest}" />
</target>
<target name="run" depends="build">
<java classname="test.ant.HelloWorld"[打包指定的class类] classpath="${hello_jar}" />
</target>
<target name="clean">[打包完成后,删除.class文件]
<delete dir="${dest}" />
<delete file="${hello_jar}" />
</target>
<target name="rerun" depends="clean,run">
<ant target="clean" />
<ant target="run" />
</target>
</project>
3. ANT示例1.2(编译、运行项目antTest)
<?xml version="1.0" encoding="UTF-8" ?>
<project name="antTest" default="war"[默认执行war命令] basedir=".">
<property name="classes" value="build/classes" />
<property name="build" value="build" />
<property name="lib" value="WebRoot/WEB-INF/lib" />
<!– 删除build路径–>
<target name="clean">
<delete dir="build" />
</target>
<!– 建立build/classes路径,并编译class文件到build/classes路径下–>
<target name="compile" depends="clean">
<mkdir dir="${classes}" />
<javac srcdir="src" destdir="${classes}" includeantruntime="on" />
</target>
<!– 打war包–>
<target name="war" depends="compile">[这里先执行找war命令,找到war命令后,发现在执行war行时,必须先执行compile命令。当找到compile命令后,发现必须先执行clean命令。找到clean命令后,开始执行。实际上,这里最开始执行的是clean命令,然后接着一层一层的执行。]
<war destfile="${build}/antTest.war" webxml="WebRoot/WEB-INF/web.xml">
<!– 拷贝WebRoot下除了WEB-INF和META-INF的两个文件夹–>
<fileset dir="WebRoot" includes="**/*.jsp" />
<!– 拷贝lib目录下的jar包–>
<lib dir="${lib}" />
<!– 拷贝build/classes下的class文件–>
<classes dir="${classes}"/>
</war>
</target>
</project>
4. ANT示例1.3(编译、运行项目antTest)
<?xml version="1.0" encoding="UTF-8" ?>
<project name="antTest" default="compile" basedir=".">
<target name="clean">
<delete dir="build" />[删除文件build命令]
</target>
<target name="compile" depends="clean">
<mkdir dir="build/classes" />[创建多级目录]
<javac srcdir="src" destdir="build/classes" />
</target>
</project>
5. ANT示例1.4(编译、运行项目antTest)
<?xml version="1.0" encoding="UTF-8" ?>
<project name="javaTest" default="jar" basedir=".">
<target name="clean">
<delete dir="build" />
</target>
<target name="compile" depends="clean">
<mkdir dir="build/classes" />
<javac srcdir="src" destdir="build/classes" />
</target>
<target name="run" depends="compile">
<java classname="test.ant.HelloWorld">
<classpath>
<pathelement path="build/classes" />
</classpath>
</java>
</target>
<target name="jar" depends="run">
<jar destfile="helloworld.jar" basedir="build/classes">
<manifest[指定main方法配置]>
<attribute name="Main-class" value="HelloWorld" />[指定HelloWorld类的main方法]
</manifest>
</jar>
</target>
</project>
6. ANT示例1.5(编译、运行项目)复杂的配置
<?xml version="1.0" encoding="UTF-8"?>
<project name="antTest" basedir="." default="pack.web[找pack.web命令]">
<property name="build.debug" value="on" />
<property name="build.debug.level" value="lines,vars,source" />
<property name="compile.deprecation" value="false" />
<property name="compile.optimize" value="true" />
<property name="source.excludes" value="applicationContext.xml, jdbc.properties " />
<property name="portal.opensource.excludes" value="wlclient.jar,servlet-api.jar,j2ee.jar" />
<property name="lib.dev.excludes" value="a.jar" />
<property name="source.dao.dir" value="${basedir}/../dao" />
<property name="source.mgr.dir" value="${basedir}/../mgr" />
<property name="source.portal.dir" value="${basedir}/../web" />
<property name="build.dir" value="c:/web" />
<property name="lib.dir" value="${basedir}/../libIbssPub" />
<property name="lib.dev.dir" value="${lib.dir}/dev" />
<property name="lib.opensource.dir" value="${lib.dir}/opensource" />
<property name="lib.weblogic.dir" value="${lib.dir}/weblogic" />
<target name="clean">
<delete dir="${build.dir}" />
<mkdir dir="${build.dir}/mgr" />
<mkdir dir="${build.dir}/dao" />
</target>
<path id="general.compile.classpath">
<fileset dir="${lib.opensource.dir}" includes="**/*.jar" />
<fileset dir="${lib.dev.dir}" includes="**/*.jar" />
<fileset dir="${lib.weblogic.dir}" includes="**/*.jar" />
</path>
<target name="compile.mgr">
<javac destdir="${build.dir}/mgr" encoding="GBK" debug="${build.debug}" debuglevel="${build.debug.level}"[指定日志级别] deprecation="${compile.deprecation}" optimize="${compile.optimize}">
<src path="${source.mgr.dir}/src" />
<classpath refid="general.compile.classpath" />
<classpath>
<fileset file="${build.dir}/dao.jar" />
</classpath>
</javac>
<copy todir="${build.dir}/mgr">
<fileset dir="${source.mgr.dir}/src" excludes[排除指定路径下的文件的命令]="**/*.java, **/*.class, log4j.properties, ${source.excludes}" />
</copy>
</target>
<target name="compile.dao">
<javac srcdir="${source.dao.dir}/src" destdir="${build.dir}/dao" encoding="GBK"[指定编码GBK] debug=”${build.debug}” debuglevel=”${build.debug.level}” deprecation=”${compile.deprecation}” optimize=”${compile.optimize}”[优化指定目录]>
<classpath refid="general.compile.classpath" />
</javac>
<copy todir="${build.dir}/dao" overwrite="true">
<fileset dir="${source.dao.dir}/src" excludes="**/*.java, **/*.class, log4j.properties, ${source.excludes}" />
</copy>
</target>
<target name="pack.dao" depends="compile.dao">
<delete file="${build.dir}/dao.jar" />
<jar destfile="${build.dir}/dao.jar">
<fileset dir="${build.dir}/dao" />
</jar>
</target>
<target name="pack.mgr" depends="compile.mgr">
<delete file="${build.dir}/mgr.jar" />
<jar destfile="${build.dir}/mgr.jar">
<fileset dir="${build.dir}/mgr" />
</jar>
</target>
<target name="pack.portal">
<war destfile="${build.dir}/web.war" webxml="${source.portal.dir}/WebRoot/WEB-INF/web.xml">
<fileset dir="${source.portal.dir}/WebRoot" />
<lib dir="${lib.dev.dir}" excludes="${lib.dev.excludes}" />
<lib dir="${lib.opensource.dir}" excludes="${portal.opensource.excludes}" />
<lib dir="${build.dir}" includes="*.jar"[导入项目中引用的jar包文件] />
</war>
</target>
<target name="pack.web" description="打包WEB"[备注命令]>
<echo file[创建jdbc.properties文件,并新增内容]="${source.portal.dir}/src/jdbc.properties" append="false" message="jdbc.driverClassName=oracle.jdbc.driver.OracleDriver${line.separator}" />
<echo file="${source.portal.dir}/src/jdbc.properties" append="true" message="jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl${line.separator}" />
<echo file="${source.portal.dir}/src/jdbc.properties" append="true" message="jdbc.username=web_dev${line.separator}" />
<echo file="${source.portal.dir}/src/jdbc.properties" append="true" message="jdbc.password=devweb${line.separator}" />
<echo file="${source.portal.dir}/src/jdbc.properties" append="true" message="datasource.name=jdbc/web${line.separator}" />
<antcall target="clean" />[先执行clean命令]
<antcall target="pack.dao" />[等待clean命令执行完毕后,执行pack.dao命令]
<antcall target="pack.mgr" />[等待pack.dao命令执行完毕后,执行pack.mgr命令]
<delete dir="${build.dir}/mgr" />
<delete dir="${build.dir}/dao" />
<antcall target="pack.portal" />
<exec executable="cmd.exe">[打开cmd.exe, 切换到c盘,开始启动tomcat]
<arg value="/c" />
<arg value="start ${build.dir}" />
</exec>
</target>
<property name="tomcat.home" value="D:/apache-tomcat-6.0.20[项目打包部署路径]" />
<target name="deploy.tomcat" description="部署到tomcat">
<copy todir="${tomcat.home}/webapps" overwrite="true">
<fileset dir="${build.dir}" includes="web.war" />
</copy>
</target>
<target name="tomcat.start">[启动tomcat命令]
<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
<jvmarg value="-Dcatalina.home=${tomcat.home}" />
<jvmarg value="-Dsys.run.environment=SPS" />
<jvmarg value="-Xdebug" />
</java>
</target>
<target name="tomcat.stop[停止tomcat命令]">
<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
<jvmarg value="-Dcatalina.home=${tomcat.home}" />
<arg line="stop" />
</java>
</target>
</project>
: » ANT指令build.xml[ANT运行的配置文件]命令集
原创文章,作者:306829225,如若转载,请注明出处:https://blog.ytso.com/251524.html