Ant--java Engineering Packaging Tools __java

Source: Internet
Author: User
Tags echo message mkdir pack sin

Really, the blog stopped more than two months, sin sin

Writing this blog is an introduction to Ant. This is a Java packaging tool. It's like a makefile tool for C. However, it is based on XML format, so it may be more casual in syntax, and will not be bothered by the problem of strict indentation like makefile.

Ant learning is actually very simple, the content of a blog can be. Because there are so many knowledge points. But also like makefile, packing a big project is also very difficult to write, are to deal with the dependency relationship. Cut the crap, let's get to the point.


One: Installation

This step, go to ant.apache.org download ant binary package (do not download the source code for that package unless you want to study it). And then just want to configure the environment variables like Java OK. The environment variable points to the bin directory. Once configured, you are ready to use it. Open cmd and run ant-version.


II: Establishment of Build.xml documents

This is a simple ant file.

<project default= "Hello" >
		<target name= "Hello" >
			<echo message= "Hello, ant" >
		</ Target>
	</project>

Save as a build.xml file. Execute in the directory where the file resides: Ant command. 'll see

$ ant
Buildfile:build.xml

Hello:
     [echo] Hello, world builds

successful total
time:2 seconds
Analysis: From the above example, we can see. The build.xml is simple with the following levels:

Project (the project label is the root element of a build file, you can include one or more target,default attributes that are required and indicate target for default execution.) Default target is run because the ant command has no parameter designation

Target (This is discussed in more depth later, as long as you know that the target's name attribute is required)

Task (the smallest unit in ant, usually executes some commands)

This tree-shaped relationship is the most basic build file structure.

P.S. You can do something with some command parameters. For example, the following sentence specifies that ant is executed against Echo.xml, and that the goodbye is specified as the default target

Ant-f Echo.xml Goodbye


Three: Start, pack your Java program

First, simply deal with a file. First write a Hello World Java program (not written here, and don't ask me why). In order to save space, here is a step-by-step, and directly to "compile-pack-run" have done a build file content sent up.

<project default= "Compile" >
  <target name= "Compile" >
    <javac srcdir= "."/>
  </target >

  <target name= "jar" depends= "compile" > <jar destfile= "Hello.jar" basedir= "
         ."
         Includes= "**/*.class"
         <manifest>
               <attribute name= "Main-class" value= "Hello"/>
         </ manifest>
         />
  </target>

  <target name= "Run" depends= "jar" >
    <java classname = "Hello"
          classpath= "Hello.jar"
          fork= "true"
          />
  </target>
</project>


Analysis, very good read.

1. The default implementation of this project is compile. The target task is to execute the javac command (ANT-supported commands, followed by "Java") to compile the Java file. SRCDIR Specifies the location of the Java file to compile.

2. The second target is the jar, and the back depend= "compile" is critical. Indicate the relationship between commands (we always have to package or run without compiling). Notice that in the JAR task I also specify to generate the manifest file (this is the Java configuration file, do not understand the search will understand)

3. The last one is run this target, run is to invoke the Java command on it, specify the appropriate parameters. Students familiar with Java commands here should be able to understand it easily. Also, fork= "true" indicates that a request was run with a new JVM.

To run this, it is recommended that you run this build file with the commands ant run. If you specify default= "Run", you can just ant directly.

Then we can verify that by using this command Java-jar Hello.jar. Did you see that?

Four: Go deep, pack your Java project--use Ant's Properties

Or look at the content first:

<project default= "All" > <property name= "obj-dir" location= "obj"/> <property-name= "Lib-dir" location= "l Ib "/> <property name=" src-dir "location=" src "/> <target" init "> Name= <mkdir" dir=} " /> <mkdir dir= "${lib-dir}"/> </target> <target name= "Clean-init" > <delete dir= "${obj- dir} "/> <delete dir=" ${lib-dir} "/> </target> <target name=" compile "depends=" init "> &L T;javac srcdir= "${src-dir}" destdir= "${obj-dir}"/> </target> <target name= "Clean-co Mpile "> <delete> <fileset dir=" ${obj-dir} "includes=" **/*.class "/> </delete> </ta rget> <target name= "jar" depends= "compile" > <jar destfile= "${lib-dir}/hello.jar" basedir= "${ob" J-dir} "/> </target> <target name=" Clean-jar "> <delete file=" ${lib-dir}/hello.jar "/&G
  T </target> &Lt;target name= "Run" depends= "jar" > <java classname= "Hello" classpath= "${lib-dir}/hello.jar" Fork= ' true '/> </target> <target name= ' all ' depends= ' run '/> <target ' clean ' name= nds= "Clean-init"/> </project>

Tips: Read a build file, preferably based on the target specified by default, read backwards according to dependencies.

Analysis, this file can actually deal with a lot of your Java project (of course, here we ignore only one hello.java fact). Brush up, a good project can be as follows, generally have obj, lib, src and other meaningful directories. The SRC directory holds the source code, obj stores the compiled code (here is. Class), and the Lib directory holds the packaged file (the generated library). Feeling said these, the above code should be no problem, it should be noted that mkdir and other tags are actually called ant command.

Execute Ant and run successfully. When you find that you want to kill your files, ant clean. This is the other target in the build file.


Five: What ' s more?

It's like the makefile of C. Ant also supports some more complex operations, such as conditional statements. These are generally used to write your test tasks, like the clean task in the previous section, so that your project looks more robust.

The better use of ant testing is to use JUnit. But it is not introduced here.

For more ant tasks, see http://ant.apache.org/manual/tasksoverview.html here. Like the property tasks are very important.

Also recommend Ant's Official help document Http://ant.apache.org/manual/index.html. Content is not much, simple and comprehensive.


VI: Written in the last

Learning Ant is actually condemned to me ... the Eclipse plugin for Hadoop is not done, so you have to use ant ... But I feel ant is better than eclipse.

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.