4

Question: is there any way in Maven (without resorting to an ant plugin) to unzip a file, cd into the directory, remove the file, and the rezip it, all as part of the build?

This is necessary as it is a complex build and also do not want to have to use gradle to accomplish this task.

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
user1974753
  • 1,359
  • 1
  • 18
  • 32
  • 3
    Can you explain more why you need to do this? Is this ZIP generated? If so, by who and when? What file do you want to remove in it and why? – Tunaki Sep 26 '16 at 14:18
  • 1
    Possible duplicate of [Unpack inner zips in zip with Maven](http://stackoverflow.com/questions/3264064/unpack-inner-zips-in-zip-with-maven) – DimaSan Sep 26 '16 at 14:19
  • mvn dependency:unpack + maven-assembly-plugin for rezip – johnnymnmonic Sep 26 '16 at 14:59
  • You can take a look at maven-assembly-plugin. This can handle both unzip and repackaging ... – khmarbaise Sep 26 '16 at 15:24

1 Answers1

2

The requirement of unzipping, removing file and zipping again can also be met in one single step by the truezip-maven-plugin and its remove goal which:

Remove a set of files from an existing archive.

The official examples also cover this scenario.

Given the following snippet:

<properties>
    <archive>${project.basedir}/sample.zip</archive>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>truezip-maven-plugin</artifactId>
            <version>1.2</version>
            <executions>
                <execution>
                    <id>remove-a-file</id>
                    <goals>
                        <goal>remove</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <fileset>
                            <!-- note how the archive is treated as a normal file directory -->
                            <directory>${archive}</directory>
                            <includes>
                                <include>hello.txt</include>
                            </includes>
                        </fileset>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

And executing:

mvn clean package

The build will process the ${archive} file (in this case a sample.zip at the same level of the pom.xml file, that is, in the project.basedir directory) and remove from it the hello.txt file. Then rezip everything.

I just tested it successfully, you can even skip the properties section if not required. However, you should also carefully know that:

  • The zip file should not be under version control, otherwise it would create conflicts at each build
  • The behavior most probably should not be part of the default Maven build, hence good candidate for a Maven profile
  • the plugin replaces the original file, so if that was an issue you could firstly copy it to another location and then process it as above. To copy it, you could use the maven-resources-plugin and its copy-resources goal.
A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128