Apache-ant Preliminary understanding of _ant

Source: Internet
Author: User
Tags create directory echo message
Ant is a Cross-platform component tool under the Apache Foundation, which enables the automatic construction and deployment of projects. In this article, the reader is primarily familiar with how to apply ant to a Java project to simplify build and deploy operations.
I. Installation and configuration
Download Address: http://ant.apache.org/, downloaded in this article is version 1.9.9. Extract to a directory (for example: D:\software\apache-ant-1.9.9)
Add the System environment variable: Ant_home, which points to the ANT-unzipped root directory, which is D:\software\apache-ant-1.9.9.
After installation and configuration, the reader can test whether ant is available, first enter Ant's Bin directory, Run command ant–version, and if the installation and configuration succeeds, the ant version information is displayed
As you can see, when the reader runs Ant's command, it needs to go into Ant's Bin directory and how to get the system to automatically find Ant. The reader is then required to add ant's Bin directory to the System environment variable path. Once the setup is complete, we can enter the ant command in any directory (such as the C: "Documents and Settings" Amigoxie directory) to get the result of the command running.
Two. Ant's key element

Ant's widget files are written based on XML, and the default name is Build.xml. To get a better idea of ant, write a simple ant program here to show Ant's functionality and give the reader a rudimentary understanding of ant. First, set up a build.xml file under D disk, which reads as follows:

<?xml version= "1.0"?>   
    <project name= "HelloWorld" >     
        <target name= "SayHelloWorld" >             
        <echo message= "Hello,amigo"/>     
        </target>  
    </project>
Readers can go to D disk and run Ant SayHelloWorld

Where SayHelloWorld is the name of the task that needs to be performed.

If the file name is not Build.xml and is hello.xml, when the reader runs the same command, the following error occurs in the command window: Buildfile:build.xml does not exist!
Build failed

The error prompted by the command above can be seen,

The ant command finds the Build.xml file by default.

If the file name is Hello.xml, the reader also needs to make a slight change to the command,

To

Ant–f Hello.xml SayHelloWorld,

Ant–buildfile Hello.xml SayHelloWorld,

Ant–file Hello.xml SayHelloWorld. Key elements of Ant project, target, property, and task.
1. Project elements
The project element is the root element of the ant widget file, and the ant widget file should contain at least one project element, or an error will occur. Under each project element, you can include multiple target elements. Next, show the reader the properties of the project element.
1) Name property
Use to specify the name of a project element.
2) Default Property
Specifies the name of the target to execute when project defaults.
3) Basedir Property
is used to specify the location of the base path. When this property is not specified, use the attached directory of ant's widget file as the base directory.
2. Target element
It is the basic execution unit of ant, which can contain one or more specific tasks. Multiple target can have interdependencies. It has the following properties:
1) Name property
Specifies the name of the target element, which is unique within a project element. We can specify a target by specifying the name of the target element.
2) Depends property
Used to describe the dependencies between target and if there is a dependency on multiple target, the "," interval is required. Ant executes each target sequentially according to the order in which target appears in the depends attribute. The target being relied on will execute first.
3) If property
Used to verify that the specified property exists, and if it does not exist, Target will not be executed.
4) Unless property
The function of this property is the opposite of the function of the If property, and it is also used to verify that the specified property exists, and if it does not exist, the target will be executed.
5) Description Property
This property is a short description and description of the target feature.
3. Property element
This element can be considered as a parameter or argument definition, and project properties can be set by the property element or outside of ant. To introduce a file externally, such as a build.properties file, you can introduce it through the following: <property file= "Build.properties"/>
Property elements can be used as attribute values for a task. The task is implemented by placing the property name between "${" and "}" and placing it in the position of the task property value.
Ant provides some built-in properties that can be obtained with a list of system properties consistent with those obtained by the System.getpropertis () method in the Java document, which refer to the Sun Web site's instructions.
At the same time, Ant also provides some of its own built-in properties, as follows:
Basedir:project the absolute path of the base directory, which is explained in detail when explaining project elements, and is no longer verbose;
Ant.file:buildfile's absolute path, as in the above example, the Ant.file value is D: "Build.xml;
The ant.version:Ant version, in this article, has a value of 1.9.9;
Ant.project.name: The name of the currently specified project, that is, the value of project's Name property as previously mentioned;

The version of the JDK that Ant.java.version:Ant detected is 1.7 in the results of the previous example run.

<?xml version= "1.0"?> <project name= "Propertystudy" default= "  
     example" >   
    <property name= "name "Value=" amigo "/>   
    <property name=" age "value=" a "/> <target" name= "   
    >       
            Example Message= ' name: ${name}, Age: ${age} '/>  
     </target>   
    </project>
The results of this example are shown in the following illustration: name:amigo,age:25
As this reader can see, the following two statements are adopted:
<property name= "name" value= "Amigo"/>
<property name= "age" value= "/>"
We set two properties named Name and age, and after these two properties are set, readers can get the values of these two properties, respectively, through ${name} and ${age}.
Three Ant's Common tasks
Each task in the Ant tool encapsulates the specific functionality to be performed and is the basic execution unit of the Ant tool. In this section, the reader is primarily guided to look at Ant's Common tasks and their usage examples.
1. Copy Task
This task is primarily used for copying files and directories. Examples are as follows:
Eg1. Copy a single file: <copy file= "file.txt" tofile= "Copy.txt"/>
Eg2. Copy the file directory:
<copy todir= ". /newdir/dest_dir ">
<fileset dir= "Src_dir"/>
</copy>
Eg3. To copy a file to a different directory:
<copy file= "file.txt" todir= ". /other/dir "/>
2. Delete Task
Delete the file or directory, for example:
Eg1. Delete a file: <delete file= "Photo/amigo.jpg"/>
Eg2. Delete a directory: <delete dir= "Photo"/>
Eg3. Delete all backup directories or empty directories:
<delete includeemptydirs= "true" >
<fileset dir= "." includes= "**/*.bak"/>
</delete>
3. MkDir Mission
Create a directory. Eg:<mkdir dir= "Build"/>
4. Move Task
Move the file or directory, for example:
Eg1. Move a single file: <move file= "FromFile" tofile= "ToFile"/>
Eg2. Move a single file to another directory: <move file= "FromFile" todir= "Movedir"/>
Eg3. To move a directory to another directory:
<move todir= "Newdir" >
<fileset dir= "Olddir"/>
</move>
5. Echo Mission
The role of this task is to output information based on the level of the log or monitor. It includes message, file, append, and level four attributes, for example:
<echo message= "Hello,amigo" file= "Logs/system.log" append= "true" >
Four Building and deploying Java Engineering with Ant

Ant can perform Java operations in lieu of commands such as Javac, Java, and jars to make it easy to build and deploy Java engineering.

<?xml version= "1.0" encoding= "UTF-8"?> <project name= "Testant" "Run" default= "." > <!--basedir is the root of the work. Represents the current directory, and default represents what you want to do. --> <property name= "src" value= "src"/><!--property name--> <property name= "dest" value= "Classes"/&gt  
    ; <property name= "Hello_jar" value= "Hello1.jar"/> <target name= "Init" ><!--initialization--> &LT;MKD IR dir= "${dest}"/> </target> <target name= "compile" depends= "init" ><!--compiling depends is what it depends on. Lai's target, ant checks to see if Init has been executed before executing this target, such as the compile here, if executed, directly executes the compile, and if not, the target that it relies on, such as Init here, is executed first. Then execute this target--> <javac srcdir= "${src}" destdir= "${dest}"/> </target> <targ   
    ET name= "build" depends= "compile" ><!--generate jar packs--> <jar jarfile= "${hello_jar}" basedir= "${dest}"/> </target> <target name= "Run" depends= "Build" ><!--run--> <java clasSname= "Com.test.Test" classpath= "${hello_jar}"/> </target> <target name= "clean" ><!-- Delete the generated file--> <delete dir= "${dest}"/> <delete file= "${hello_jar}"/> </target&  
      
    Gt <target name= "Rerun" depends= "Clean,run" ><!--run again, showing how to invoke other target--> <ant in a target. target= "Clean"/> <ant target= "Run"/> </target> <property name= "Classes" value= "b Uild/classes "/> <property name=" Build "value=" Build "/> <property name=" Lib "value=" Webroot/web-inf " /lib "/> <!--War package--> <target name=" war "depends=" compile "> <war destfile=" ${build}/antwebproject.war "webxml=" Webroot/web-inf/web.xml "> <!--copy WebRoot two texts except Web-inf and Meta-inf            
      Clip--> <fileset dir= "WebRoot" includes= "**/*.jsp"/> <!--the jar package under the copy Lib directory-->  class file Build/classes <lib dir= "${lib}"/> <!--copy--> <classes dir  = "${classes}"/> </war> </target> </project>

The code is as follows:

<?xml version= "1.0"?> <project name= "HelloWorld" > <target name = "SayHelloWorld" > <echo Messa Ge= "Hello,amigo"/> </target> </project> <project name= "Propertystudy" default= "Example" > &LT;PR Operty name= "name" value= "Wjy"/> <property name= "age" value= ""/> <target name= "Example" > <echo mes Sage= "Name:${name},age:${age}"/> </target> </project> <!--learning Goals: Basedir--> <!-- Which: Basedir this path must exist, otherwise, error, such as: Basedir D:\ant\a does not exist--> <project name= "Projectstudy"  Saybasedir "basedir=" D:\ant\ "> <target name=" saybasedir "description=" Print message "> <echo message=" the base dir Is:${basedir} "/> </target> </project> <!--learning Goals: Creating catalogs Note: There are no description properties in Project--> <project Name= "Propertystudy" default= "mkdir" > <target name= "mkdir" description= "Create directory" > <mkdir dir= "mkdirTest2"/&
	Gt </target> </project> <!--learning Goals: PropertY-variable--> <project> <property name= "name" value= "Wjy"/> "<property" name= "Age" value= ""/> <targe T Name= "Example" > <echo message= "name: ${name}; Age: ${age}"/> </target> </project> <!--learning Goal: Copy a single text
		Pieces and folders--> <project name= "Propertystudy" default= "copydir" > <target name= "copyfile" description= "Copy Files" > File is the source file; ToFile is the generated files <copy file= "AntStudy.txt" tofile= "Copy.txt"/> </target> <target name= "Copydir" D 
	escription= "Copy directory" > <copy todir= "mkdirtestcopy" > copy-generated folder <fileset dir= "Mkdirtest"/> source folder </copy> </target> </project> <!--learning Goals: Deleting individual files and folders deleting a backup directory and an empty directory--> <project name= "Propertystudy" default= "D Eldir "> <target name=" delfile "description=" Delete file "> <delete file=" mkdirtestcopy/copy.txt "/> </targe t> <target name= "Deldir" description= "delete directory" > <delete dir= "mkdirtestcopy"/> </target> <targe T name= "Delbakandempty" description= "Delete empty directory and backup directory" > <delete includeemptydirs= "true" > <fileset dir= "." includes= "**/*.bak"/> </d Elete> </target> </project> <!--learning Goals: Moving files, directories--> <project name= "Propertystudy" default= " Movedirtodir "> <target name=" moveFile "description=" Mobile Files > <move file= "Mkdirtest/copy.txt" Mkdirtestcopy/copy.txt "/> </target> <target name=" Movefiletodir "description=" moving files to specified directory > <move fi Le= "3.xlsx" todir= "Mkdirtest"/> </target> <target name= "Movedirtodir" description= "Move Directory to directory ( If the Dir directory is not the same as the Todir directory, the contents of the Dir directory are moved under the Todir directory, the dir directory is deleted, or the directory name is renamed (if the Dir directory exists and the Todir directory does not exist, the Dir directory will be changed to the name of the Todir directory. is called: directory rename) "> <move todir=" mkdirTestCopy2 "> <fileset dir=" mkdirtestcopy "/> </move> </target&
Gt </project> <!--learning Goals: compiling--> <project name= "javactest" default= "Compile" basedir= "." > <target name= "clean" description= "delete compiled directories and files" > <delete dir= "Build"/>
		</target> <target name= "compile" depends= "clean" description= "compiling source Files" > <mkdir dir= "build/classes"/> <javac srcdir= "src" destdir= "build/classes"/> </target> </project> <!--learning Goals: Compiling and running Java programs--> & lt;! --General run: Ant Run, now project has default defaults: Can run directly: Ant can--> <project name= "javatest" default= "Run" basedir= "." > <target name= "clean" description= "clear Catalog" > <delete dir= "Build"/> </target> <target name= "com Pile "depends=" clean "description=" compiled, provided that Demo.java files are placed under Src/build/classes folder "> <mkdir dir=" build/classes "  > <javac srcdir= "src" destdir= "build/classes" includeantruntime= "on"/> </target> <target name= "Run" depends= "Compile" description= "Run" > <java classname= "Demo" > <classpath> <pathelement path= "Bui" Ld/classes "/> </classpath> </java> </target> </project> <!--learning goals: Playing jar bag--> &LT;PR Oject name= "Javatest" default= "Jar "Basedir=". > <target name= "jar" description= "jar Pack" > <jar destfile= "Demo.jar" basedir= "src/build/classes/" > < manifest> <attribute name= "Main-class" value= "Demo"/> </manifest> </jar> </target> &l T;/project> <!--default packaging operation--> <project name= "structured" default= "Archive" > <description> Compile and package an application </description> <!--create an output directory under the engineering directory: build/classes and dist--> <target name= "Init" description= " Create directory > <mkdir dir= "build/classes"/> <mkdir dir= "dist"/> </target> <!--compile output directory: srcdir-source directory and Des tdir-target directory--> <target name= "compile" depends= "init" description= "compiling" > <javac srcdir= "src" destdir= "build/" Classes "/> </target> <!--packaged, depends depends on which target--> <target name=" Archive "depends=" compile "descript ion= "Packaging" > <war destfile= "Dist/antwebproject.war" basedir= "build/classes"/> <jar destfile= "dist/ Project.jar "basedir=" build/classes "/>
	</target> <!--cleanup delete--> <target name= "clean" depends= "init" description= "cleanup" > <delete dir= "Build "/> <delete dir=" dist "/> </target> </project> <!--Web Project play War package--> <project name= "Antwebproject" default= "war" basedir= "." > <!--define constants, tasks can reference--> <property name= "Classes" value= "build/classes"/> <property name= "Build" value= "Build"/> <property name= "Lib" value= "Webroot/web-inf/lib"/> <!--Delete build path--> <target name= "clean" > <delete dir= "Build"/> </target> <!--establish build/classes path and compile class file to build/classes--> path <tar Get name= "compile" depends= "clean" > <mkdir dir= "${classes}"/> <javac srcdir= "src" destdir= "${classes}"/&G
	T </target> <!--playing war package--> <target name= "war" depends= "compile" > <war destfile= "${build}/antproject . War "webxml=" Webroot/web-inf/web.xml "> <!--copy WebRoot in addition to Web-inf and meta-inf two folders, * * means: match 0 or more meshRecord--> <fileset dir= "WebRoot" includes= "**/*.jsp"/> <!--the jar package--> <lib "dir=}" under the copy Lib directory ${lib <!--copy build/classes class file--> <classes dir= "${classes}"/> </war> </target> </project& Gt


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.