Java build tool Ant's first build. xml

Source: Internet
Author: User

Java build tool Ant's first build. xml

ANT is a building tool that features cross-platform, simple operation, simple installation, and low resource usage. It has only one configuration file to build. xml, the thing we need to do is build. in xml, we define what we want to do. ANT has many commands and syntaxes. When I first came into contact with it, it was also very big. Later I learned about the query and basically learned about the entire build. the best way to learn the content contained in xml is to view the example. You can use the example to familiarize yourself with the attributes and parameter meanings in different labels. We do not need to remember all of them, you only need to know how to query it when using it.

Next we will build our first build. xml file, which is just a simple start:

Create the HelloWorld. java file under D:/test/ant/src.

 
 
  1. package test.ant; 
  2. public class HelloWorld { 
  3.     public static void main(String[] args) { 
  4.         System.out.println("Hello World"); 
  5.     } 

Create the build. xml file in the directory at the upper level and type the following content:

 
 
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Project name = "HelloWorld" default = "run" basedir = "."> <! -- The project name is HelloWorld. default indicates the task to be done by default, and. indicates the current directory. -->
  3. <Property name = "src" value = "src"/> <! -- Define the src variable -->
  4. <Property name = "dest" value = "classes"/> <! -- Define the dest variable -->
  5. <Property name = "hello_jar" value = "hello. jar"/> <! -- Define the hello_jar variable -->
  6. <Target name = "init"> <! -- Define a target, which indicates initialization, mkdir indicates creating a directory, and $ {dest} indicates taking the value of the variable dest -->
  7. <Mkdir dir = "$ {dest}"/>
  8. </Target>
  9. <! -- Compile the target. depends is the target on which it depends. compile is executed only after init is executed. -->
  10. <Target name = "complie" depends = "init">
  11. <! -- Run javac command srcdir source directory destdir target directory -->
  12. <Javac srcdir = "$ {src}" destdir = "$ {dest}"/>
  13. </Target>
  14. <! -- Build the target and rely on compile -->
  15. <Target name = "build" depends = "complie">
  16. <! -- Run the jar command jarfile to set the jar file basedir to classes -->
  17. <Jar jarfile = "$ {hello_jar}" basedir = "$ {dest}"/>
  18. </Target>
  19. <! -- Run -->
  20. <Target name = "run" depends = "build">
  21. <Java classname = "test. ant. HelloWorld" classpath = "$ {hello_jar}"/>
  22. </Target>
  23. <! -- Delete the generated file -->
  24. <Target name = "clean">
  25. <Delete dir = "$ {dest}"/>
  26. <Delete file = "$ {hello_jar}"/>
  27. </Target>
  28. <! -- Run again -->
  29. <Target name = "rerun" depends = "clean, run">
  30. <Ant target = "clean"/>
  31. <Ant target = "run"/>
  32. </Target>
  33. </Project>

To summarize the elements used above:

Project node Element

The root element of the Ant build file. attributes:

  • Name attribute: Specifies the name of the project element.
  • Default attribute: Specifies the name of the target to be executed when the project is executed by default.
  • Basedir attribute: used to specify the location of the base path.

Property node Element

Used to define variables or parameters. attributes:

  • Name attribute: variable name
  • Value Attribute: variable value
Ant has some built-in features such as ant. file-> build a file such as build. path of the xml file ant. home-> ant root path ant. java. version-> java version ant. project. name-> name of the current project, in build. specify ant. version-> ant version

Target node Element

A task has the following attributes:

  • Name attribute: Specifies the name of the target element.
  • Depends attribute: used to describe the dependency between targets.
  • If property: used to verify whether the specified property exists. if it does not exist, the target will not be executed.
  • Unless attribute: This attribute is opposite to the if attribute. It is also used to verify whether the specified attribute exists. if it does not exist, the target of the attribute will be executed.
  • Description attribute: a brief description of the target function.

Mkdir command

Create a directory.

Javac command

Compile one or more java files.

Jar command

Used to generate a JAR file.

Delete command

Delete a file or directory.

Related Article

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.