Packaging Web foreground programs with Ant

Summary

This article uses a simple Web project as an example to describe how to package a Web foreground program using ANT.Contains three parts: file copy, compile, and package; complete these three parts and you get a War that runs under Tomcat.

Installation of ANT, setting of environment variables, etc. are omitted.There are many articles on the Internet.

Dead work

First, create a simple Web program that displays "Hello ANT!!!" through a simple Web page (index.html).Programs rely on third-party Jar packages GSON.The basic program structure is as follows:



The ant script build.xml is placed in the tools/build directory, and the war packages generated by packaging and compiling are placed under runtime.

Write build.xml file

<?xml version="1.0" encoding="utf-8" ?>
<project name="WebAntTest" default="create_war" basedir="../../">
    <!--Define the current time-->
    <tstamp>
        <format property="current_date" pattern="yyyy_MM_dd_HH_mm_ss" locale="en"></format>
    </tstamp>

    <!--Define Dependencies jar Path to package-->
    <path id="depend_lib_path">
        <fileset dir="WebContent\WEB-INF\lib">
            <include name="*.jar"></include>
        </fileset>
    </path>

    <!--Delete old packaged files(contain war Packaging and Compiling Files)-->
    <target name="clean">
        <delete verbose="true" includeemptydirs="true">
            <fileset dir="build">
                <include name="**/*"/>
            </fileset>
        </delete>
        <delete verbose="true" includeemptydirs="true">
            <fileset dir="runtime">
                <include name="**/*"/>
            </fileset>
        </delete>
    </target>

    <!--Compile Generation class file-->
    <target name="compile" depends="clean">
        <javac srcdir="src" destdir="build" includeantruntime="on">
            <classpath refid="depend_lib_path"></classpath>
        </javac>
    </target>

    <!--hit war package-->
    <target name="create_war" depends="compile">
        <war destfile="runtime/AntWeb.war" webxml="WebContent/WEB-INF/web.xml">
            <fileset dir="WebContent">
                <exclude name="**.jar"/>
                <exclude name="**.class"/>
            </fileset>
            <lib dir="WebContent\WEB-INF\lib"></lib>
            <classes dir="build"/>
        </war>
    </target>   
</project>

test run

Switch the current path to the tools/build directory, execute the ant command to complete the packaging operation, and generate a war package in the runtime directory:



Packaging process:



In order to package as a timed task, you can also add a batch build.bat:

::Start Packager
cd /d %cd%

start "Start Packager..." ant. 

Publish War Package

Put the packaged war file under tomcat's webApps and run Tomcat.Accessed through the web page, it can be displayed normally.



Tags: xml Tomcat encoding

Posted on Tue, 05 May 2020 09:00:28 -0400 by Frapster