Ant is an open-source Apache project. It is widely used in Java project development and has powerful functions! Therefore, we need to familiarize ourselves with this Java-based tool.
For more information about ant, see www.apache.org.
First, install ant. download it from apache.org and install it on the machine. It is best to set related environment variables, although many systems can automatically identify them. You should set the environment variables ant_home, java_home, and path. For example, if your ant is installed in C:/ant1.6, your Java is installed in C:/j2sdk1.4.2. then we can perform the following operations to set the environment variable (WINXP ):
Set ant_home = C:/ant1.6
Set java_home = C:/j2sdk1.4.2
Set Path = % PATH %; % PATH %/bin
After ant is set, execute ant-version in the command to check whether the ant version information can be output to confirm the installation is successful.
Because many development tools have built-in ant, I directly use eclipse as an example to describe how to use ant to develop Java projects. The project here is very simple. Create a project named testant, then write the following two classes in SRC:
Package com. North;
/**
* @ Author p2800
*
* Todo to change the template for this generated type comment go
* Window-preferences-Java-code style-code templates
*/
Public class testant
{
Public static void main (string [] ARGs)
{
New mywork (). Print ();
}
}
Package com. North;
/*
* Created on 2004-7-23
*
* Todo to change the template for this generated file go
* Window-preferences-Java-code style-code templates
*/
/**
* @ Author p2800
*
* Todo to change the template for this generated type comment go
* Window-preferences-Java-code style-code templates
*/
Public class mywork
{
Public void print ()
{
System. Out. println (system. getenv ("classpath "));
}
}
I am not going to introduce too many ant users. You can get started quickly by checking the user manual. Instead, the content of build. XML is provided directly and the relevant content is described as necessary. You create a new file build. XML in project testant, which ant searches for by default. If you use other file names, you should use ant-buildfile mybuildfile. xml
<Project default = "jar" basedir = "." name = "myproject">
<Description> This is my test for ant tool </description>
<Property name = "src" location = "src"> </property>
<Property name = "Dist" location = "Dist"> </property>
<Property name = "classes" location = "classes"/>
<Target name = "init">
<Tstamp> </tstamp>
<Mkdir dir = "$ {Dist}"/>
<Mkdir dir = "$ {classes}"/>
</Target>
<Target name = "compile" depends = "init">
<Javac srcdir = "$ {SRC}" destdir = "$ {classes}"/>
</Target>
<Target name = "jar" depends = "compile">
<Jar destfile = "$ {Dist}/myjar-$ {dstamp}. Jar" basedir = "$ {classes}"> </jar>
</Target>
</Project>
This build. XML is not complex. Its purpose is to compile the source code in the project and package it into the DIST directory. In build. XML, there are mainly the following elements: Project target task property. Here, property allows you to define some attribute values and use them in future targets or tasks. A default target must be specified for each project. Each target is a set of tasks used to complete a specific task, which is generally more complex than a single task. Depends, a parameter of target, describes the compilation sequence. For example, to execute jar files, compile must be executed first, and init must be executed before compile is executed. Therefore, the order is INIT-> compile-> jar. Task is the most important. There are many built-in tasks in ant. You can check the list and Think ant is really powerful. For details about which task you want to use, refer to its documentation.
Right-click build. xml and choose run> ant build. You will see
Buildfile: C:/Eclipse/workspace/testant/build. xml
Init:
[Mkdir] created dir: C:/Eclipse/workspace/testant/Dist
[Mkdir] created dir: C:/Eclipse/workspace/testant/classes
Compile:
[Javac] compiling 2 source files to C:/Eclipse/workspace/testant/classes
[Javac] Note: C:/Eclipse/workspace/testant/src/COM/North/mywork. Java uses or overrides a deprecated API.
[Javac] Note: recompile with-deprecation for details.
Jar:
[Jar] building jar: C:/Eclipse/workspace/testant/Dist/myjar-20040723.jar
Build successful
Total time: 2 seconds
This indicates that the compilation is successful, but a prompt is displayed that a deprecated API is used in the program. This is left to the reader to check the manual to see which API is displayed?