Using Ant

When you see a project to be built by Ant, you will see a build.xml file. This is the project build file, the one that Ant was missing when you checked that the installation was correct. It can have any other name, and you can specify the name of the file as a command-line option for Ant, but this is the default file name, as Makefile was for make. A build.xml sample looks like the following:

<project name="HelloWorld" default="jar" basedir="."> 
<description>
This is a sample HelloWorld project build file.
</description>
<property name="buildDir" value="build"/>
<property name="srcDir" value="src"/>
<property name="classesDir" value="${buildDir}/classes"/>
<property name="jarDir" value="${buildDir}/jar"/>

<target name="dirs">
<mkdir dir="${classesDir}"/>
<mkdir dir="${jarDir}"/>
</target>

<target name="compile" depends="dirs">
<javac srcdir="${srcDir}" destdir="${classesDir}"/>
</target>

<target name="jar" depends="dirs,compile">
<jar destfile="${jarDir}/HelloWorld.jar" basedir="${classesDir}"/>
</target>
</project>

The top-level XML tag is project. Each build file describes one project, hence the name. There are three possible attributes to the tag, which are as follows:

  • name: This defines the name of the project and is used by some IDEs to display it in the left panel identifying the project
  • default: This names the target to use when no target is defined on the command line starting Ant
  • basedir: This defines the initial directory used for any other directory name calculation in the build file

The build file can contain a description for the project, as well as properties in property tags. These properties can be used as variables in the attributes of the tasks between the ${ and } characters, and play an important role in the build process.

The targets are defined in target XML tags. Each tag should have a name that uniquely identifies the target in the build file and may have a depends tag that specifies one or more other targets that this target depends on. In case there is more than one target, the targets are comma separated in the attribute. The tasks belonging to the targets are executed in the same order as the targets dependency chain requires, in a very similar way as we saw in the case of make.

You can also add a description attribute to a target that is printed by Ant when the command-line option, -projecthelp, is used. This helps the users of the build file to know what targets are there and which does what. Build files tend to grow large with many targets, and when you have ten or more targets, it is hard to remember each and every target.

The sample project with HelloWorld.java is now arranged in the following directories:

  • build.xml in the root folder of the project
  • HelloWorld.java in the src folder of the project
  • The build/ folder does not exist; it will be created during the build process
  • The build/classes and build/jar also do not exist yet, and will be created during the build process

When you start the build for the HelloWorld project the first time, you will see the following output:

$ ant  
Buildfile: /Users/verhasp/Dropbox/java_9-by_Example/sources/ch02/build.xml

dirs:
[mkdir] Created dir: /Users/verhasp/Dropbox/java_9-by_Example/sources/ch02/build/classes
[mkdir] Created dir: /Users/verhasp/Dropbox/java_9-by_Example/sources/ch02/build/jar

compile:
...
[javac] Compiling 1 source file to /Users/verhasp/Dropbox/java_9-by_Example/sources/ch02/build/classes

jar:
[jar] Building jar: /Users/verhasp/Dropbox/java_9-by_Example/sources/ch02/build/jar/HelloWorld.jar

BUILD SUCCESSFUL
Total time: 0 seconds
Some unimportant lines are deleted from the actual output.

Ant realizes that first it has to create the directories, then it has to compile the source code, and finally it can pack the .class files into a .jar file. Now it is up to you to remember the command to execute the HelloWorld application. It was listed already in the first chapter. Note that this time, the JAR file is named HelloWorld.jar, and it is not in the current directory. You can also try to read the online documentation of Ant and create a target run that executes the compiled and packed program.

Ant has a built-in task named java that executes a Java class in almost the same way as you typed the java command in the terminal.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.220.92