In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "what is Apache Ant", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn this article "what is Apache Ant?"
What is Ant?
Apache Ant is a Java-based build (Build) tool. In theory, it is similar to the Make tools that Unix/Linux C programmers often use. Compared with Make, Ant is implemented entirely by Java and has the advantage of cross-platform.
The source of the naming of Ant
As for the name Ant, it is quite interesting. According to James Duncan Davidson, the original author of Ant, Ant is the abbreviation of "Another Neat Tool", which means "another concise tool", which means more concise and applicable than Make. However, people are more willing to accept the understanding that Ant means "ant". As we all know, ants are famous architects in the animal world (ants do an extremely good job at building things); although they are small, they are extremely strong (ants are very small and can carry a weight dozens of times their own).
History of Ant
Speaking of Ant, we have to say that another Apache open source project Tomcat. Tomcat has long gained a reputation as a lightweight Web container. Initially, Ant is actually part of Tomcat, and the sole purpose of Ant is build Tomcat. James Duncan Davidson, the original author of Ant, is also the founder of Tomcat.
Soon, many Java open source projects realized the simplicity and applicability of Ant and, more importantly, to make up for the shortcomings of Makefiles. Since Ant was adopted by Jakarta and Apache projects, Ant has spread like a virus to a variety of projects as a build tool.
In January 2000, Ant broke away from Tomcat and became an independent Apache open source project, maintained by independent CVS modules and officially renamed Apache Ant. In July 2000, version 1.1 of Apache Ant was officially unveiled. As of press time, the * * version of Apache Ant is 1.8.2, and the release date is December 27th, 2010. Click http://ant.apache.org/bindownload.cgi to download the * * version.
Installation of Ant
After a brief introduction to the relevant background of Ant, we take the Windows platform as an example to show how to install Ant.
For the smooth installation of Ant, make sure that the Java version is above 1.4, which is recommended. To ensure that all Ant features are available, use JDK, not JRE.
* choose Windows installer for Apache Ant to install automatically. The default Ant installation directory is C:\ Program Files\ WinAnt. And automatically create the environment variables ANT_HOME and PATH, pointing to the Ant root directory. This method is characterized by simplicity and rapidity.
The second way is to install it manually. The specific methods are as follows:
1. download. As mentioned earlier, download the Ant binary installation package a.zip from ANT Binary Page (http://ant.apache.org/bindownload.cgi)). After decompression, the directory is as follows:
Bin-common binaries and running scripts
Build-temporarily created files, such as .class files
Dist-Target output file, such as .jar file
Docs-document
Lib-jar package to be exported
Src-Source Fil
two。 Specify the ANT_HOME variable. Open Control Panel-> system-> Advanced-> Environment variables. Create a user variable ANT_HOME with the value of 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 the user variable JAVA_HOME with the value of the Java installation directory. For example:
Variable name: "JAVA_HOME"
Variable value: "C:\ Program Files\ Java\ jdk1.6.0_21"
Create the user variable JAVA_HOME with the value of 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"
Compared with the former, the second approach is more flexible.
Either way, you can use the following methods to ensure a successful installation
◆ uses javac-version to check that the Java environment is correct. The return value should be: javac 1.6.0q21
◆ uses ant-version to check that 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, please refer to the official Apache Ant website: http://ant.apache.org/problems.html.
* examples of Ant
Suppose we create a Java project HelloWorld, store the source file .java in the src directory, the compiled bytecode .class in the bin directory, and the corresponding jar package in the exe directory, as shown below:
The HelloWorld.java list is as follows:
Public class HelloWorld {public static void main (String [] args) {System.out.println ("HelloWorld");}}
We adopt two different build methods: command line and Ant.
1. Command Lin
First, create the src directory:
Md src
Second, create the bin directory, compile and run:
Md binjavac-sourcepath src-d bin\ src\ HelloWorld.javajava-cp bin HelloWorld
Third, create the exe directory, create the jar package, including the creation of manifest files, exe directory, jar packaging. It can be done with a single command:
Echo Main-Class: HelloWorld > MonManifest md exe jar cfm exe\ HelloWorld.jar MonManifest-C bin.
* *, run the project:
Java-jar exe\ HelloWorld.jar
2. Ant
Ant's build script file, build file, is written in XML, which we call build.xml for short. Build.xml contains a standard root node Project, which represents a project. Only one Project element can be defined per build file. Project defines at least one or more Target, representing different modules. Target is a collection of columns of Task, and each Task is a piece of executable code. The relationship between the three is shown in the picture. For more details, see the official Apache Ant website: http://ant.apache.org.
We use Ant to build the project HelloWorld. Build.xml contains 4 target:clean,compile,jar,run.
◆ clean
Clean clears all compiled files and related directories, in this case the directories bin and exe.
< target name = " clean " > < delete dir = " bin " / > < delete dir = " exe " / > < /target >◆ compile
Compile creates the directory bin, compiles the src directory source files, and places the generated .class files in the bin directory.
< target name = " compile " > < mkdir dir = " bin " / > < javac srcdir = " src " destdir = " bin " / > < /target >◆ jar
Jar creates a directory exe and packages jar. You can easily create a manifest file 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 jar.
< target name = " run " > < java jar = " exe/HelloWorld.jar " fork = " true " / > < /target >It should be pointed out that there is a dependency on the execution order of the above four target. For example, jar depends on compile,run and depends on jar. Ant provides the attribute depends to describe the dependencies between target. For example, suppose you execute D, because D depends on C, C, B, B, and A, so in order, execute A first, then B, and then C < D >. Each Target can only be executed once at most.
< target name = " A " / > < target name = " B " depends = " A " / > < target name = " C " depends = " B " / > < target name = " D " depends = " C,B,A " / >For our project HelloWorld. You can run the following Ant command.
Ant cleanant compileant jarant 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 Ant view, which makes it easy to view and edit Ant scripts, as shown in the figure:
Ant API
The power of Ant is that you can not only call Ant scripts for various file deployment and management tasks, but also call Ant-rich API, or even extend Ant API programming. To give a few examples:
1. Create a directory:
Project prj=new Project (); Mkdir mkdir=new Mkdir (); mkdir.setProject (prj); mkdir.setDir (new File ("src")); mkdir.execute ()
two。 Package class files into jar packages
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 users to customize Task by following the steps below:
1. Create a class that inherits org.apache.tools.ant.Task
two。 For each Attribute, you need to implement the set method of the standard Java bean specification.
3. If you create a Task that requires other child Task, 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 the create, add or addConfigured methods.
6. Implement the public void execute () method.
7. Customize the Task using references in build.xml.
For example, let's write a custom Task to print a message in the Java console. The Task has only one attribute, called 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
Including the compilation of MyTask, the complete Ant script is:
List of Ant important tags
Label
Each build file corresponds to a project. Tag is the root tag of the build file. It can have multiple inherent attributes, as follows:
(1) default indicates the default run target, and this attribute is required.
(2) basedir represents the base directory of the project.
(3) name represents the project name.
(4) description represents the description of the project.
Label
There can be one or more target tags under an item tag. A target tag can rely on other target tags. A target can only be executed once, even if there are multiple target dependencies on it. If there is no if or unless attribute, target will always be executed. The attributes of the target tag are as follows. :
(1) .name indicates that this attribute is required.
(2) .depends indicates the dependent goal.
(3) if means that it is executed only when the property is set.
(4) unless means that it is executed when the property is not set.
(5) description represents the description of the project.
Label
This tag is used to create a directory with an attribute dir that specifies the name of the directory created, with the following code:
Label
This tag is used to generate a JAR file with the following attributes.
(1) destfile represents the name of the JAR file.
(2) basedir represents the name of the file being archived.
(3) includes indicates the file mode of not archiving.
(4) exchudes indicates the file mode that is excluded.
Label
This tag is used to compile an java file or group of files with the following attributes.
(1) .srcdir represents the directory of the source program.
(2) .destdir represents the output directory of the class file.
(3) .include represents the schema of the compiled file.
(4) .exclusions represents the mode of the excluded file.
(5) .classpath represents the classpath used.
(6) .debug represents the debugging information contained.
(7) .optimization indicates whether to use optimization.
(8). Verbose means to provide detailed output information
(9) .fileonerror means to stop automatically when an error is encountered.
Label
This tag is used to execute the compiled .class file with the following attributes.
(1) .classname represents the name of the class that will be executed.
(2) .jar represents the name of the JAR file that contains the class.
(3) classpath represents the classpath used.
(4) .fork means running the class in a new virtual machine.
(5) .failonerror means to stop automatically when an error occurs.
(6). Output represents the output file.
(7) .append means appending or overwriting the default file.
Label
This tag is used to delete a file or group of files with the following attributes.
(1) .file represents the file to be deleted.
(2) .dir represents the directory to be deleted.
(3) .includeEmptyDirs specifies whether to delete an empty directory, and the default value is delete.
(4). Failonerror specifies whether to stop when an error is encountered, and the default value is to stop automatically.
(5) .verbose specifies whether the deleted file is listed, and the default value is not listed.
Label
This tag is used for copies of files or filesets and has the following attributes.
(1). File represents the source file.
(2). Tofile represents the target file.
(3). Todir represents the target directory.
(4) .overwrite specifies whether to overwrite the target file, and the default value is not to overwrite.
(5) .includeEmptyDirs indicates whether to copy an empty directory. The default value is copy.
(6) .failonerror specifies whether to stop automatically if the target is not found, the default value is stop.
(7) .verbose indicates whether the details are displayed or not, but not by default.
Ant debugging
Ant debugging is supported in Eclipse 3.1 and above. The following are the specific steps that Eclipse uses for debugging.
First, set a breakpoint in the target of the Ant script, which is almost no different from the breakpoint in the Java text, as shown in the following figure. However, Ant scripts do not support Hit Count and conditional breakpoints.
Next, start Ant debugging with Debug As-> Ant Build. As with Java debugging, the debug cursor stops at the breakpoint, as shown in the following figure. Ant supports single-step breakpoint execution of commands Step Over and Run to Line.
In addition, we can use the Debug view to see the value of the variable, as shown in the figure.
If you are debugging remotely, you need to add the appropriate parameters to the Ant script, as follows:
... The above is all the content of this article "what is Apache Ant?" thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.