Use ant to compile and package java files

Source: Internet
Author: User

Use ant to compile and package java files
1. Use ant independently
1.1 get ant and install
Go to http://ant.apache.org/to download the file. Decompress the package in C:/ant. (Document Writing, path as short as possible)
1.2 Configuration
Set environment variables:
Ant_home: C:/ant
Path:; C:/ANT/bin;
1. 3. Use
Create a folder project.
1.2.1 create a project.
Create SRC under it to store the source code.
Lib is used to store the jar package to be used.
1.2.2 create an XML file
Example. XML (ANT recognizes build. xml by default), in which the write process starts. This usually includes setting folder property values and initializing (clearing the folder

To start from), compile, package, and execute several steps.
See the help manual (C:/ANT/docs/manual/index.html)/developing with ant/tutorials in the ant documentation.

.
Example:
<? XML version = "1.0"?>
<Project name = "ant_test" default = "run">

<! -- Description of various folders -->
<Property name = "src. dir" value = "src"/>
<Property name = "report. dir" value = "report"/>
<Property name = "classes. dir" value = "classes"/>
<Property name = "Lib. dir" value = "lib"/>
<Property name = "Dist. dir" value = "Dist"/>
<Property name = "Doc. dir" value = "Doc"/>
<! -- Class for storing the main function -->
<Property name = "Main-class" value = "Test. Test"/>

<! -- Description classpath, including the jar package used in the project -->
<Path id = "classpath">
<Fileset file = "$ (Lib. DIR)" includes = "**/*. Jar"/>
</Path>
 
<! -- Initialize and clear the files in the folder for recompilation -->
<Target name = "init">
<Delete dir = "$ {classes. dir}"/>
<Delete dir = "$ {Dist. dir}"/>
<Delete dir = "$ {Doc. dir}"/>
</Target>

<! -- Compile -->
<Target name = "compile" depends = "init"
Description = "compile the source files">
<Mkdir dir = "$ {classes. dir}"/>
<Javac srcdir = "$ {SRC. dir}" destdir = "$ {classes. dir}" classpath = "classpath"/>
</Target>
<! -- Package it into a jar file with the project name or other name -->
<Target name = "jar" depends = "compile" Description = "make. jar file">
<Mkdir dir = "$ {Dist. dir}"/>
<Jar destfile = "$ {Dist. dir}/$ {ant. Project. name}. Jar" basedir = "$ {classes. dir}">
<Manifest>
<Attribute name = "Main-class" value = "$ {main-class}"/>
</Manifest>
</Jar>
</Target>
<! -- Run -->
<Target name = "run" depends = "jar">
<Java jar = "$ {Dist. dir}/$ {ant. Project. name}. Jar" fork = "true"/>
</Target>
</Project>
1.2.3 run ant
Command Line in the project, ant-buildfile example. xml run
Note:
A. For build. XML, you can directly write: ant (run the default target of build. XML)
B. if you want to run another target, such as init: ant-buildfile example. xml init
See the manual runnint ANT/command line.
2. Use ant in eclipse
1. Create a project.
Create a source folder to store the source code.
Jar used for lib
Build. xml file
2. Call up ant
Window/show view select ant, and then select Add buildfiles. In the dialog box, select the build. xml file of the project.
3. Run
Compile build. XML as above. Each target is displayed in ant view. You can double-click the target to execute it separately.
Click the "run default target" button to run the default target. Here, the default target is set to the last one. You can execute each target on which it depends.

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.