ant : jar and zipfileset - copy files from one JAR

2019-07-06 10:49发布

问题:

I am currently doing this:

<jar update="yes"
     jarfile="${pwd}/dist/${release}_installer.jar">
   <zipfileset src="${pwd}/dist/app.jar" includes="com/izforge/izpack/panels/**"/>
   <zipfileset src="${pwd}/dist/app.jar" includes="com/xyz/img/logo.png"/>
</jar>

My existing installer JAR gets updated to include the files as needed, extracted from the app JAR.

So far, so good.

However, I want to modify the behaviour such that the path of the image file is different than what is being copied from:

Currently:

com/izforge/izpack/panels/MyIzPanel.class
com/xyz/img/logo.png

What I want:

com/izforge/izpack/panels/MyIzPanel.class
blah/img/logo.png

So I need to copy the files, but use <zipfileset> and <jar> in such a way that I can modify the directory structure.

Is there a way to do this, apart from unzipping the entire contents copying file and then zipping it back up again?


EDIT:

Link to earlier related question: ant task to remove files from a jar

回答1:

You can use the fullpath attribute:

<zipfileset src="${pwd}/dist/app.jar"
    includes="com/xyz/img/logo.png" fullpath="blah/img/logo.img"/>

If you need to copy several files you may want to have a look at the prefix attribute, e.g.:

<zipfileset src="${pwd}/dist/app.jar"
    includes="**/*.png" prefix="blah/img"/>


回答2:

In order to modify the directory structure within the archive on the fly you can use the task in combination with <mappedresources>, eg:

<jar file="target.jar" update="true">
  <mappedresources>
    <zipfileset src="source.jar">
      <include name="com/xyz/img/*.png"/>
    </zipfileset>
    <mapper type="glob" from="com/xyz/img/*.png" to="bla/img/*.png" />
  </mappedresources>
</jar>


回答3:

You should probably look into zipgroupfileset as explained here.