整合Ant

在实际的工作中,可能一个项目有多个模块,每个小组负责一个模块,一个模块有一个build文件。

当你把这些模块组合到一起时,你不必重新写一个build.xml,直接用他们的build.xml,减少工作。

 

如有模块test1,test2

test1目录结构:

test1

-----src

-----------Test1.java

build.xml

build.xml:

<?xml version="1.0" encoding="utf-8"?>
<project name="Test1" default="run" basedir=".">
	<property name="src.dir" value="src"></property>
	<property name="dest.dir" value="dest"></property>
	<property name="classes.dir" value="${dest.dir}/classes"></property>
	<property name="jarfile" value="${dest.dir}/test1.jar"></property>

	<target name="init">
		<mkdir dir="${classes.dir}"></mkdir>
		<echo message="Create dir ${dest.dir}."></echo>
	</target>

	<target name="compile" depends="init">
		<javac srcdir="${src.dir}" destdir="${classes.dir}"></javac>
		<echo>Compile source code.</echo>
	</target>

	<target name="jar" depends="compile">
		<jar jarfile="${jarfile}" basedir="${classes.dir}"  includes="**/*.class"></jar>
		<echo>Create jar file.</echo>
	</target>

	<target name="run" depends="jar">
		<java classpath="${jarfile}" classname="Test1"></java>
		<echo>Run jar file.</echo>
	</target>

	<target name="clean">
		<delete dir="${dest.dir}"></delete>
		<delete file="${jarfile}"></delete>
	</target>
</project>

 

 

test2目录结构:

test2

-----src

-----------Test2.java

build.xml

build.xml:

<?xml version="1.0" encoding="utf-8"?>
<project name="Test1" default="run" basedir=".">
	<property name="src.dir" value="src"></property>
	<property name="dest.dir" value="dest"></property>
	<property name="classes.dir" value="${dest.dir}/classes"></property>
	<property name="jarfile" value="${dest.dir}/test2.jar"></property>

	<target name="init">
		<mkdir dir="${classes.dir}"></mkdir>
		<echo message="Create dir ${dest.dir}."></echo>
	</target>

	<target name="compile" depends="init">
		<javac srcdir="${src.dir}" destdir="${classes.dir}"></javac>
		<echo>Compile source code.</echo>
	</target>

	<target name="jar" depends="compile">
		<jar jarfile="${jarfile}" basedir="${classes.dir}"  includes="**/*.class"></jar>
		<echo>Create jar file.</echo>
	</target>

	<target name="run" depends="jar">
		<java classpath="${jarfile}" classname="Test2"></java>
		<echo>Run jar file.</echo>
	</target>

	<target name="clean">
		<delete dir="${dest.dir}"></delete>
		<delete file="${jarfile}"></delete>
	</target>

</project>

 

 

那你现在要做的就是新建一个目录,如test

新建目录src1,src2,复制test1和test2的src,build.xml到src1和src2。

test

----src1

----------src

---------------------Test1.java

----------build.xml

----src2

---------src

---------------------Test2.java

---------build.xml

build.xml

 

然后在test下新增一个build.xml:

<?xml version="1.0" encoding="utf-8"?>
<project name="Test" default="run" basedir=".">
	<property name="src1" value="src1"/>
	<property name="src2" value="src2"/>
	<property name="dest" value="dest"/>
	
	<target name="init">
		<mkdir dir="${dest}"/>
	</target>
	
	<!--分别调用test1和test2的run任务-->	
	<target name="run" depends="init">
		<echo>Build file test1 start...</echo>
		<ant dir="${src1}" target="run"/>
		<echo>Build file test1 end...</echo>
		
	  <echo>Build file test2 start...</echo>
	  <ant dir="${src2}" target="run"/>		
	  <echo>Build file test2 end...</echo>
	</target>
	
	<target name="clean">
		<ant dir="${src1}" target="clean"/>
		<ant dir="${src2}" target="clean"/>
	</target>
	<!--将run任务执行后在test1和test2的dest目录生成的jar文件拷贝到test的dest目录-->	
	<target name="build" depends="run">
		<copy todir="${dest}">
			<fileset dir="${src1}/${dest}">
				<include name="*.jar"/>
			</fileset>
			<fileset dir="${src2}/${dest}">
				<include name="*.jar"/>
			</fileset>
		</copy>
	</target>
</project>

整合Ant_第1张图片

你可能感兴趣的:(ant)