With tools such as Java,javac,jar, which are cumbersome and error-prone, ant appears.
Ant's appearance is specifically for packaging to compile Java code, before using a little learning. Ant runs primarily on a configuration file (XML format), typically named Build.xml.
Before using ant, of course, install the Java JDK,JRE First, and the environment variables are all set.
Then enter Ant.bat in the cmd window. If Ant_home is set, no absolute path is required. I am lazy, not set, but can still use.
Enter Ant.bat-version to view the version number.
If the display is correct, the installation is not a problem.
The next major task, in fact, is how to edit build.xml, because the actions that ant makes are performed according to the Build.xml rules. You can download the Help documentation from the Ant website.
The Help documentation is very detailed and is just quick to get started, so it's easy to explain.
There are many nodes in Build.xml, but the following nodes are important, and it is necessary to understand these nodes.
Project,target,property,task,build-in property and so on.
Project is the item, which is the root node of the Build.xml file in ant, and the configuration file should contain at least one project, or an error will occur. Under each project element, you can include multiple target elements.
Project is typically written as:
<project name = "JTest" default= "Jar" basedir= "." >
This means that the project name is Jtest, the default execution Jar command, and the base directory is the current directory.
Default refers to the target that is to be run in the configuration file. See later examples to understand.
Again, the property node, which is similar to defining some constants. The ant itself built some predefined properties, such as the value of ${os.name}, which is the name of the current operating system. If a value is often used, then it can be obtained by customizing it in the property so that the value is used directly with the ${property name}. There are many built-in properties, and you can read more about the Help documentation. This is only a description of the Basedir property, which sets the base path for the project, which is defined in the Poject node.
Property node, there are 2 attributes, value and location, a little bit different, using value can not use location, and vice versa, if the value of this attribute is a path, then, if you use value, you must give the absolute path, But if there is a location, you can just give a relative path (relative to Basedir in project).
Target is the meaning of the goal, which is contained within the project node, and a project node can contain more than one target.
A target internally defines some action commands, that is, what tasks are defined within target. These tasks are called task. What kind of instructions must be written to do any task. These instructions can be found in the help documentation and are listed here in just a few common.
Copy,delete,mkdir,jar,java,javac, looking like a DOS command, does the same as a DOS command. The jar represents packaging, Java means running the class file, and Javac compiling the file.
See an example:
- <target name="Compile" depends="init "
- description="Compile the source" >
- <!--Compile the Java code from ${SRC} to ${build} --
- <javac srcdir="${src}" destdir="${build}"/>
- </target>
This set of XML means that the name of target is compile, which can be customized, depends refers to a dependency on another target, that is, if you want to run the compile target, you must first run the depends target.
Next is the Javac task, through the <javac srcdir= "${src}" destdir= "${build}"/>, which means that the Srcdir command is compiled for the class under the path of the Javac, and then the compiled results are exported to Destdir.
${src},${build} are property values.
Last look at an example.
Assuming that the current directory structure of the original file to be packaged is as follows, put the edited Build.xml file in the directory where you want to compile the package.
- D:\TMPMULU
- │build.xml
- │
- └─src
- └─mytest
- ├─anotherpackage
- │classprinta.java
- │classprintb.java
- │
- └─mytestpkg
- Tj.java
The contents of Build.xml are as follows:
- <? XML version="1.0" ?>
- <project name ="JTest" default="jar" basedir= "." ><!--Run the jar target by default, but the jar target depends on the run target,
- The run target relies on the compile target, and the compile target depends on the clean target, so the order is clean,compile,run,jar-->
- <target name="clean">
- <delete dir="${basedir}/build"/><!--perform delete action, delete path is defined in Dir,
- ${basedir} attribute values are defined in the project node-->
- </target>
- <target name="Compile" depends ="clean">
- <mkdir dir ="${basedir}/build/classes"/><!--mkdir, create a directory, you must run the clean target before running the task-- >
- <javac srcdir ="${basedir}/src" destdir ="${basedir}/build/classes"/><!-- Run Javac, compile, compile the path is Srcdir, after compiling the output directory is destdir-->
- </target>
- <target name="Run" depends ="Compile">
- <java classname ="Mytest.mytestpkg.Tj"><!--execute Java command, that is, run the compiled class, The target must be run after the compile target
- <classpath>
- <pathelement path="${basedir}/build/classes"/><!-- Dos in Java command to set the Classpath parameter, through this node set--
- </classpath>
- </java>
- </target>
- <target name="jar" depends="Run">
- <jar destfile="Tj.jar" basedir="${basedir}/build/classes"><!--run the jar command, The package action must be performed before the run target is completed. -
- <manifest>
- <attribute name="Main-class" value="mytest.mytestpkg.Tj"/><!-- Create a manifest file. The concept of the manifest file can be referenced in the JAR file specification
- Http://docs.oracle.com/javase/1.5.0/docs/guide/jar/jar.html#JAR manifest-->
- </manifest>
- </jar>
- </target >
- </Project>
Run the ant command. Two ways: If the current path is not D:\tmpmulu\, you can specify the location of the Build.xml file through the-f parameter.
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 look for build.xml.
After running, the interface will show the operation of each target, the run target, the output of the program printed content. And under Basedir, a Tj.jar package is generated. The file structure after running is as follows:
- D:\TMPMULU
- │build.xml
- │tj.jar
- │
- ├─build
- │└─classes
- │└─mytest
- │├─anotherpackage
- ││classprinta.class
- ││classprintb.class
- ││
- │└─mytestpkg
- │tj.class
- │
- └─src
- └─mytest
- ├─anotherpackage
- │classprinta.java
- │classprintb.java
- │
- └─mytestpkg
- Tj.java
With some IDE tools, such as Eclipse, you don't have to edit build.xml manually, and the tool will automatically help us generate it.
This article is from "a blog" blog, make sure to keep this source http://cnn237111.blog.51cto.com/2359144/1130502
Basic introduction to using ant packaging tools