Author: Michel Casabianca
This article tells how to use this tool (ANT) with great value to build and deploy Java projects.
Ant is an automated construction, deployment tool for simple or complex Java projects, which is especially useful for companies with distributed development teams or believe in uninterrupted integration through frequent construction. For companies that establish a traditional Java application and those who create web applications using HTML, JSP, and Java Servlets, ANT is very worthless. Whether your Java developers use what operating system, integrated development environments or build environments, ANT can set your project together for those important builds. Ant also automates and synchronizes document deployment, which typically happens in the software development process, no formal documentation and documentation comparison.
When building and deploying a Java app, Ant processes a lot of useful tasks. The most basic tasks include adding and removing directories, using FTP copying and downloading files, creating JAR and ZIP files, and creates a document. Advanced features include checking source code, executing SQL query or script with source code control systems, performs SQL query or scripts, converting XML files into people-can-identify HTML, and generating STUB (stub) files for remote methods.
What is the difference between Ant and Make (very famous build tools, many C language developers use it)? Ant is created for Java with its own, unique example with portability. Make relies on fixed operating system commands (so a Make file running under Microsoft Windows is useless for developers using UNIX), using the pure Java project built by Ant is portable, because Ant itself is used Java is written, and Ant BulidFiles uses XML syntax.
This article will show you a typical ANT file, which uses a lot of ANT basic tasks.
A typical ANT project
Ant uses XML, called BulidFile tools to carry out its work. Let us consider a source file in the src directory, class library (including JAR files) in the lib directory, the typical Java project in the DOC / API directory. We can build this project with the following Ant Buildfile.
XML Version = "1.0"?>
provject>
Perhaps the syntax here seems to be very lengthy (you can write a short multi-buildfile using make, but the advantage of Ant Buildfile is its readability.
The content of BuildFile is included in the element. Engineering generally has a set of engineering properties with some districts - consists of some Ant tasks such as compilation source code, generated directory, and generating JAR files.
Engineering and attributes. At the element, you can first find the property. These attribute elements are similar to variables. You can usually use attribute values anywhere in the project, which is defined in the
When you use syntax -dproperty = value in the command line to call Ant, you may define or rewrite the properties. For example, if you want to set the value of the SRC to Source-Directory, you can type ant ddsrc = sourc_directory in the command line. In addition, the attribute is constant, so once it is defined, the value cannot be modified. Goals and tasks. The target element defines a set of instructions to implement a certain type. They can be compared to a program function composed of many tasks (compared to programming instructions). For example, in the above buildfile example, the target named JAR (element with the attribute Name = JAR) contains a single task to generate JAR files from class files. This example is just the same destination name and task name, but this is not required - the target element name is selected by the creator, but the task name is fixed, and the target name that defines the target name defined in the ANT tag. match. This special goal (our goal) depends on the BIN target (for example, Depends = bin), which means that if the target JAR is called, then it will first call the BIN target (if the BIN target has not been executed). When calling Ant, you can pass a target to run on the command line. For example, by calling Ant Jar by the command line input command, Ant can run the target JAR. If you do not specify a goal and call ANT, the target specified in the default properties of the project will be run (in this case, it is "all" target). You can also call Ant with multiple targets by separating your goals with spaces.
Goal and its tasks
As mentioned earlier, Ant BulidFile is a collection of destination projects to build or deploy a given phase. Here I will describe the goals used in our example buildfile and detail some of the common problems that will encounter when using these goals and their related tasks. You can quickly customize the BulidFile shown here according to your needs. The following example describes these steps to complete these work by using our Ant BulidFile:
1. Compile Java Source File 2. Create a JAR file 3. Run the program 4. Generate an API Document 5. Clear the generated class (class) file
Compilation Java source file
The use of this goal may be the most widely used. Usually implemented by the following code:
This goal includes a single Javac task, this task is to compile the Java source file. The properties used in this example above are very simple: srcdir is a directory where the source file is stored. Destdir is a directory where the generated class file is stored. The last two attributes are used for Javac compilation options. Note that the Optimize property (not used here) is not used, so the latest Java compiler does not place the property in it. When the class library needs to be compiled, you should add a ClassPath property to list the JAR files or directories. One ANT path can be written under UNIX or Windows system, where a colon or a semicolon is used as a sliding mark of the path, using a slope or a reverse line as a separate marker. In this way, it is defined as lib / foo.jar: lib / bar.jar's classPath can be used directly in Unix without modification, and it will be automatically converted to lib / ba .jar under Windows. This task is trying to use the compiler that is provided with JDK. The compiler is a Java program with its own class, and its own class exists in the Tools.jar file (this file is located in the lib directory). In this way, the Tools.jar file must be specified in the ClassPath. In many operating systems, including Solaris and Linux, this is not a problem. When using the Javac task, if Ant cannot use this standard compiler, it will protest. In order to solve this problem, you can make a link to jRE / lib / extra / tools.jar simply simply.
2. Create a JAR file
This task generates a JAR file from the Java class (and other resources such as a picture or local file). In our example buildfile, through the following code:
The properties of the JAR element are very obvious. When you want to include other files in the document library, you can place a Fileset element in the task. As they implied as their name, those elements (not the mission itself) define a series of files. Let us imagine that if you want to introduce some image files in the PNG (Portable Network Graphics) format in the IMG directory, you should write the following code:
The DIR property indicates the basic directory of the file; and includees contains an expression that defines some files to include in the fileset. Those expressions have a syntax that is very close to the shell command, "*" characters match zero or multiple characters and "?" Only matches a character. You can use expression "**" to replace any number of directories in the directory tree (including zero). For example, if the image to be included in the above example is in any of the IMG, the fileset:
You can also use the Excludes property to exclude files from FileSet. For example, to include all other files in the IMG directory, you can write FileSet as follows:
By default, some files are excluded by all FileSet, which is the backup file generated by the editor (such as the ** / * ~ file under UNIX) and version control directory (for example ** / cvs / * ). If you want to tell Ant You don't want to exclude those files, then you need to add attribute defaultExcludes = "false" in the Fileset element.
Ant can also handle WAR files for packaging web applications and EAR files for more complex applications, which often combine JSP, Servlets, and EJB. Ant has a special task to generate a WAR and EAR files. Those tasks are extensions of JAR tasks to define new nested elements and properties. The WAR task is defined, and and and other nested elements are defined in the element of special FileSet. Includes files in those FileSets will end in the web-inf / lib, web-inf / class, and web-inflight of the document library. At the same time, it also defines the WebXML attribute for the deployment descriptor of the document (web-inf / web.xml).
EAR Task Use an element to place a file into the document's meta-inflica, use the AppXML property to deploy the descriptor (META-INF / Application.xml).
3. Run the program
To run Java applications, you need to use Java tasks, as shown in the following code:
ClassName defines the name of the class including the MAIN () method running the package, ClassPath contains classpaths running it. You can use nested ARG elements to pass command line parameters, or define system properties by nested SysProperty elements. These two actions will be implemented very fast, because by default, the program runs in a virtual machine running Ant (you can use the fork property to get Ant to start a new virtual machine).
In the element, the class path is specified by using the ClassPath property. Nesting with a child element is also possible. For example, to include the following code, directory class, and all JAR files in the lib directory, you can write the following code:
If you want to reuse a path, we can define it and their IDs in the element and reference them in elements. For example, if we want to reuse the path defined above, you can write code as follows:
4. Generate an API document
The goal here is to generate an API document with Javadoc. Generally, this is a simple goal consisting of a single Javadoc mission. The code is as follows:
SourcePath and Destdi are obvious properties. The PackageNames property is a list of packets separated by commas, providing documents. Although Java's IMPORT statement is not recursive, the packagenames attribute is recursive. This means statement packagenames = "test. *" Can provide documentation for TEST packets, test.foo packages, and test.foo.bar packages, but will not provide documentation for the foo or foo.test package.
You can use WindowTitle, DOCTILE, Header, Footer, and Bottom to define the title of the HTML code, the title of the document, and the header, footer, and bottom line of the generated page. Note: You should use the corresponding XML entity ( 5. Clear the generated class file This goal clears the generated class file. For example, to clear the class files (and other resources) in the TMP subdirectory, you can write the following code: Clear class files are always a good idea because it can avoid errors in future compilation. Suppose you define a constant foo in class A and use it in class B. When you compile these Java source files, the value of foo is embedded in the class B class file. If you modify the value of the foo and recompile (no delete class file), the Javac task will not be compiled class B because its source file is older than the corresponding class, so the old value will remain unchanged. This issue cannot be solved even with Javac using the depend attribute, because this option of the Java compiler is an error. JIKES's relevance check is better, but you should rebuild all kinds of files, so that it is the fastest way. Similar problems are encountered when you use a style table in HTML. The Style task does not check the date to use the style sheet. In this case, if you do the files, this task will not generate the target file (usually the HTML file). You can enforce files by using the Force property, but this is usually extremely efficient. In this case, the generated HTML file (in the DOC property directory) is deleted by the following Clean target: To resolve these correlation issues, when you generate a new version of file, you should run the Clean target from time to time. Next: In the second part, we will discuss some more advanced Ant tasks, such as checking files from the source control system, manipulating databases with tag, running Unit tests with JUnit, deploying projects for testing or deploying production servers. Michel Casabianca Casa@sweetohm.net ) Software engineer in in-fusio. In-Fusio is a French company that provides game service for mobile users. At the same time, Michel Casabianca is also a book of "XML Pocket Reference" (O'Reilly publishing, 2001). Download and install Ant Ant is a pure Java tool, so you want to run it, first need to install a Java virtual machine (JVM). You may have installed a JVM, but if you haven't, you can http://java.sun.com/j2se/1.4/download.html Download one for free above. Then http://www.apache.org/dist/jakarta/jakarta-ant/release/v1.4.1/bin/ Download the binary version of Ant. Put the ANT compressed document unzip or Untar (depending on your system platform) to your selected installation directory (under Windows is usually c: / Ant, UNIX is usually / usr / local / Ant). After the software installation is complete, you must indicate that your system will find these two directorys to launch those applications. Do this by placing the bin directory of these two tools to your PATH environment variable. In addition, you should define java_home and Ant_home environment variables to let those tools understand their location. If you use the system is Windows, then you need to add the following number of words in the autoexec.bat file: Set java_home = And is the installation directory of these tools. Restart the machine to complete the installation. If the system reports pass too much parameter to the set command, it may be that space is included in the PATH. To solve this problem, define the path in a single row, and use a double quotes. If your system is UNIX and is using the Bash housing, add the following number of lines in ~ / .bash_profile file: Java_home = Users using other housings should overwrite this script and edit the appropriate profile. In order to update the system environment, enter. ~ / .Bash_profile. Enter the Java and Ant commands for the Terminal window for testing. These commands will be found and run. ANT works like Make. Enter the directory where you want to run the buildfile (its default name is build.xml), then enter ANT. To run the buildfile or name in another directory is not buildfile, you need to use the -buildfile parameter. Ant can also find the required buildfile by using the -Find parameter in the file system, so Ant can be started from anywhere in the project. To display help about the command line Ant parameter, enter ANT -HELP.