简体   繁体   中英

How to include Classpath Jars in a war file using Ant

I am creating an ant script to build and deploy a project. My project structure is as follows:

 Projectname
--src
--tst
--lib ( I have 5 jars here)
--resources
--WebContent
      --META-INF
      --WEB-INF
            --lib (I have 3 jars here)
            --properties
            --templates
            --stylesheets
            --web.xml

My requirement is I have to write an ant script to build the project and create a war file in the staging environment (not local). The war should contain all the jar files available in the classpath ( classpath has more than 50 jars). But when I run my ant script it shows me only 8 jars, that are available in lib folders and does not include the jars from classpath.

How to add classpath while creating a War file? Below is the script I have written for creating a war.

<target name="war" description="Bundles the application as a WAR file" depends="build">
            <echo> === PACKAGE WAR ====== </echo>
             <delete dir="target" failonerror="false"/>
             <mkdir dir="target"/>
            <war destfile="target/test.war" needxmlfile="false" >
                <fileset dir="WebRoot">
                    <include name="**/*.*"/>
                </fileset>
                <lib dir ="lib">
                    <include name="**/*.*"/>
                </lib>
<classes dir="WebContent/WEB-INF/classes"/>
            </war>
</target>

Classpath is as follows in the ant script.

 <property name="ExternalJars.location" value="../ExternalJars"/>
 <path id="Services.classpath">
<fileset dir="lib">
        <include name="**/*.jar"/>
    </fileset>
</path>

尝试添加任务:

<war destfile="target/test.war" needxmlfile="false" > <fileset dir="WebRoot"> <include name="**/*.*"/> </fileset> <path> <path refid="Services.classpath"/> </path> <lib dir ="lib"> <include name="**/*.*"/> </lib> <classes dir="WebContent/WEB-INF/classes"/> </war>

Where is located your 50 jars? Try to add your classpath folder like a in your task. Something like this:

<war destfile="target/test.war" needxmlfile="false" > <fileset dir="WebRoot"> <include name="**/*.*"/> </fileset> <fileset dir="${classpath_folder}"> <include name="*.jar"/> </fileset> <lib dir ="lib"> <include name="**/*.*"/> </lib> <classes dir="WebContent/WEB-INF/classes"/> </war>

I found a solution by using mappedresources which a bit uncomfortable but works

<war destfile="target/test.war" needxmlfile="false" >
    <fileset dir="WebRoot" />
    <lib dir ="lib" />
    <classes dir="WebContent/WEB-INF/classes"/>

    <mappedresources>
        <path refid="Services.classpath"/>
        <chainedmapper>
            <flattenmapper />
            <globmapper from="*.jar" to="WEB-INF/lib/*.jar" />
        </chainedmapper>
    </mappedresources>
</war>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM