整合ant

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

在实际工作中,可能会遇到这种情况,一个项目分成多个模块,为了测试,他们自己写了一个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,就可以看到效果了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值