Ant learning Example 1 (use ant to create a jar package and run helloworld)

Source: Internet
Author: User


Build. properties (if there are many attributes, they can be placed in the attribute file and then referenced in build. XML)

Execute. Class = org. Test. Ant. helloworld
Jar. Name = World. Jar

Build. xml

<? XML version = "1.0" encoding = "UTF-8"?>


<Project default = "execute">
<! -- Use location instead of value when defining a path using properties -->
<Property name = "build. dir" location = "build"> </property> <! -- Name is referenced by $ {name}, and ID is referenced by refID = "ID". -->

<Property name = "src. dir" location = "src"> </property>

<Property name = "build. SRC" location = "$ {build. dir}/src"> </property>

<Property name = "build. Classes" location = "$ {build. dir}/classes"> </property>
<Property name = "build. Dist" location = "$ {build. dir}/Dist"> </property>
<! --
<Property name = "Execute. Class" value = "org. Test. Ant. helloworld"> </property>
<Property name = "jar. Name" value = "Hello. Jar"> </property>
-->
<! -- If there are too many attributes, they can be defined in an external file, but the path location is not recommended in an external file to reference the external properties attribute file, as shown below -->
<Property file = "build. properties"> </property>
<! -- The current directory of the file set dir = "." table, that is, the directory where build. XML is located. Includes = "**/*. Java" indicates that all files ending with. Java in the current directory and subdirectory are included. The ID can be referenced. -->
<Fileset id = "src. Path" dir = "." includes = "**/*. Java"> </fileset>


<! -- Initialize, create a folder, and write the Dir directory directly -->

<Target name = "init0">

<Delete dir = "build"> </delete>
<Mkdir dir = "build"/>
<Mkdir dir = "build/src"/>
<Mkdir dir = "build/classes"/>
<Mkdir dir = "build/Dist"/>
</Target>
<! -- Initialize, create a folder, and reference the Dir directory using $ {name} -->

<Target name = "init">

<Delete dir = "$ {build. dir}"> </delete>
<Mkdir dir = "$ {build. dir}"/>
<Mkdir dir = "$ {build. SRC}"/>
<Mkdir dir = "$ {build. classes}"/>
<Mkdir dir = "$ {build. Dist}"/>
</Target>


<Target name = "copysrc" depends = "init">
<Copy todir = "$ {build. SRC}">
<Fileset refID = "src. Path"> </fileset> <! -- Copy the file set and use refID to reference the file set with the id src. path -->
</Copy>
</Target>

<Target name = "complie" depends = "init">
<Javac destdir = "$ {build. classes}" srcdir = "$ {SRC. dir}"> </javac>
</Target>

<Target name = "jar" depends = "complie">
<Jar destfile = "build/Dist/Hello. Jar" basedir = "build/classes">
<Manifest>
<Attribute name = "Main-class" value = "org. Test. Ant. helloworld"/>
<Attribute name = "Build-by" value = "sawliet"/>
</Manifest>
</Jar>
</Target>

<Target name = "execute" depends = "jar">
<Echo> perform the execution based on the classname of the class path. </echo>
<Java classname = "$ {execute. Class}" classpath = "$ {build. classes}">
<Arg value = "Jack"/>
<Arg value = "Tom"/>
<Arg value = "Steven"/>
</Java>

<Echo> jar-based execution </echo>
<Java jar = "$ {build. Dist}/$ {jar. name}" fork = "true"> <! -- Use the built-in JDK compilation instead of ant Compilation -->
<Arg value = "Jack"/>
<Arg value = "Tom"/>
<Arg value = "Steven"/>
</Java>
</Target>




<Target name = "test">

<Property environment = "env"> </property>
<Echo >$ {env. tomcat_home} </echo>
<Echo >$ {env. OS} </echo>
<Echo >$ {env. java_home} </echo>

</Target>

</Project>

Helloworld. Java

Package org. Test. ant;

Public class helloworld {

Public static void main (string [] ARGs ){

For (string Arg: ARGs)
{
System. Out. println ("hello --" + Arg );
}
}

}

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.