Apache Ant should be familiar to every Java programmer: it’s a popular software build tool, written entirely in Java. Ant uses a script that is a regular XML file. Despite its Java focus, web developers also use this tool.
We’ll go through
five simple steps to start using Ant:
- Download, install and check.
- Let's write a simple HelloWorld script.
- Let's understand the principles of operation and the XML format of the build script.
- We learn the minimum required list of tasks.
- We will write a script for a complete cycle of assembly and testing of an educational project.
We need the
Java SE Development Kit (JDK, download at
www.oracle.com/technetwork/java/javase/downloads ), your favorite text editor (for Linux, vi / vim / gedit, for Windows I recommend Notepad ++) and basic skills on the command line. The build scripts and Java examples are tested on Linux (Simply Linux 7.95.0, CentOS Linux 6.8) and on Windows (XP / 7). Using Ant is the same in Linux and Windows.
1. Download, install, check
Linux: install from repository with a command like
sudo apt-get install ant (replace
apt-get with
yum if necessary). Important: we need a version not lower than 1.8. *. In the CentOS 6.8 repository version 1.7.1, therefore it is better to use the
script described in the
previous article .
')
Windows: visit the
ant.apache.org website, go to the Download / Binary Distributions section and download the apache-ant-1.10.1-bin.zip archive (perhaps now there is a more recent version). The contents of the archive are copied to any directory, for example, in “C: \ Program Files \ Apache Ant”. Then we add the path to the bin directory (C: \ Program Files \ Apache Ant \ bin) to the system variable Path.
We check the performance by calling ant on the command line:
$ ant -version
Apache Ant (TM) version 1.10.1 compiled on February 2 2017
If a similar message is received - everything is in order.
2. We write HelloWorld script
<?xml version="1.0"?> <project name="HelloWorld" default="hello"> <target name="hello"> <echo>Hello, World!</echo> </target> </project>
Create the
hello subdirectory in the home directory (in Linux, the
mkdir command does it, in Windows -
md ) and save the file named
build.xml containing the script suggested above. Go to this directory and call ant:
$ mkdir hello
$ cd hello
$ ant
Buildfile: /home/lamp/hello/build.xml
hello:
[echo] Hello, World!
BUILD SUCCESSFULL
Total time: 0 seconds
What happened? Ant found a script file with a default name (build.xml) and executed target with the name
hello , which is also specified by default in the
project tag using the
default attribute (note that we also specified the project name in the
project tag using the
name attribute). We will get the same result if, when calling ant, we specify
hello as a parameter:
$ ant hello
3. Basic principles of work
The build script is a regular XML file. The text opens (and closes) with the
project tag, where you can specify the name of the project and the default goal. Next, it contains the definition of goals (
target ), dependencies (
depends ) and properties (
property ). The simplest scenario should have at least one goal. In the target tag, we describe the call to one or more tasks. You can set a name for the target using the
name attribute (name = "name_of_target"). The specified name becomes a command for our script and you can call the corresponding target like this:
$ ant name_of_target
The target has the ability to specify a dependency using the
depends attribute. Dependencies connect target'y between themselves. For example, there is a target with the name “compile”, and there is with the name “run”, dependent on the “compile”. And if we want to run, the compile will be executed first.
4. The minimum required list of tasks (tasks)
The standard Ant version contains more than 150 tasks (
https://ant.apache.org/manual/tasklist.html ). So far we will need only seven:
- echo - display messages in the console
- mkdir - create directories
- delete - delete files and directories
- javac - compile java code
- java - run class and jar files
- jar - create a jar file
- junit - run tests
5. Script for building and testing Java project
Ant provides complete freedom in building a directory structure. Create a src subdirectory in our hello directory for Java sources:
$ mkdir src
And save the
HelloWorld.java file as follows:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
And then we slightly complicate the text of our script (build.xml):
<?xml version="1.0"?> <project name="HelloWorld" default="run"> <target name="compile"> <mkdir dir="build/classes"/> <javac destdir="build/classes" includeantruntime="false"> <src path="src"/> </javac> </target> <target name="run" depends="compile"> <java classname="HelloWorld" classpath="build/classes"/> </target> <target name="clean"> <delete dir="build"/> </target> </project>
Now the script contains three target (commands):
compile (compiling the .java file (s)),
run (running the .class file),
clean (deleting the folders with the results of the compilation). In this
compile contains two tasks -
mkdir and
javac . Pay attention to the dependency: target
run previously calls
compile . In addition,
run is the default target for the project.
Run the script without parameters and see the result of the Java program: Hello, World!
Directly indicating directory names does not mean good style. Especially if the names in the script are repeated. Modify build.xml using
property (notice how the project name specified in the project came in handy and adding a couple of comments:
<?xml version="1.0"?> <project name="HelloWorld" default="run"> <property name="src" location="src"/> <property name="build" location="build"/> <property name="classes" location="${build}/classes"/> <target name="compile"> <mkdir dir="${classes}"/> <javac srcdir="${src}" destdir="${classes}" includeAntRuntime="false"/> </target> <target name="run" depends="compile"> <java classname="${ant.project.name}" classpath="${classes}"/> </target> <target name="clean"> <delete dir="${build}"/> </target> </project>
Now add the target
package to the script to form the jar file:
<target name="package" depends="compile"> <jar destfile="${build}/${ant.project.name}.jar" basedir="${classes}"> <manifest> <attribute name="Main–Class" value="${ant.project.name}"/> </manifest> </jar> </target>
and make sure everything works:
$ ant package
$ java -jar build / HelloWorld.jar
Hello, World!
Let us turn to testing. Change the project code (so that there is something to test):
public class HelloWorld { public static void main(String[] args) { HelloWorld hello = new HelloWorld(); System.out.println(hello.sayHello()); } String sayHello() { return "Hello, World!"; } }
and add the
TestHello.java file / class to the
src directory with a simple test:
import static org.junit.Assert.assertEquals; import org.junit.Test; public class TestHello { @Test public void testHello() { HelloWorld hello = new HelloWorld(); assertEquals("Hello, World!", hello.sayHello()); } }
Using the information from
https://github.com/junit-team/junit4/wiki/getting-started, download two files,
junit-4.12.jar and
hamcrest-core-1.3.jar and copy them into our
JDK / jre / directory
lib / ext (we use this copy command in CentOS 6.8):
$ sudo cp ~ / Downloads / *. jar /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.121-0.b13.el6_8.i386/jre/lib/ext/
Now you can check how the test works on the command line:
$ java -cp build / classes org.junit.runner.JUnitCore TestHello
JUnit version 4.12
.
Time: 0.281
OK (1 test)
Add another target -
test to our script:
<target name="test" depends="compile"> <junit> <classpath> <pathelement location="${classes}"/> </classpath> <test name="TestHello"/> </junit> </target>
and supplement the line in target
package (jar):
<jar destfile="${build}/${ant.project.name}.jar" basedir="${classes}" excludes="Test*.class">
Now
test has been added to the list of commands of our script (compile, run, package, clean).
In conclusion, we change the code of our project so that the greeting is displayed in a separate graphic window. Then we create a jar file and launch it with a double click of the mouse (you should be configured to execute jar files with a double click).
Third version of java code:
import javax.swing.*; import java.awt.*; public class HelloWorld extends JFrame { public static void main(String[] args) { new HelloWorld(); } HelloWorld() { setTitle(sayHello()); setDefaultCloseOperation(EXIT_ON_CLOSE); setBounds(200, 200, 300, 200); JLabel label = new JLabel(sayHello(), SwingConstants.CENTER); label.setFont(new Font("", Font.BOLD, 24)); add(label); setVisible(true); } String sayHello() { return "Hello, World!"; } }
Slightly supplement the script (in the target
run ) by adding
fork = "true" (start the execution of the class in another virtual machine). Otherwise, run will not work (tested experimentally):
<java classname="${ant.project.name}" classpath="${classes}" fork="true"/>
We execute the command to generate the jar file ($ ant package), open the
~ / hello / build directory in the file explorer, find
HelloWorld.jar there, double-click on it with the mouse and enjoy the contemplation of the graphic window with a greeting.