Ant script: Prevent duplication of JAR in javac-classpath war-lib

前端 未结 2 602
醉话见心
醉话见心 2020-12-31 13:53

I have a ANT script and I have a lot of duplicated path to same set JAR files. But there is so many double wording in the classpath and also in the war element.



        
相关标签:
2条回答
  • 2020-12-31 14:36

    Another solution, possibly 'not the best' will be to place required jar file in WEB-INF/lib, and then set the classpath from there.

    <path id="compile.classpath">
        <fileset dir="${lib.dir}" includes="*.jar"/>
    </path>
    

    When its time to build the war, you need not worry about the <lib> at all, as jars are already placed in WEB-INF/lib folder.

    <war destfile="${dist.dir}/${project.name}.war" webxml="${web.dir}/WEB-INF/web.xml">
            <fileset dir="${web.dir}"/>
            <classes dir="${build.dir}/classes"/>
        </war>
    
    0 讨论(0)
  • 2020-12-31 14:52

    Since Ant 1.8.0 there is a new resource collection - mappedresources that can be used in place of the war task lib element.

    So, the task might look like this (pretty much straight from the docs):

    <war ... >
        <mappedresources>
            <restrict>
               <path refid="my.classpath"/>
               <type type="file"/>
            </restrict>
            <chainedmapper>
              <flattenmapper/>
              <globmapper from="*" to="WEB-INF/lib/*"/>
            </chainedmapper>
        </mappedresources>
    </war>
    

    This feature was added to resolve a long-standing feature request to make the task flatten jars when deploying to WEB-INF/lib.

    previous answer:

    Although you can't easily convert a path to a fileset with vanilla Ant, you can go the other way. So one option would be to define your jars in a fileset, and derive the path from it. Something like this perhaps:

    <fileset id="my.fileset" dir="${basedir}">
        <include name="folderA/subFolderA/1.0/A.jar"/>
        <include name="folderC/subFolderB/1.0/B.jar"/>
        <include name="folderF/subFolderZ/2.0/Z.jar"/>
        <include name="moreFolderF/subFolderZ/2.0/additionFile.jar"/>
        <include name="moreFolderF/subFolderZ/2.0/additionRuntimeFile.jar"/>
    </fileset>
    
    <path id="my.classpath">
        <fileset refid="my.fileset" />
    </path>
    
    <!-- javac stays the same -->
    
    <war ...>
        <lib refid="my.fileset" />
    </war>
    

    Another possibility is to use the ant-contrib pathtofileset task.

    0 讨论(0)
提交回复
热议问题