Simple process for packaging war with Ant

Source: Internet
Author: User

The original text is easy to understand and has little content. It is recommended to read English directly. Here is a simple translation for future reference.

 

In this example, I use the Spring SimpleFormController example to explain the war construction process, as shown in the structure of the application.

 

 

All classes in src need to be compiled and placed under the build/classes directory. The war package to be built must be placed in the dist directory.

 

Therefore, the first step is to create the build/classes and dist directories and use init to complete these tasks:

[Html]
<Target name = "init">
<Mkdir dir = "build/classes"/>
<Mkdir dir = "dist"/>
</Target>

Next, compile the class in the src directory to the build/classes directory. Before compiling, set classpath as follows:

[Html]
<Path id = "compile. classpath">
<Fileset dir = "WebContent/WEB-INF/lib">
<Include name = "*. jar"/>
</Fileset>
</Path>

Next, compile with the javac command. Make sure that the relevant directories have been created during compilation. Therefore, the compilation depends on init, as shown below:

[Html]
<Target name = "compile" depends = "init">
<Javac destdir = "build/classes" debug = "true" srcdir = "src">
<Classpath refid = "compile. classpath"/>
</Javac>
</Target>

Next, you can create a war package and use the war Command provided by ant to complete this work:

[Html]
<Target name = "war" depends = "compile">
<War destfile = "dist/AntExample. war" webxml = "WebContent/WEB-INF/web. xml">
<Fileset dir = "WebContent"/>
<Lib dir = "webcontents/WEB-INF/lib"/>
<Classes dir = "build/classes"/>
</War>
</Target>
War Command details reference: http://ant.apache.org/manual/Tasks/war.html

 

Finally, some cleanup work may be required, such:

[Html]
<Target name = "clean">
<Delete dir = "dist"/>
<Delete dir = "build"/>
</Target>

The following is a complete build script:

[Html]
<? Xml version = "1.0"?>
<Project name = "AntExample1" default = "war">
 
<Path id = "compile. classpath">
<Fileset dir = "WebContent/WEB-INF/lib">
<Include name = "*. jar"/>
</Fileset>
</Path>

<Target name = "init">
<Mkdir dir = "build/classes"/>
<Mkdir dir = "dist"/>
</Target>

<Target name = "compile" depends = "init">
<Javac destdir = "build/classes" debug = "true" srcdir = "src">
<Classpath refid = "compile. classpath"/>
</Javac>
</Target>

<Target name = "war" depends = "compile">
<War destfile = "dist/AntExample. war" webxml = "WebContent/WEB-INF/web. xml">
<Fileset dir = "WebContent"/>
<Lib dir = "webcontents/WEB-INF/lib"/>
<Classes dir = "build/classes"/>
</War>
</Target>

<Target name = "clean">
<Delete dir = "dist"/>
<Delete dir = "build"/>
</Target>

</Project>

If there are attachments, I cannot find the place where the attachments are to be uploaded. I am not familiar with the related operations. Click the original file to download the attachment.

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.