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

被折叠的 条评论
为什么被折叠?



