Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

war

ant

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.

<path id="my.classpath">
  <pathelement location="folderA/subFolderA/1.0/A.jar"/>
  <pathelement location="folderC/subFolderB/1.0/B.jar"/>
  <pathelement location="folderF/subFolderZ/2.0/Z.jar"/>
  <pathelement location="compile/subFolderX/1.0/onlyForJavac.jar"/>
</path>
....
<javac ...>
 <classpath refid="my.classpath" />
</javac>
....
<war ...>
 <lib file="folderA/subFolderA/1.0/A.jar"/>
 <lib file="folderC/subFolderB/1.0/B.jar"/>
 <lib file="folderF/subFolderZ/2.0/Z.jar"/>
 <lib file="moreFolderF/subFolderZ/2.0/additionFile.jar"/>
 <lib file="moreFolderF/subFolderZ/2.0/additionRuntimeFile.jar"/>
</war>

I want to summary them into ONE list which is easier to keep update.

But I am blocked as I have no idea how to share a path-like-structure with a fileset-like-structure.

like image 299
Dennis C Avatar asked Feb 02 '23 21:02

Dennis C


1 Answers

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.

like image 179
martin clayton Avatar answered Apr 27 '23 22:04

martin clayton