Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rename files when included in a jar by ant's jar task?

Tags:

jar

ant

mapper

I want to put a set of files that look like this into a jar:

yay/my.jar
boo/my.jar
foo/my.jar
bar/my.jar

In the process, I want all of them renamed as follows:

yay_my.jar
boo_my.jar
foo_my.jar
bar_my.jar

I was hoping to use a mapper to accomplish this, but the fileset elements I am using and the jar task don't seem to support it anywhere.

How do I apply a mapper when building a jar, or else how can I perform a transformation like this? I want to avoid copying all the files to the directory structure I want and making duplicates all over the place, which is how our build system works now.

like image 490
skiphoppy Avatar asked Sep 22 '09 14:09

skiphoppy


People also ask

Can you rename a jar file?

To demonstrate with project that has existing jar file, go to project structure->artifacts and select jar to rename. right-click and select rename.

What is a jar task?

The Jar task checks whether you specified package information according to the versioning specification. Please note that the ZIP format allows multiple files of the same fully-qualified name to exist within a single archive.


2 Answers

You can use a zipfileset with a fullpath attribute to rename the filename in the jar:

<jar destfile="newjar.jar">
    <zipfileset dir="yay" includes="my.jar" fullpath="yay_my.jar"/>
    <zipfileset dir="boo" includes="my.jar" fullpath="boo_my.jar"/>
    <!-- etc. -->
</jar>

You can't use a mapper with this technique though, you'll have to list each jar file explicitly. If you can assume that every file is named my.jar, and they are all in an immediate child directory, you can use the subant target to glob them all up:

<target name="glom">
    <subant genericantfile="${ant.file}" target="update-jar">
        <dirset dir="." includes="*"/>
    </subant>
</target>

<target name="update-jar">
    <basename file="${basedir}" property="dirname"/>
    <property name="path" value="${dirname}_my.jar"/>
    <jar destfile="../newjar.jar" update="yes">
        <zipfileset dir="." includes="my.jar" fullpath="${path}"/>
    </jar>
</target> 
like image 113
Jason Day Avatar answered Oct 06 '22 23:10

Jason Day


Update: you probably actually want the copy task rather than move, but the regexp mapper works the same for both copy and move.

The following regexp will copy all jars in the jars directory to jars_out, mapping [folder]/[file].jar to [folder]_[file].jar.

<copy todir="./jars_out">
  <fileset dir="jars"/>
  <mapper type="regexp" from="([^/\\]*)[/\\](.*)\.jar$$" to="\1_\2.jar"/>
</copy>

The regexp mapper needs an appropriate regexp implementation jar on the classpath to work. Various implementations are available:

  • java.util.regex package of JDK 1.4 or higher
  • jakarta-regexp
  • jakarta-oro
like image 35
Rich Seller Avatar answered Oct 06 '22 22:10

Rich Seller