Use eclipse and ant for Java program development

Source: Internet
Author: User

I believe that everyone has some experience in using eclipse. It is a proud thing to write Java source code. It is a great headache to compile a large number of Java source codes, it would be a waste of time to compile the program by clicking "run" one by one. I want to break away from the "Bitter Sea". Now let's go with me and step into ant's use process.

1. Install ant
A. Use the ant provided by eclipse.3.2 to save installation trouble. The default version is Ant-1.6.5, and the latest ant version is 1.7.0.
B. Manually install ant and download the latest ant-1.7.0 ON THE http://ant.apache.org
Decompress the package to any directory, set path, classpath, and ant_home, and then run ant in command line mode.
(I personally suggest you download the latest version of ant from the official website, so that the ant installed in eclipse by default in the form of plug-ins usually does not have Doc, And the next ant-1.7.0, take a look at the doc in it. It is useful for learning ant in detail)

2. Simple Development Instance
A. Create a new project named helloworld in eclipse and a new package named helloworld In the src directory.
B. Create a class named helloworld IN THE helloworld package.
The source code is as follows:
Package helloworld;

Public class helloworld {

Public static void main (string [] ARGs ){

System. Out. println ("helloworld ");

}
}

3. Create a build. xml file
Ant and make are two similar tools. Make searches for files named makefile in the directory to perform the specified operation, while ant searches for files named build. xml.
Episode: ant does not have its own custom language, but the generated files are written in XML. There is a group of ant users who can understand the predefined XML elements for parsing, such as <project>, <target>, <javac>, <jar>, etc.

The content of the build. xml file is as follows:

<? XML version = "1.0"?>
<! -- Define the file name and default execution target of the project file -->
<Project default = "Dist" name = "project argon">
<! -- Add description -->
<Description> A simple Java project </description>
<! -- Add an attribute, which is equivalent to a global variable at the file level. It is used to set the directory name here -->
<Property name = "srcdir" location = "src"/>
<Property name = "builddir" location = "build"/>
<Property name = "distdir" location = "Dist"/>
<! -- Create a target and create a folder -->
<Target name = "init">
<Tstamp/>
<Mkdir dir = "$ {builddir}"/>
<Mkdir dir = "$ {distdir}"/>
</Target>
<! -- Create the target compile, compile the. Java file under the src directory, and output the. Class file to the build directory -->
<Target name = "compile" depends = "init">
<Javac srcdir = "$ {srcdir}" destdir = "$ {builddir}"/>
</Target>
<! -- Create the target Dist. The operation is to package the. Class file, output it to the Dist directory, package the. Java file, and output it to the 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 the target clean and clear the build and DIST directories -->
<Target name = "clean">
<_ Delete dir = "$ {builddir}"/>
<_ Delete dir = "$ {distdir}"/>
</Target>
 
</Project>

4. Open the ant View
Click windows-show view-ant, and the ant view appears.

5. Run ant to generate the target
Drag the build. xml file under the helloworld project in the navigator View to the ant view. Click the green running button to view the output and debugging information in the console window.

After completing the above five steps, I believe you have been able to experience the basic functions of ant.

We hope that the combination of ant and eclipse will help you a little.

PS: Due to the rush of time, the bug in this article must exist. I hope you can test it more and give me a "test report". It is best to pull tomato.

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.