10分钟使用ant实现java程序的自动化编译

本文介绍了如何使用ANT这一自动化编译工具简化Java项目的编译过程。通过build.xml文件定义项目编译任务,包括子任务和依赖关系,实现编译、打包和清理等功能。示例展示了ANT命令的使用,并提供了官方文档链接以供深入学习。

类似于C中的make, java的自动化编译工具有ant, maven和gradle。

今天我们来学习最古老的ant。

我们在上一篇文章(https://blog.youkuaiyun.com/solinger/article/details/100919167)中,学习了如何快速的打jar包。

实际上,在一个复杂的java项目中,使用java, javac, jar等原始指令去进行编译还是非常痛苦的。这节让我们学习如何使用ant这个这个自动化编译工具去简化这个过程。

ant的原理其实很简单,核心文件为build.xml.

在这个文件里,定义一个project的编译工作,每一个目标编译工作,都可以有许多子任务组成,且任务和任务之间,目标编译工作和目标编译工作直接可以有很多的组合和依赖关系。

则具体实现如下:

<?xml version="1.0" encoding="utf-8"?>
<project name="first_ant" basedir='.' default="help">
	<property name="src" value="src" />
	<property name="classes" value="bin" />
	<property name="output" value="output" />
	<path id="classpath">
		<pathelement path="${classes}" />
	</path>

	<target name="help" description="Show what the ant can help">
		<echo>help: show ant menu </echo>
		<echo>compile: javac to compile java to class </echo>
		<echo>run: java to run the class</echo>
		<echo>build: jar to archive the classes</echo>
		<echo>clean: remove the classes and jars</echo>
	</target>

	<target name="compile" description="javac to compile java to class">
		<delete dir="${classes}" />
		<mkdir  dir="${classes}" />
		<javac srcdir="${src}" destdir="${classes}" debug="true" includeantruntime="false">
			<classpath refid="classpath" />
		</javac>
	</target>

	<target name="build" description="archive the classes" depends="compile">
		<delete dir="${output}" />
		<mkdir  dir="${output}" />
		<jar destfile="${output}/first_ant.jar" basedir="${classes}" includes="**/*.class">
			<manifest>
				<attribute name="Main-Class" value="HelloWorld"/>
				<attribute name="Class-Path" value="${classes}" />
				<attribute name="classpath" value="${classes}"/>
			</manifest>
		</jar>
	</target>

	<target name="run" description="java run the java class" depends="build">
		<java jar="${output}/${ant.project.name}.jar" fork="true"/>
	</target>

	<target name="clean" description="remove the generated files">
		<delete dir="${output}"/>
		<delete dir="${classes}"/>
	</target>
</project>

则我们当前的目录结构如下:

[wlin@wlin first_ant]$ tree
.
├── build.xml
└── src
    └── HelloWorld.java

1 directory, 2 files
[wlin@wlin first_ant]$ 

安装完ant后,则我们可以执行以下操作:

[wlin@wlin first_ant]$ ant help
Buildfile: /home/wlin/first_ant/build.xml

help:
     [echo] help: show ant menu 
     [echo] compile: javac to compile java to class 
     [echo] run: java to run the class
     [echo] build: jar to archive the classes
     [echo] clean: remove the classes and jars

BUILD SUCCESSFUL
Total time: 0 seconds

则进行编译和打包:

[wlin@wlin first_ant]$ ant build
Buildfile: /home/wlin/first_ant/build.xml

compile:
    [mkdir] Created dir: /home/wlin/first_ant/bin
    [javac] Compiling 1 source file to /home/wlin/first_ant/bin

build:
    [mkdir] Created dir: /home/wlin/first_ant/output
      [jar] Building jar: /home/wlin/first_ant/output/first_ant.jar

BUILD SUCCESSFUL
Total time: 0 seconds
[wlin@wlin first_ant]$ tree
.
├── bin
│   └── HelloWorld.class
├── build.xml
├── output
│   └── first_ant.jar
└── src
    └── HelloWorld.java

3 directories, 4 files

我们可以使用run去执行所有操作

[wlin@wlin first_ant]$ ant run
Buildfile: /home/wlin/first_ant/build.xml

compile:
   [delete] Deleting directory /home/wlin/first_ant/bin
    [mkdir] Created dir: /home/wlin/first_ant/bin
    [javac] Compiling 1 source file to /home/wlin/first_ant/bin

build:
   [delete] Deleting directory /home/wlin/first_ant/output
    [mkdir] Created dir: /home/wlin/first_ant/output
      [jar] Building jar: /home/wlin/first_ant/output/first_ant.jar

run:
     [java] hello world!

BUILD SUCCESSFUL
Total time: 0 seconds

最后删除文件

[wlin@wlin first_ant]$ ant clean
Buildfile: /home/wlin/first_ant/build.xml

clean:
   [delete] Deleting directory /home/wlin/first_ant/output
   [delete] Deleting directory /home/wlin/first_ant/bin

BUILD SUCCESSFUL
Total time: 0 seconds

除了本次实战,你也可以参考官方文档去进行实战https://ant.apache.org/manual/index.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值