Ant

Copy命令使用:http://www.blogjava.net/hh-lux/archive/2009/04/07/86635.html

1.单文件拷贝 <copy todir="${basedir}/new" file="${basedir}/old/old1.txt1">或者<copy tofile="${basedir}/new/new1.txt" file="${basedir}/old/old1.txt1">。两者差别应该很容易鉴别。

2.多文件拷贝

<copy todir="${basedir}/new">
           <fileset dir="${basedir}/old">
              <include name="old1.txt" />
              <include name="old2.txt" />
              <exclude name="old8.txt" />
          </fileset>
       </copy>

其中include是包含,exclude是排除的意思。

也可以把fileset写成:<fileset dir="${basedir}/old" includes="old1.txt,old2.txt" />这种形式

多文件拷贝同样支持*,譬如在fileset里面内容为:

<include name="appgen/**"/>
<include name="ibatis/**"/>
<exclude name="**/*.log"/>

3.目录拷贝

<copy todir="${basedir}/new">
           <fileset dir="${basedir}/old">
             <include name="appgen" />
             <include name="appgen/" />
             <include name=appgen/**" />
             <include name="appgen/***" />
           </fileset>
       </copy>

若使用<include/>, 又要分三种情况
          若是“appgen”,则只会拷贝名为appgen的空目录过去,它里面的文件和子目录则不会拷贝。
          若是“appgen/”,或“appgen/**”,则会把整个appgen目录拷贝过去,包括里面的文件和子目录。
          若是“appgen/*”,则只会把该目录和该目录下第一级子目录的所有东西拷贝过去,而不会拷贝第二级和第二级以下的。注:“appgen/*”这儿是 一个*号,*号若大于两个,也跟一个*号是同样效果。比如“appgen/*”和“appgen/****”都只拷贝appgen目录下第一级子目录。

注:使用<exclude/>排除目录时,目录名必须写成“appgen/”或“appgen/**”形式,否则不会生效。
以上是三种拷贝到目录的种类,注意如果计算机中没有todir指定的路径,ant将会自动创建这个路径。

4.复制肯定还要涉及到同名覆盖的问题,ant在copy类的API中说明:Files are only copied if the source file is newer than the destination file,这里的newer是指文件的修改时间,即使你在修改时文件内容没有任何变化,只是导致修改时间变了,ant同样会覆盖同名文件,也就是 说,ant不会检查文件内容。

      对于是复制目录的情况,由于目录没有修改时间,ant还是通过检查目录内文件的修改时间来决定是否覆盖的,若目录内某文件修改时间有变化,则会覆盖这个文件,而不是整个目录。

如果要强行覆盖,<copy/>有个overwrite属性,默认为false,改成true就行了。

 

filter的使用filter分filter和filterset两个,其作用是将某些文件的内容替换为新内容。

一、<filter filtersfile="filter.properties"/>  用外部文件定义filter,外部文件的写法为:
month=12
year=2008 这样这个filter就达到了把@month@和@year@替换为你指定的值的目的了。

二、<filter token="time" value="14时7分"/>,这个会把@time@替换掉。

三、

<copy toDir="${dist.dir}/docs">
  <fileset dir="${build.dir}/docs">
    <include name="**/*.html">
  </fileset>
  <filterset begintoken="%" endtoken="*">
    <filtersfile file="${user.dir}/dist.properties"/>
  </filterset>
</copy>

若filter指定的外部文件还和上面一样,那么会把%month*替换为指定的值。

当然也可以在外面指定filterset,赋个ID(如myFilterSet),然后在里面引用的时候,

<filterset refid="myFilterSet"/>

即可。

 

最后,举一个build.xml的例子,用来打包war,ear文件【一般为EJB工程打包】(步骤分为copy到目标目录,然后运行javac编译,最后war,ear命令进行打包)。

<?xml version="1.0" encoding="UTF-8"?>
<project name="ProjectWindow.web" default="buildear" basedir=".">
	<property name="dist" location="dist" />
	<property name="src" location="src/main" />
	<property name="web.webroot" location="WebRoot" />  
	<property name="web.inf" location="WebRoot/WEB-INF" /> 
	
	<path id="project.class.path">
		<fileset dir="${web.inf}/lib">
			<include name="*.jar" />
		</fileset>
		<pathelement path="${java.class.path}" />
	</path>

	<target name="clean">
		<delete dir="${dist}">
		</delete>
		<delete dir="${web.inf}/classes">
		</delete> 
	</target>
	 
	<target name="copy.config" depends="clean" >
		<mkdir dir="${web.inf}/classes"/>
		<mkdir dir="${web.inf}/classes/schedule"/>
		<copy file="${src}/resources/application.properties" tofile="${web.inf}/classes/application.properties"/>
		<copy file="${src}/resources/applicationContext-action.xml" tofile="${web.inf}/classes/applicationContext-action.xml"/>
		<copy file="${src}/resources/applicationContext-dao.xml" tofile="${web.inf}/classes/applicationContext-dao.xml"/>
		<copy file="${src}/resources/applicationContext-interceptor.xml" tofile="${web.inf}/classes/applicationContext-interceptor.xml"/>
		<copy file="${src}/resources/applicationContext-service.xml" tofile="${web.inf}/classes/applicationContext-service.xml"/>
		<copy file="${src}/resources/applicationContext.xml" tofile="${web.inf}/classes/applicationContext.xml"/>
		<copy file="${src}/resources/applicationContext-SumDataView.xml" tofile="${web.inf}/classes/applicationContext-SumDataView.xml"/>
		<copy file="${src}/resources/applicationContext-SysIndexView.xml" tofile="${web.inf}/classes/applicationContext-SysIndexView.xml"/>
		<copy file="${src}/resources/applicationContext-ProvinceDataView.xml" tofile="${web.inf}/classes/applicationContext-ProvinceDataView.xml"/>
		
		<copy file="${src}/resources/companyList.xml" tofile="${web.inf}/classes/companyList.xml"/>
		<copy file="${src}/resources/configuration.xml" tofile="${web.inf}/classes/configuration.xml"/>
		
		<copy file="${src}/resources/oracle/Fsxtsjwbd.hbm.xml" tofile="${web.inf}/classes/oracle/Fsxtsjwbd.hbm.xml"/>
		<copy file="${src}/resources/oracle/Xtzcywyyl.hbm.xml" tofile="${web.inf}/classes/oracle/Xtzcywyyl.hbm.xml"/>
		<copy file="${src}/resources/oracle/Yhhyd.hbm.xml" tofile="${web.inf}/classes/oracle/Yhhyd.hbm.xml"/>
		
		<copy file="${src}/java/com/telek/yyjk/oracle/ProvinceDataView.hbm.xml" tofile="${web.inf}/classes/com/telek/yyjk/oracle/ProvinceDataView.hbm.xml"/>
		<copy file="${src}/java/com/telek/yyjk/oracle/SumDataView.hbm.xml" tofile="${web.inf}/classes/com/telek/yyjk/oracle/SumDataView.hbm.xml"/>
		<copy file="${src}/java/com/telek/yyjk/oracle/SysIndexView.hbm.xml" tofile="${web.inf}/classes/com/telek/yyjk/oracle/SysIndexView.hbm.xml"/>
		
		<copy file="${src}/resources/schedule/applicationContext-quartz-cron-local.xml" tofile="${web.inf}/classes/schedule/applicationContext-quartz-cron-local.xml"/>
		<copy file="${src}/resources/fwn.properties" tofile="${web.inf}/classes/fwn.properties"/>
		<copy file="${src}/resources/log4j.properties" tofile="${web.inf}/classes/log4j.properties"/>
		<copy file="${src}/resources/msg.properties" tofile="${web.inf}/classes/msg.properties"/>
	</target>
	 
	<target name="compile" description="compile the source " >
		<!-- Compile the java code from ${src} into ${build} -->
		<javac srcdir="${src}" destdir="${web.inf}/classes" source="1.4" target="1.4">
			<classpath refid="project.class.path">
			</classpath>
		</javac>
	</target>

	<target name="buildwar" depends="copy.config,compile" description="generate the distribution">
		<mkdir dir="${dist}" />
		<war destfile="${dist}/ProjectWindow.war" manifest="${web.webroot}/META-INF/MANIFEST.MF" webxml="${web.inf}/web.xml" basedir="${web.webroot}">
		</war>
	</target>
		
	<target name="buildear" depends="buildwar" description="generate the distribution">
		<ear destfile="${dist}/ProjectWindow.ear" appxml="${web.webroot}/SAPMETA-INF/application.xml" 
			manifest="${web.webroot}/SAPMETA-INF/MANIFEST.MF">
			<metainf dir="${web.webroot}/SAPMETA-INF">
                <include name="application-j2ee-engine.xml"/>
                <include name="sda-dd.xml"/>
			 	<include name="SAP_MANIFEST.MF"/>
            </metainf>
			
			<fileset dir="${dist}">
				<include name="ProjectWindow.war" />
			</fileset>
		</ear>
	</target>
</project>

你可能感兴趣的:(ant)