Apache Ant vs Make: Actual Java build Tools __java

Source: Internet
Author: User
Tags create directory echo message java open source projects
When it comes to ant in English, people naturally think of ants. When it comes to another word make, people naturally think about building tools. Who would associate these two words with each other? So what's so weird about ant? This article will reveal the answer to you.

what is Ant
Apache Ant is a Java based build tool. In theory, make tools like unix/linux C programmers often use. Ant is fully implemented by Java and has cross-platform benefits compared to make.

  Ant's Named source

As for Ant's address, it's quite interesting. According to the ant original author James Duncan Davidson, Ant is "Another Neat Tool" abbreviation, meaning "another concise tool", meaning is more concise than make, applicable. But people are more receptive to the idea that ant is "ant". As we all know, ants are famous architects of animal world (ants do a extremely good job at building things); the ant is small but strong (ants are very small and can carry a we Ight dozens of the Times their own).

  Ant's History

Speaking of Ant, you have to say another Apache Open source project Tomcat. Tomcat has already gained prominence as a lightweight web container. Initially, Ant is a part of Tomcat, and the only purpose of ant is to build tomcat. The ant original author James Duncan Davidson is also the founder of Tomcat.

Soon, many Java open source projects realized the simplicity of ant's application and, more importantly, made up for makefiles deficiencies. Since Jakarta and the Apache project started using ant, as a build tool, Ant has spread to a variety of projects like a virus.

In January 2000, Ant broke out of Tomcat and became an independent Apache open source project, maintained by an independent CVS module and officially renamed Apache Ant. July 2000, the first version of Apache Ant 1.1 is officially unveiled. As of the time of the author, Apache Ant's latest version is 1.8.2, the release date is December 27, 2010. Click http://ant.apache.org/bindownload.cgi to download the latest version.

  Ant Installation

After a brief introduction to Ant's background, let's take the Windows platform as an example to explain how to install ant.

To successfully install ant, make sure that the Java version is above 1.4 and recommend 1.5.   To ensure that the ant feature is fully available, use the JDK and do not recommend using the JRE. The first option is to select Windows Installer for Apache ant automatic installation, and the default ant installation directory is C:\Program files\winant. and automatically create environment variables ant_home and path, pointing to the ant root directory.   This approach is characterized by simplicity and rapidity. The second way is to install it manually. The specific methods are as follows:

1. Download. As described above, download the ant binary installation package a.zip from Ant Binary Page (http://ant.apache.org/bindownload.cgi). After the decompression directory is as follows:

bin– public binaries, and running scripts

build– files that are temporarily created, such as. class files

dist– target output files, such as. jar files

docs– Document

lib– the jar package that needs to be exported

src– Source Files

2. Specify the Ant_home variable. Open the Control Panel-> system –> advanced –> environment variable. Create user variable ant_home, the value is the ZIP package decompression path. For example:

Variable name: "Ant_home"

Variable value: "C:\Program files\apache Software foundation\apache-ant-1.8.1"


3. Add ant_home to the PATH variable.

Variable name: "PATH"

Variable value: ";%ant_home%\bin"

4. Create user variable java_home, the value is the Java installation directory. For example:

Variable name: "Java_home"

Variable value: "C:\Program files\java\jdk1.6.0_21"

Create user variable Java_home, the value is the Java installation directory. Java_home = C:\Program files\java\jdk1.6.0_02

5. Add Java_home to the PATH variable.

Variable name: "PATH"

Variable value: ";%java_home%\bin"

The second approach is more flexible than the previous one.

Either way, you can use the following methods to ensure that the installation is successful.

• Use javac–version to check whether the Java environment is correct. The return value should be: Javac 1.6.0_21

• Use ant–version to check whether the ant environment is correct. The return value should be: Apache Ant version 1.8.1 compiled on April 30 2010.

As shown in the figure:

If you encounter installation errors, see the Apache Ant official website: http://ant.apache.org/problems.html.

  First Ant Example

Let's say we create a Java engineering HelloWorld that stores the source file. Java in the SRC directory, compiled bytecode. Class is stored in the bin directory, the corresponding jar package is stored in the EXE directory, the structure of the following figure:

The Helloworld.java list is as follows:

public class HelloWorld {

public static void Main (String [] args) {

System.out.println ("Hello World");

}

}

We used the command line and ant two different build methods respectively.

  1. Command line

First, create the SRC directory: MD src

Second, create the bin directory, compile and run: MD bin

Javac-sourcepath src-d bin \ src \ Helloworld.java

JAVA-CP bin HelloWorld

Again, create exe directory, create jar package, include create manifest file, exe directory, jar package. One command can be handled: Echo Main-class:helloworld > monmanifest md exe jar CFM EXE \ Helloworld.jar monmanifest-c bin.

Finally, the operation of the project: Java-jar exe \ Helloworld.jar

  2. Ant

Ant's Build script file is written in the XML language, and we'll refer to Build.xml for short. Build.xml contains a standard root node project, which represents a project. Each build file allows only one project element to be defined. Project defines at least 1 or more target to represent different modules. Target is also a collection of column tasks, and each task is a piece of executable code. The relationship between the three is shown in the figure. For more details, please see the Apache Ant official website: http://ant.apache.org.

We use ant to build the engineering HelloWorld. The Build.xml contains 4 target:clean,compile,jar,run.

   Clean

Clean clears all compiled files and related directories, which are directory bin and EXE. < target name = "Clean" >

< Delete dir = "Bin"/>

< delete dir = "EXE"/>

</target >

  Compile

Compile creates the directory bin, compiles the src directory source file, and puts the generated. class file in the Bin directory. < target name = "Compile" >

< mkdir dir = "Bin"/>

< Javac Srcdir = "src" destdir = "bin"/>

</target >

  Jar

Jar Create directory EXE, package jar. You can easily create manifest files using the manifest element. < target name = "Jar" >

< mkdir dir = "EXE"/>

< jar DestFile = "Exe/helloworld.jar" Basedir = "Bin" >

< manifest >

< attribute name = "Main-class" value = "HelloWorld"/>

</manifest >

</jar >

</target >

  Run

Run runs the jar. < target name = "Run" >

< Java jar = "Exe/helloworld.jar" fork = "true"/>

</target >

It should be noted that the above 4 target execution order is dependent. For example, a jar relies on compile,run to rely on a jar. Ant provides a property depends to describe the dependencies between target. For example, assume that execution D, because D depends on the c,c dependent on the b,b dependent on a, therefore, in order, first executes a, second B, then C, and finally D. Each target can only be executed at most once. < target name = "A"/>

< target name = "B" depends = "A"/>

< target name = "C" depends = "B"/>

< target name = "D" depends = "c,b,a"/>

For our engineering HelloWorld. You can run the following ant commands. Ant Clean

Ant Compile

Ant Jar

Ant Run

Of course, you can simply run Ant run

The Build.xml list is as follows: < project default = "Run" >

< property name = "Src.dir" value = "src"/>

< property name = "Bin.dir" value = "Bin"/>

< property name = "Jar.dir" value = "EXE"/>

< property name = "Main-class" value = "HelloWorld"/>

< target name = "Clean" >

< Delete dir = "${bin.dir}"/>

< Delete dir = "${jar.dir}"/>

< echo message = "Nettoyage termine"/>

</target >

< target name = "Compile" depends = "clean" >

< mkdir dir = "${bin.dir}"/>

< Javac Srcdir = "${src.dir}" Destdir = "${bin.dir}"/>

< echo message = "Compilation Terminee"/>

</target >

< target name = "Jar" depends = "compile" >

< mkdir dir = "${jar.dir}"/>

< jar DestFile = "${jar.dir}/Sdf.jar" Basedir = "${bin.dir}" >

< manifest >

< attribute name = "Main-class" value = "${main-class}"/>

</manifest >

</jar >

< echo message = "Creation du fichier Jar terminee"/>

</target >

< target name = "Run" depends = "jar" >

< Java jar = "${jar.dir}/sdf.jar" fork = "true"/>

</target >

</Project >

Eclipse provides an ant view that makes it easy to view and edit your ant script, as shown in the figure:

  Ant API

The strength of ant is that you can call ant scripts for a variety of file deployments, administrative work, Ant-rich APIs, and even extended ant API programming. Give a few examples:

1. Create a table of contents: Project PRJ = new Project ();

Mkdir Mkdir = new Mkdir ();

Mkdir.setproject (PRJ);

Mkdir.setdir (New File ("src"));

Mkdir.execute ();

2. Make the class file into a jar package project PRJ = new Project ();

Jar jar = new Jar ();

Jar.setproject (PRJ);

Jar.setdestfile (New File ("Helloworld.jar"));

Fileset fileset = new Fileset ();

Fileset.setproject (PRJ);

Fileset.setdir (New File ("bin"));

Fileset.setincludes ("**/*.class");

Jar.addfileset (Fileset);

Jar.execute ();

  Create your own task

Apache ant allows a user to customize a task as follows:

1. Create a class that inherits Org.apache.tools.ant.Task

2. For each attribute, the set method of the standard Java Bean Specification needs to be implemented.

3. If you create a task that requires other subtasks, you need to implement the Org.apache.tools.ant.TaskContainer interface.

4. If the extended task needs to support text, you need to implement the method public void AddText (String).

5. For each nested element, implement create, add, or addconfigured methods.

6. Implement the public void execute () method.

7. Use references to customize tasks in Build.xml.

For example, we write a custom task to print a message in the Java console. The task has only one attribute, called a message.

Source code Mytask.java Import org.apache.tools.ant.BuildException;

Import Org.apache.tools.ant.Task;

public class MyTask extends Task {

Private String msg;

The method executing the task

public void execute () throws Buildexception {

SYSTEM.OUT.PRINTLN (msg);

}

The setter for the ' message ' attribute

public void Setmessage (String msg) {

this.msg = msg;

}

}


corresponding Build.xml < XML version = "1.0"? >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.