Question

I have a Java web app and am using Ant to produce the WAR file for me. Here is my project structure:

src/main/java/
    <Java sources>
src/main/config/
    web.xml
    mvc-dispatcher-servlet.xml
    pages/
        home.jsp
        cool.jsp
lib/main/
    <Libs>
gen/
    bin/
    dist/

I need a Java WAR produced with the following directory structure:

myapp.war/
    META-INF/
    WEB-INF/
        web.xml
        mvc-dispatcher.xml
        pages/
            home.jsp
            cool.jsp
        classes/
            <Compiled Java binaries>
        lib/
            <All lib JARS>

Here is my WAR task:

<target name="package" depends="compile">
    <war destfile="gen/dist/myapp.war" webxml="src/main/config/web.xml"
            basedir="gen/bin"
            includes="src/main/config">
        <classes dir="gen/bin" />
        <lib dir="lib/main" />
    </war>
</target>

When I run this target, it produces a WAR with the following structure:

myapp.war/
    META-INF/
    WEB-INF/
        web.xml
        classes/
            <Compiled Java binaries>
        lib/
            <All lib JARS>

Notice how mvc-dispatcher-servlet.xml and the entire pages directory is missing? What am I doing wrong?

Was it helpful?

Solution

You are seeking for webinf element. See ant manual for more details.

The nested webinf element specifies a FileSet. All files included in this fileset will end up in the WEB-INF directory of the war file. If this fileset includes a file named web.xml, the file is ignored and you will get a warning.

Use following snippet that combines several use cases.

<war destfile="${build.dir}/${ant.project.name}_${version}.war" webxml="${web.dir}/WEB-INF/web.xml">
    <webinf dir="${web.dir}/WEB-INF">
        <exclude name="web.xml"/>
        <exclude name="classes/**"/>
        <exclude name="lib/**"/>
        <exclude name="flex/services-config.xml"/>
    </webinf>
    <lib dir="${war.web-inf.dir}/lib"/>
    <lib file="${build.dir}/*.jar"/>
    <fileset dir="${web.dir}">
        <include name="img/*"/>
    </fileset>

OTHER TIPS

You might want to consider using the zip ant task instead of war. You can copy all classes/libraries in a folder in the structure you require and zip the content into a .war file. This deploys as a war without any issues as long as web.xml is in the right place in the war.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top