Java program development using Eclipse and Ant

Source: Internet
Author: User
I believe you have some experience in the use of Eclipse, in writing Java source code is a proud thing to compile a large number of Java source code but practice more distressed things, if a point of "run" to compile is too waste of time. Want to get out of the "abyss of Misery," now follow me together , and step into Ant's course of use.

1. Install Ant
A. Using the eclipse.3.2 version of the ant can save installation trouble, the default version of Ant-1.6.5, now the latest ant is 1.7.0
B. Install ant manually and download the latest ant-1.7.0 on http://ant.apache.org
Extract to any directory, set path and Classpath and Ant_home, and then run ant in command line mode.
(Personal advice or go to the official website to download a new version of Ant, the eclipse in the plug-in form of the default installation of ant is usually not doc, the next ant-1.7.0, see the inside of the doc for detailed learning ant is very useful)

2.simple Development Example
A. Create a new project named HelloWorld in Eclipse, and create a new package named HelloWorld in the SRC directory.
B. In the HelloWorld package, create a new class named HelloWorld
The source code is as follows:
Package HelloWorld;

public class HelloWorld {

public static void Main (string[] args) {

System.out.println ("HelloWorld");

}
}

3. New Build.xml file
Ant and make are two tools that are similar, make looks for a file named Makefile in the directory, and Ant looks for a file named Build.xml.
Episode: Ant doesn't have a custom language of its own, but the resulting file is written in XML, and there is a set of predefined XML elements that Ant understands to recognize resolution, such as <project>,<target>,<javac>,< Jar>, wait.

The contents of the Build.xml file are as follows:

<?xml version= "1.0"?>
<!--define the file name of the project file and the default execution target-->
<project default= "Dist" name= "Project Argon" >
<!--add Description-->
<description>a Simple Java project</description>
<!--add properties that are equivalent to a file-level global variable, which is used to set the directory name-->
<property name= "srcdir" location= "src"/>
<property name= "BuildDir" location= "Build"/>
<property name= "Distdir" location= "dist"/>
<!--Create a target, action to create a folder-->
<target name= "Init" >
<tstamp/>
<mkdir dir= "${builddir}"/>
<mkdir dir= "${distdir}"/>
</target>
<!--Create the target compile, which is to compile the. java file in the SRC directory and output the. class file to the build directory-->
<target name= "Compile" depends= "init" >
<javac srcdir= "${srcdir}" destdir= "${builddir}"/>
</target>
<!--Create target dist to package. class file and output to dist directory, package. java file and output to dist directory-->
<target name= "Dist" depends= "compile" >
<jar destfile= "${distdir}/package-${dstamp}.jar" basedir= "${builddir}" >
<!-Create a list--->
<manifest>
<attribute name= "built-by" value= "${user.name}"/>
<attribute name= "Main-class" value= "package". Main "/>
</manifest>
</jar>
<jar destfile= "${distdir}/package-src-${dstamp}.jar" basedir= "${srcdir}"/>
</target>
<!--Create target clean, operation for empty build and Dist directory-->
<target name= "clean" >
<_delete dir= "${builddir}"/>
<_delete dir= "${distdir}"/>
</target>

</project>

4. Open Ant View
Click Windows-show view-ant in turn, and the Ant view appears

5. Run ant, Build target
Drag the Build.xml file under the HelloWorld item in the Navigator view into Ant view, and click the Green button to see the output and debug information in the console window.

Complete the 5 steps above and believe you have been able to experience ant's basic functionality.

Hopefully, the combination of Ant and Eclipse will help.

PS: Because of time haste, this article of the bug must exist, I hope everyone more test, give me back a "test" is best to pull, tomato tomatoes.

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.