Use Ant packaging Tool

Source: Internet
Author: User

Since java, javac, jar and other tools are used for compilation and packaging, which is cumbersome, inefficient, and error-prone, Ant has emerged.

Ant is designed to package and compile java code. You have to learn a little before using it. Ant runs mainly on the configuration file (XML format), which is usually named build. xml.

Install Java JDK and JRE before using ant, and set the environment variables.

Enter ant. bat in the cmd window. If ant_home is set, no absolute path is required. I am lazy and have no choice, but I can still use it.

Enter ant. bat-version to view the version number.

650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clipboard "border =" 0 "alt =" clipboard "height =" 52 "src =" http://img1.51cto.com/attachment/201302/1/2359144_1359679047Khnf.png "/>

If it is correctly displayed, the installation is correct.

The next major task is actually how to edit build. xml, because ant's actions are executed according to the build. xml rules. You can download help documents from the ant official website.

The help documentation is very detailed. Here is just a quick start, so I will explain it briefly.

There are many nodes in build. xml, but the following nodes are important. It is necessary to understand these nodes.

Project, target, property, task, build-in property, etc.

Project indicates a project. It is the root node of the build. xml file in Ant. The configuration file should contain at least one project. Otherwise, an error occurs. Each project element can contain multiple target elements.

The project is usually written as follows:

<Project name = "jTest" default = "jar" basedir = ".">

The project name is jTest. By default, the jar command is executed. The reference directory is the current directory.

Default indicates the default target to be run in the configuration file. I will see the example later.

Let's talk about the property node, which is similar to defining some constants. Ant has built-in predefined attributes. For example, the value of $ {OS. name} is the name of the current operating system. If a value is frequently used, you can customize it in the attribute. To use this value, you can directly use $ {attribute name} to obtain it. There are also many built-in attributes. For more information, see the help documentation. Only the basedir attribute is described here. This attribute sets the baseline path of the project, which is defined in the poject node.

The property node has two features: value and location, which are slightly different. location cannot be used if value is used, and vice versa. If the value of this attribute is a path, if value is used, an absolute path must be provided, but if location exists, the relative path can only be given relative to basedir in the project ).

Target indicates the target, which is contained in the project node. A project node can contain multiple targets.

A target internally defines some action commands, that is, the target defines what tasks to do. These tasks are called tasks. What commands must be written for the task. These commands can be found in the help document. here only a few common commands are listed.

Copy, delete, mkdir, Jar, Java, Javac. The name is similar to the DOS command, and the function is indeed similar to that of the DOS command. Jar indicates packaging, java indicates running class files, and javac indicates compiling files.

Let's look at an example:

 
 
  1. <target name="compile" depends="init" 
  2.       description="compile the source " > 
  3.   <!-- Compile the java code from ${src} into ${build} --> 
  4.   <javac srcdir="${src}" destdir="${build}"/> 
  5. </target> 

This set of xml indicates that the target name is compile, which can be customized. depends indicates that it depends on another target. That is to say, if you want to run the compile target, you must first run the depends target.

The next step is the javac task, which uses <javac srcdir = "$ {src}" destdir = "$ {build}"/> to compile the javac command for classes in the srcdir path, then the compiled result is output to destdir.

$ {Src} and $ {build} Are attribute values.

Finally, let's look at an example.

Assume that the directory structure of the original file to be packaged is as follows, and put the edited build. xml file into the directory to be compiled and packaged.

 
 
  1. D:\TMPMULU 
  2. │  build.xml 
  3. │  
  4. └─src 
  5.     └─mytest 
  6.         ├─AnotherPackage 
  7.         │      ClassPrintA.java 
  8.         │      ClassPrintB.java 
  9.         │      
  10.         └─mytestpkg 
  11.                 Tj.java 

The content of build. xml is as follows:

 
 
  1. <? Xml version = "1.0"?>
  2. <Project name = "jTest" default = "jar" basedir = "."> <! -- The jar target is run by default, but the jar target depends on the run target,
  3. The run target depends on the compile target, and the compile target depends on the clean target. Therefore, the running sequence is clean, compile, run, jar -->
  4. <Target name = "clean">
  5. <Delete dir = "$ {basedir}/build"/> <! -- Execute the delete action. The delete path is defined in dir,
  6. The $ {basedir} property value is defined in the project node -->
  7. </Target>
  8. <Target name = "compile" depends = "clean">
  9. <Mkdir dir = "$ {basedir}/build/classes"/> <! -- Mkdir: create a directory. Run the clean target first before running the task. -->
  10. <Javac srcdir = "$ {basedir}/src" destdir = "$ {basedir}/build/classes"/> <! -- Run javac and execute the compilation. The compiling path is srcdir. After compilation, the output directory is destdir -->
  11. </Target>
  12. <Target name = "run" depends = "compile">
  13. <Java classname = "mytest. mytestpkg. Tj"> <! -- Execute the java command, that is, the compiled class, which must be run after the compile target -->
  14. <Classpath>
  15. <Pathelement path = "$ {basedir}/build/classes"/> <! -- To set the classpath parameter for java commands in dos, use this node to set -->
  16. </Classpath>
  17. </Java>
  18. </Target>
  19. <Target name = "jar" depends = "run">
  20. <Jar destfile = "Tj. jar" basedir = "$ {basedir}/build/classes"> <! -- Run the jar command to execute the packaging action. The package must be completed at the run target. -->
  21. <Manifest>
  22. <Attribute name = "Main-class" value = "mytest. mytestpkg. Tj"/> <! -- Create a manifest file. For the concepts of manifest files, refer to the JAR file specifications.
  23. Http://docs.oracle.com/javase/1.5.0/docs/guide/jar/jar.html#JAR Manifest -->
  24. </Manifest>
  25. </Jar>
  26. </Target>
  27. </Project>

Run the ant command. Two Methods: if the current path is not D: \ tmpmulu \, you can use the-f parameter to specify the location of the build. xml file.

C: \ Users \ Administrator> C: \ ant \ bin \ ant. bat-f D: \ tmpmulu \ build. xml

If the current path is D: \ tmpmulu \, run ant. bat directly. ant will automatically search for build. xml.

650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" clipboard [1] "border =" 0 "alt =" clipboard [1] "height =" 404 "src =" http://www.bkjia.com/uploads/allimg/131228/1149303T4-1.png "/>

After running, the interface displays the running status of each target. In the run target, the content printed by the program is output. In basedir, a Tj. jar package is generated. The file structure after running is as follows:

 
 
  1. D:\TMPMULU 
  2. │  build.xml 
  3. │  Tj.jar 
  4. │  
  5. ├─build 
  6. │  └─classes 
  7. │      └─mytest 
  8. │          ├─AnotherPackage 
  9. │          │      ClassPrintA.class 
  10. │          │      ClassPrintB.class 
  11. │          │      
  12. │          └─mytestpkg 
  13. │                  Tj.class 
  14. │                  
  15. └─src 
  16.     └─mytest 
  17.         ├─AnotherPackage 
  18.         │      ClassPrintA.java 
  19.         │      ClassPrintB.java 
  20.         │      
  21.         └─mytestpkg 
  22.                 Tj.java 

With some IDE tools, such as eclipse, you do not have to manually edit build. xml, which is automatically generated by the tool.

This article is from the "one blog" blog, please be sure to keep this source http://cnn237111.blog.51cto.com/2359144/1130502

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.