Reputation: 2771
Today I wanted to perform this task, and I run across some issues in the way. So I present here my problem and the solution I found. Perhaps somebody knows a simpler solution!
The problem was this: I was trying to build a distribution package of a Java project which is built with Maven2. In a previous step, several zip files all containing a file named manifest.xml in the root were generated, and I wanted to modify this XML file in all this ZIP files. This is a scheme:
package-file-1.zip
|- contents(DIR)
\- manifest.xml
package-file-2.zip
|- contents(DIR)
\- manifest.xml
Upvotes: 0
Views: 2284
Reputation: 2771
This example modifies the zip files in ${zip.sourcedir}
replacing the string &
with &
in the file manifest.xml of all this zip files and places the modified zip files in the directory target
.
For that, it uses the maven-antrun-plugin
including the for
and var
tasks from the antcontrib
tasks(http://ant-contrib.sourceforge.net). This permits to unzip the contents of every zip file into a separate directory. Note also the use of the task basename
to extract the name of the zip files out of their path.
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>copy-and-repair-zips</id>
<phase>initialize</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="maven.plugin.classpath"/>
<for param="filepath">
<path>
<fileset dir="${zip.sourcedir}" includes="**/*.zip"/>
</path>
<sequential>
<var name="for.filename" unset="true" />
<basename property="for.filename" file="@{filepath}" />
<unzip src="@{filepath}" dest="target/repair-temp/${for.filename}" encoding="UTF8" />
<replace file="target/repair-temp/${for.filename}/manifest.xml" token="&" value="&amp;" encoding="UTF8" />
<zip basedir="target/repair-temp/${for.filename}" destfile="target/${for.filename}" encoding="UTF8" />
</sequential>
</for>
</tasks>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>1.0b3</version>
<exclusions>
<exclusion>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
In order to write this solution, I got the needed knowledge from this URLs:
Modify a JAR(or ZIP) file using Ant: How do I modify a file in a jar file using ANT?
Unzip multiple files into separate directories: http://grokbase.com/t/ant.apache.org/user/2004/01/re-how-to-unzip-multiple-file-into-separate-directories/122a5ezxhh6eolf5enkrdfgryika
Use ant-contrib in Maven2: http://docs.codehaus.org/display/MAVENUSER/Antrun+Plugin
net/sf/antcontrib/antlib.xml
instead of net/sf/antcontrib/antcontrib.properties
to be able to use the for
taskUse the ant-contrib var
task: http://www.jguru.com/forums/view.jsp?EID=1374074
After posting the question, I was able to find a couple of questions related, that could help if one is having problems implementing a similar thing:
Upvotes: 1