如何使用ant

第一步:从http://ant.apache.org 下载 apache-ant-1.7.0-bin.zip,解压到你自己的目录,我的:E:\apache-ant-1.7.0

第二步:配置 JAVA_HOME 和 ANT_HOME
配置完成后,打开dos窗口,输入 ant 回车,如果提示:
Buildfile: build.xml does not exist!
Build failed

则说明配置完成

第三步:创建自己的工程
我的工程:test
目录结构:
D:\work\code\test_core\src
D:\work\code\test_core\lib


第四步:编写build.xml
<?xml version="1.0" encoding="UTF-8"?>

<project default="jar" name="test" basedir=".">
  
  <property name="defaulttargetdir" value="./target"></property> 
  <property name="classesdir" value="./target/classes"></property>
  <property name="logsdir" value="./logs"></property>
  <property name="defaulttargetdir" value="./target"></property>
  <property name="final.name" value="test"></property>
  

  <target name="compile" description="o Compile the code" >
    <mkdir dir="${defaulttargetdir}"></mkdir>
    <mkdir dir="${classesdir}"></mkdir>
	<mkdir dir="${logsdir}"></mkdir>

    <javac destdir="${classesdir}" deprecation="true" debug="true" optimize="false" excludes="**/package.html">
      <src>
        <pathelement location="./src"></pathelement>
      </src>
      <classpath>
        <fileset dir="./lib">
          <include name="*.jar"></include>
        </fileset>
      </classpath>
    </javac>
    
  </target>
    
  <target name="jar" description="o Create the jar" depends="compile">
		<copy todir="${classesdir}">
			<fileset dir="./src">
				<include name="**/*.properties" />
			</fileset>
		</copy>    
    <jar jarfile="./lib/${final.name}.jar" excludes="**/package.html" basedir="${classesdir}"></jar>

  </target>
  
  <target name="clean" description="o Clean up the generated directories">
    <delete dir="${classesdir}"></delete>
	<delete file="${lib.dir}/${webapp.name}.jar" />
  </target>



</project>
    



打开dos窗口,进入D:\work\code\test_core\目录,输入ant jar 回车
则test.jar 文件将会被输出到 lib目录下
如果仅仅是编译java源文件,你可以使用 ant compile命令,classes文件将会被输出到 target/classes目录下,如果你在src下有以properties结尾的配置文件,则配置文件将也会被拷贝到 target/classes 目录下,起作用的是:
<copy todir="${classesdir}">
<fileset dir="./src">
<include name="**/*.properties" />
</fileset>
</copy>

web工程:
D:\work\code\test_web\src
D:\work\code\test_web\lib
D:\work\code\test_web\web

build.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project name="testweb" basedir="." default="">
	<property name="webapp.name" value="testweb" />
	<property name="webapp.version" value="1.0" />
	<property name="lib.dir" value="${basedir}/lib" />
	<property name="dist.dir" value="${basedir}/dist" />
	<!-- The target directory for building the unpacked web application -->
	<property name="build.dir" value="${basedir}/build" />
	<property name="classes.dir" value="${basedir}/build/classes" />
	<!-- The target directory for building the packed web application -->
	<property name="webapp.dist" value="${dist.dir}" />
	<!-- The target directory for building the unpacked web application -->
	<property name="webapp.target" value="${build.dir}/web" />
	<property environment="env" />
	<property name="tomcat.home" value="D:/tomcat-5.5.23" />

	<target name="init" description="defines custom tasks">
	</target>
	  <path id="tomcat.classpath">
		<pathelement location="${tomcat.home}/common/lib/servlet-api.jar"/>
	  </path>
	<!-- Check timestamp on files -->
	<target name="prepare" depends="init" description="create target directories">
		<tstamp>
			<format property="copyright.year" pattern="yyyy" />
		</tstamp>
		<echo message="Preparing target directory '${webapp.target}'" />
		<mkdir dir="${lib.dir}" />
		<mkdir dir="${dist.dir}" />
		<mkdir dir="${build.dir}" />
		<mkdir dir="${classes.dir}" />
		<mkdir dir="${webapp.target}" />
		<mkdir dir="${webapp.target}/WEB-INF" />
		<mkdir dir="${webapp.target}/WEB-INF/lib" />
		<mkdir dir="${webapp.dist}" />

	</target>

	<!-- List of variables in .properties files that will be replaced at
         build time -->
	<filterset id="variables.to.replace">
		<filter token="APPNAME" value="${webapp.name}" />
	</filterset>

	<!-- Copy any resource or configuration files -->
	<target name="compile" depends="prepare" description="compile source directory">
		<echo message="compileing ${webapp.name} ..." />
 
		<copy todir="${classes.dir}">
			<fileset dir="${basedir}/src">
				<include name="**/*.properties" />
 
			
			</fileset>
		</copy>
 		
		
		<javac srcdir="${basedir}/src" destdir="${classes.dir}">
			<include name="**/*.java" />
			<classpath>
				<fileset dir="${basedir}/web/WEB-INF/lib">
					<include name="*.jar">
					</include>
				</fileset>
			</classpath>
            <classpath refid="tomcat.classpath"/>
		</javac>
	</target>
	<!-- Copy any resource or configuration files -->
	<target name="jar" depends="compile" description="jar ">
		<echo message="jar ${webapp.name}.jar  ..." />
		<jar jarfile="${lib.dir}/${webapp.name}.jar">
			<fileset dir="${classes.dir}" includes="**" />
		</jar>	
	</target>
	<!-- Simple alias to package-web -->
	<target name="war" depends="jar" description="alias for package-web">
		<echo message="war {webapp.name} ..." />
		<copy todir="${webapp.target}/WEB-INF">
			<fileset dir="${basedir}/web/WEB-INF">
				<include name="*.xml" />
			</fileset>
		</copy>
		<copy todir="${webapp.target}/WEB-INF/lib">
			<fileset dir="${lib.dir}">
				<include name="**/*.jar" />
			</fileset>
			<fileset dir="${basedir}/web/WEB-INF/lib">
				<include name="**/*.jar" />
			</fileset>
		</copy>

		<copy todir="${webapp.target}">
			<fileset dir="${basedir}/web">
				<include name="**/*.html" />
				<include name="**/*.htm" />
				<include name="**/*.vm" />
				<include name="**/*.jsp" />
				<include name="**/*.txt" />
				<include name="**/*.zip" />
				<include name="**/*.gif" />
				<include name="**/*.jpg" />
				<include name="**/*.png" />
				<include name="**/*.js" />
				<include name="**/*.css" />
				<include name="**/*.htc" />
				<include name="**/*.dwt" />
				<include name="**/*.cert" />
				<include name="**/*.jnlp" />
				<include name="**/*.xml" />
				<include name="**/*.tld" />
				<!--include name="**/*.properties" /-->
			</fileset>
		</copy>
		<echo message="Packaging ${webapp.name}'s web archive file ..." />
		<delete file="${webapp.dist}/${webapp.name}.war" />
		<jar jarfile="${webapp.dist}/${webapp.name}.war">
			<fileset dir="${webapp.target}" includes="**" />
		</jar>

	</target>

	<target name="deploy" depends="compile,war" if="tomcat.home" description="unwar into the servlet container's deployment directory">

		<unwar src="${webapp.dist}/${webapp.name}.war" dest="${tomcat.home}/webapps/${webapp.name}" />
	</target>
	<target name="undeploy" if="tomcat.home" description="undeploy war file to servlet container's deployment dir">
		<echo message="Undeploying webapp from Tomcat" />
		<delete file="${tomcat.home}/webapps/${webapp.war}" />
		<delete dir="${tomcat.home}/webapps/${webapp.name}" />
	</target>
	<target name="clean">
		<echo message="cleaning ${build.dir} and ${dist.dir} ... " />
		<delete dir="${build.dir}" />
		<delete dir="${dist.dir}" />
		<delete file="${lib.dir}/${webapp.name}.jar" />
	</target>
</project>

你可能感兴趣的:(apache,xml,Web,ant,dos)