整合ant

在实际工作中,可能会遇到这种情况,一个项目分成多个模块,为了测试,他们自己写了一个build.xml,而你需要将这些模块组合在一起使用,写一个build.xml.这时如果自己写一个整体的build.xml将比较麻烦,可以充分利用他们写好的build.xml,进行组合,本文就是介绍ant的这个功能。

为了测试,我在桌面建一个test文件夹,然后在里面建立src1,src2,src3三个文件夹,分别存放他们的src和build.xml,如:

src1下的:

HelloWorld1:

package test.ant;

public class HelloWorld1 {

	public static void main(String[] args) {
		System.out.println("test HelloWorld1");
	}

}

build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="HelloWorld1" default="run" basedir=".">
<property name ="src" value="src"/>
<property name ="dest" value="classes"/>
<property name ="hello1_jar" value="hello1.jar"/>

<target name ="init">
<mkdir dir="${dest}"/>
</target>

<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}"/>
</target>

<target name="build" depends="compile">
<jar jarfile="${hello1_jar}" basedir="${dest}"/>
</target>

<target name="run" depends="build">
<java classname="test.ant.HelloWorld1" classpath="${hello1_jar}" />
</target>

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

<target name="rerun" depends="clean,run">
<ant target="clean"/>
<ant target="run"/>
</target>
</project>

src2下的:

HelloWorld2:

package test.ant;

public class HelloWorld2 {

	public static void main(String[] args) {
		System.out.println("test HelloWorld2");
	}

}

build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="HelloWorld2" default="run" basedir=".">
<property name ="src" value="src"/>
<property name ="dest" value="classes"/>
<property name ="hello2_jar" value="hello2.jar"/>

<target name ="init">
<mkdir dir="${dest}"/>
</target>

<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}"/>
</target>

<target name="build" depends="compile">
<jar jarfile="${hello2_jar}" basedir="${dest}"/>
</target>

<target name="run" depends="build">
<java classname="test.ant.HelloWorld2" classpath="${hello2_jar}" />
</target>

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

<target name="rerun" depends="clean,run">
<ant target="clean"/>
<ant target="run"/>
</target>
</project>

src3下的:

HelloWorld3:

package test.ant;

public class HelloWorld3 {

	public static void main(String[] args) {
		System.out.println("test HelloWorld3");
	}

}

build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="HelloWorld1" default="run" basedir=".">
<property name ="src" value="src"/>
<property name ="dest" value="classes"/>
<property name ="hello3_jar" value="hello3.jar"/>

<target name ="init">
<mkdir dir="${dest}"/>
</target>

<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}"/>
</target>

<target name="build" depends="compile">
<jar jarfile="${hello3_jar}" basedir="${dest}"/>
</target>

<target name="run" depends="build">
<java classname="test.ant.HelloWorld3" classpath="${hello3_jar}" />
</target>

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

<target name="rerun" depends="clean,run">
<ant target="clean"/>
<ant target="run"/>
</target>
</project>


上面是他们各自准备好的东西,接下来我们需要做一个build.xml来进行整合,将其放在test目录下:

<?xml version="1.0" encoding="UTF-8"?>
<project name="main" default="build" basedir=".">
<property name="bin" value="${basedir}\bin"/>
<property name="src1" value="${basedir}\src1"/>
<property name="src2" value="${basedir}\src2"/>
<property name="src3" value="${basedir}\src3"/>

<target name ="init">
<mkdir dir="${bin}"/>
</target>

<target name="run"> 
<ant dir="${src1}" target="run"/>
<ant dir="${src2}" target="run"/>
<ant dir="${src3}" target="run"/>
</target>

<target name="clean">
<ant dir="${src1}" target="clean"/>
<ant dir="${src2}" target="clean"/>
<ant dir="${src3}" target="clean"/>
</target>

<target name="build" depends="init,run">
<copy todir="${bin}">
<fileset dir="${src1}">
<include name="*.jar"/>
</fileset>
<fileset dir="${src2}">
<include name="*.jar"/>
</fileset>
<fileset dir="${src3}">
<include name="*.jar"/>
</fileset>
</copy>
</target>

<target name ="rebuild" depends="build,clean">
<ant target="clean" />
<ant target="build" />
</target>
</project>


然后在控制台进入test目录,执行ant,就可以看到效果了。

你可能感兴趣的:(java,ant,复制,编译,模块整合)