Ant uses and simple implementations

Source: Internet
Author: User
Tags echo command echo message

 Ant uses and simple implementationsTags: antjavadeletejarbuildjavaee2012-07-17 14:15 5945 People read comments (0) favorite reports Classification:Other (6)

The role of ant tools:
1, you can compile Java class with ant, Generate class file
2, Ant can customize the label, configuration file
3. Ant can structure the relevant layer into a package
4. Ant builds the entire project into a Web package and publishes it to Tomcat

Maybe you heard someone talk about ant, on impulse to learn ant, when you read the first instance above, maybe you feel ant is really good, maybe you feel ant so much, to come to these conclusions can not say wrong, although ant is very useful, but not in any case is the best choice, For example, there are more simpler, easier-to-use tools on Windows, such as Eclipse+myeclipse ECLIPSE+WTP, and so on, whether it's compiling, deploying, running, and easier to use than ant, but in some cases it's a good place for ant to play:
1, when deploying on the server
When your program is developed and deployed to the server, it is not always possible to configure a eclipse+myeclipse because of the installation of a program, Ant is a good choice at this time because it is small, easy to configure, You take your build.xml to any server, just make simple changes (some settings, such as a directory), then one or two commands to complete, this is not a good thing.
2,linux, most of the time, program development is under Windows, but programs run on Linux or UNIX, on Linux or
Deployment on Unix (especially Unix) is a hassle, and this time the ant feature comes out because Ant is cross-platform and you can use it on most operating systems in Build.xml, with minimal modification.
3, when the server maintainer does not understand the programming
A lot of people have had this experience, people who use your program do not know how to write programs. You have to update the program because it needs to be re-deployed again and again because of bug fixes. This time you will find it difficult to teach a person. But with ant, you just need to tell him to enter one or two commands such as Ant xxx, everything OK.

These are some of the things I have encountered. After reading the above situation, think about whether you need to use Ant.

-------------------------------------------------Ant Simple Tutorial-------------------------------------------------


First, ant key elements
1. Project elements
The project element is the root element of the ant artifact file, and the ant component 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

Used to specify the name of the project element.

2) Default Property

Specifies the name of the target to execute when project is executed by default.

3) Basedir Property

Used to specify the location of the base path. When this property is not specified, use the attached directory of the ANT's artifact file as the base directory.

<?xml version= "1.0"?>
<project name = "Antpro" default = "Getbasedir" Basedir = "C:/thinkinjavacode" >
<target name= "Getbasedir" >
<echo message= "The base dir is: ${basedir}"/>
</target>
</project>

As can be seen from the example above, the value of the default property defined here is Getbasedir, which means that when the ant command is run, the default target--getbasedir will be executed if no target is specified for execution. Also, define the value of the Basedir property as "C:/thinkinjavacode" and run the ant command after entering "C:/thinkinjavacode" to get the result:

Buildfile:c:\thinkinjavacode\build.xml

Saybasedir:
[Echo] The Base dir is:c:\thinkinjavacode

BUILD Successful
Total time:0 seconds

2. Target element
Target is the basic execution unit of ant, which can contain one or more specific tasks. Multiple target can exist 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 a dependency between target and, if there is a dependency on more than one target, the "," interval is required. ANT executes each target sequentially, in the order in which the target appears in the Depends property. The target to be relied upon will be executed first.

3) If property

Used to verify that the specified property exists, and if it does not exist, the 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.

Examples are as follows:

<?xml version= "1.0"? > 
<project name = "Targetpro" default= "Targetb" >&NBSP;
<target Name= "Targeta" if = "ant.java.version" >&NBSP;
        <echo message = " Java Version: ${ant.java.version} "/>&NBSP;
    </target> 
    <target name= "Targetb"   depends = "Targeta" unless = "Philander" >&NBSP;
         <description> 
            A Depend example! 
        </description> 
         <echo message = "The base dir is: ${basedir}"/>&NBSP;
    </ target> 
</project> 

As you can see from the following results, we are running target named Targetb, because it relies on targeta, so Targeta will be executed first, and because the system is configured with JDK, the Ant.java.version attribute exists and executes Targeta , Output information: "[echo] Java version:1.6", Targeta execution, then execute TARGETB, because Philander does not exist, and the unless attribute is not present when the target is entered, so TARGETB To execute, output information: "[echo] the base dir is:c:\thinkinjavacode".

Buildfile:c:\thinkinjavacode\build.xml

Targeta:
[Echo] Java version:1.6

TARGETB:
[Echo] The Base dir is:c:\thinkinjavacode

BUILD Successful
Total time:0 seconds

3. Property elements
Property elements can be thought of as parameters or parameter definitions, and project properties can be set through the attribute element or outside of Ant. To introduce a file externally, such as a build.properties file, you can introduce it with the following content:

1 <property file= "Build.properties"/>

Property elements can be used as attribute values for a task. In a task, this is done 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 give a list of system properties that are consistent with the properties of the System.getpropertis () method in the Java documentation, which can be referenced by the Sun Web site's description. At the same time, Ant also provides some of its own built-in properties, as follows:

The absolute path of the Basedir:project base directory;

The absolute path of the ant.file:buildfile, the Ant.file value in the above example is C:\ThinkInJavaCode\build.xml;

Ant.version:Ant version information, this article is 1.8.1;

Ant.project.name: The name of project that is currently specified, which is the name attribute value of project mentioned earlier;

Ant.java.version:Ant detected JDK version, this article is 1.6.

Examples are as follows:

<? XML version= "1.0"?>
<project name = "Propertypro" default = "Example" >
<property name = "name" value = "Philander"/>
<property name = "Age" value = "/>"
<target name = "Example" >
<echo message = "Name: ${name}, Age: ${age}"/>
</target>
</project>

In the example above, the user has set two properties named Name and age, and after these two properties have been set, the two property values can be obtained by ${name} and ${age}, respectively.

Second, ant common commands
1. Copy command
Copy is primarily used for copying files and directories. Examples are as follows:

Eg1. To copy a single file:

<copy file= "Original.txt" tofile= "Copied.txt"/>

EG2. To copy a file directory:

<copy todir= ". /dest_dir ">
<fileset dir= "Src_dir"/>
</copy>

Eg3. Copy the file to a different directory:

1 <copy file= "Source.txt" todir= ". /home/philander "/>

2. Delete command
To delete a file or directory, see the following example:

Eg1. Delete a file:

1 <delete file= "/home/photos/philander.jpg"/>

EG2. To delete a directory:

1 <delete dir= "/home/photos"/>

Eg3. Delete all backup directories or empty directories:

<delete includeemptydirs= "true" >
<fileset dir= "." includes= "**/*.bak"/>
</delete>

3. mkdir command
Create a directory. Eg:

1 <mkdir dir= "/home/philander/build/classes"/>

4. Move command
To move a file or directory, for example:

Eg1. To move a single file:

1 <move file= "sourcefile" tofile= "DestFile"/>

EG2. To move a single file to another directory:

1 <move file= "sourcefile" todir= "Movedir"/>

Eg3. To move a directory to another directory:

<move todir= "Newdir" > <fileset dir= "Olddir"/></move>

5. Echo command
The purpose of this task is to output information based on the level of the log or monitor. It includes the message, file, append, and level four properties, for example:

1 <echo message= "hello,ant" file= "/home/philander/logs/ant.log" append= "true" >

Iii. building and deploying Java projects with Ant
Ant can instead use commands such as Javac, Java, and jars to perform Java operations, making it easy to build and deploy Java projects.

1. Using Ant's Javac command to compile Java programs
Ant's Javac command is used to implement the functionality of compiling Java programs. Here's a simple example: first we build a Java project named Javatestpro, build the SRC directory as the source code directory, and build the Helloworld.java class file in the SRC directory. The contents of this type of file are as follows:

public class HelloWorld {
public static void Main (string[] args) {
System.out.println ("Hello world!");
}
}

At the same time, in the root directory of the Javatestpro project, the Build.xml file is built, the Java file under the SR directory is compiled, and the compiled class file is placed in the Build/classes directory, the entire project directory structure is as follows:

| Javatestpro
|src
|build
|classes
|build.xml

Before compiling, the classes directory needs to be cleared, and the contents of the file are as follows:

<?xml version= "1.0"?>

<project name = "Javactest" default= "Compile" basedir= "." >
<target name= "clean" >
<delete dir= "${basedir}/build"/>
</target>
<target name= "compile" depends = "clean" >
<mkdir dir = "${basedir}/build/classes"/>
<javac srcdir = "${basedir}/src" Destdir = "${basedir}/build/classes"/>
</target>
</project>

After the project root directory (C:\THINKINJAVACODE\JAVATESTPRO) executes the ant command, the newly generated build/classes subdirectory can be found under that directory, and the Helloworld.class file generated after compilation is in that directory.

2. Executing Java programs using Java commands
The ability to run Java programs can be implemented using Java commands in Ant. Can be modified on the basis of the above build.xml to achieve:

<?xml version= "1.0"?>
<project name = "Javactest" default= "Run" basedir= "." >
<target name= "clean" >
<delete dir= "${basedir}/build"/>
</target>
<target name= "compile" depends = "clean" >
<mkdir dir = "${basedir}/build/classes"/>
<javac srcdir = "${basedir}/src" Destdir = "${basedir}/build/classes"/>
</target>
<target name= "Run" depends = "compile" >
<java classname = "HelloWorld" >
<classpath>
<pathelement path= "${basedir}/build/classes"/>
</classpath>
</java>
</target>
</project>

Next, you can see the output in the console: "[java] Hello world!"

3. Using the jar command to generate the jar file
You can also build a jar package based on the example above, and you can add the following target under the run target:

<?xml version= "1.0"?>
<project name = "Javactest" default= "Jar" basedir= "." >
<target name= "clean" >
<delete dir= "${basedir}/build"/>
</target>
<target name= "compile" depends = "clean" >
<mkdir dir = "${basedir}/build/classes"/>
<javac srcdir = "${basedir}/src" Destdir = "${basedir}/build/classes"/>
</target>
<target name= "Run" depends= "compile" >
<java classname = "HelloWorld" >
<classpath>
<pathelement path= "${basedir}/build/classes"/>
</classpath>
</java>
</target>
<target name= "Jar" depends= "Run" >
<jar destfile= "Helloworld.jar" basedir= "${basedir}/build/classes" >
<manifest>
<attribute name= "Main-class" value= "HelloWorld"/>
</manifest>
</jar>
</target >
</project>

Where Project's default property setting should be set to Jar,ant after the run is complete, you can see that a Helloworld.jar jar package is generated at the root of the project. You can execute the jar package by running the following command:

1 Java-jar Helloworld.jar

4. Use the War command to package a Java EE project
Create a Java EE project, where SRC is the source directory for the JavaScript, webcontent for each JSP directory, and Lib is the directory for the package referenced by the project. The Build.xml file is created under the WebTest project directory, which is the Ant component file for the project.

| WebContent
|src
|build
|classes
| WebContent
| Meta-inf
| MANIFEST. Mf
| Web-inf
|lib
|classes
| hellojsp.jsp
|build.xml

The reader can be placed in the SRC directory in the previous example of the development of the Helloworld.java file, and webcontent under the creation of hellojsp.jsp file, the content is very simple, is to output Hello information, the code is as follows:

<%@ page language= "java" contenttype= "Text/html;charset=utf-8" pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>war test for Ant</title>
<body>
Hello jsp! Hello ant!
</body>

Next, write the Build.xml file, which reads as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<project name = "WebTest" default = "war" Basedir = "." >
<property name = "Classes" value = "${basedir}/build/classes"/>
<property name = "Build" value = "${basedir}/build"/>
<property name = "Lib" value = "${basedir}/webcontent/web-inf/lib"/>
<!--Delete the build path--
<target name = "clean" >
<delete dir = "${build}"/>
</target>

<!--build the build/classes path and compile the class file to the build/classes path--
<target name = "Compile" depends = "clean" >
<mkdir dir = "${classes}"/>
<javac srcdir = "${basedir}/src" Destdir = "${classes}"/>
</target>

<!--War Pack--
<target name = "war" depends = "compile" >
<war destfile = "${build}/webtest.war" webxml = "${basedir}/webcontent/web-inf/web.xml" >
<!--copy Webroot under two folders in addition to Web-inf and Meta-inf--
<fileset dir = "${basedir}/webcontent" includes = "**/*.jsp"/>
<!--Copy the jar package in the Lib directory--
<lib dir = "${lib}"/>
<!--copy the class file under Build/classes--
<classes dir = "${classes}"/>
</war>
</target>
</project>

After you run ant in the C:\ThinkInJavaCode\WebTest directory, the Webtest.war file is generated and can then be placed in the appropriate directory for the Web container (such as Tomcat) (${tomcata installation directory}\webapps) Run the Web project.

Executes the Build.xml file: cmd enters the directory where the file executes the command: Ant-file build.xml.

Original address: http://www.cnblogs.com/philander/articles/1782254.html

Ant uses and simple implementations

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.