Compiling and packaging with Ant (Java)

Source: Internet
Author: User

Ant is the best packaging deployment tool in the Java environment, which is written in XML format and has a very powerful function.
This article describes how to manually use ant to compile and package Java programs.
First, install Ant
1. Download and install Ant.
To the official homepage http://ant.apache.org Download the new version (currently Ant1.8.1) ant, get a apache-ant-1.8.1-bin.zip compression package. Unzip it to your hard drive, for example: D:\apache-ant-1.8.1.
2. Configure the environment variables.
Select: My Computer, properties, high-level environment variable--system variable--the value of the variable->path-> edit, add the following to the front:
D:\apache-ant-1.8.1\bin;
Then click "OK" to complete the configuration of the environment variables.
3. Verify Ant.
To verify that ant is installed successfully, you can do the following:
Select: Start and Run->cmd, enter the following command:
Ant
If the following appears, the installation is successful:
Buildfile:build.xml does not exist!
Build failed
However, if the following occurs, the installation fails: (the preceding steps should be repeated until the installation is successful.) )
' Ant ' is not an internal or external command, nor is it a running program or batch file.

Second, edit the Java source file
In my "Java Notepad" example, the new file e:\ data \test\src\notepad.java, the structure is as follows:

Package com.xiboliya.snowpad;
public class NotePad {
public static void Main (string[] args) {
// ...
}
}

Third, edit ant
New file e:\ data \test\build.xml, which reads as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<project name= "Antproject" default= "Build" basedir= "." >
<property name= "srcdir" location= "src"/>
<property name= "BinDir" location= "Ant/bin"/>
<property name= "Jardir" location= "Ant/jar"/>
<property name= "Jarfile" location= "${jardir}/notepad.jar"/>
<property name= "Package" value= "Com.xiboliya.snowpad"/>
<property name= "MainClass" value= "NotePad"/>
<target name= "Init" >
<delete dir= "${bindir}"/>
<delete dir= "${jardir}"/>
<mkdir dir= "${bindir}"/>
<mkdir dir= "${jardir}"/>
</target>
<target name= "Compile" depends= "Init" >
<javac srcdir= "${srcdir}" destdir= "${bindir}"/>
</target>
<target name= "Makejar" depends= "Init,compile" >
<jar destfile= "${jarfile}" basedir= "${bindir}" >
<manifest>
<attribute name= "Main-class"
Value= "${package}.${mainclass}"/>
</manifest>
</jar>
</target>
<target name= "Build" depends= "Init,compile,makejar" >
<echo message= "Ant is building the project."/>
</target>
</project>

Four, run Ant
1. At the command prompt, go to directory e:\ data \test, enter the following command:
Ant
If there is no error, the following information is output:

Buildfile:build.xml

Init:
[Delete] Deleting Directory e:\ Information \test\ant\bin
[Delete] Deleting Directory e:\ Information \test\ant\jar
[MkDir] Created dir:e:\ Information \test\ant\bin
[MkDir] Created dir:e:\ Information \test\ant\jar

Compile:
[Javac] Compiling 1 source file to e:\ data \test\ant\bin

Makejar:
[Jar] Building jar:e:\ Information \test\ant\jar\notepad.jar

Build:
[Echo] ANT is building the project.

BUILD Successful
Total Time:2 seconds

2. Double-click the generated e:\ data \test\ant\jar\notepad.jar file, and if it works correctly, the ant is written correctly.

Use:

The. jar file has two uses, the first one can be executed directly under DOS, the specific step is to run into the. jar Directory Java-jar Xxx.jar Enter can be executed directly. The second is to use this jar as a class library, that is, you can invoke it by referencing it in other Java classes (which can be referenced at the front of the class source file with Import XXX).

Compiling and packaging with Ant (Java)

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.