38

I can unpack zip file via the maven-dependency plugin, but currently I have the problem that inside that zip file other zip files are include and I need to unpack them as well. How can I do this?

Sergio
  • 3,317
  • 5
  • 32
  • 51
khmarbaise
  • 92,914
  • 28
  • 189
  • 235

4 Answers4

43

You can unzip any files using ant task runner plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <id>prepare</id>
            <phase>validate</phase>
            <configuration>
                <tasks>
                    <echo message="prepare phase" />
                    <unzip src="zips/archive.zip" dest="output/" />
                    <unzip src="output/inner.zip" dest="output/" />
                    <unzip dest="output">
                      <fileset dir="archives">
                        <include name="prefix*.zip" />
                      </fileset>
                    </unzip>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
24

Using ANT is not cool any more ;)

http://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-artifacts.html

Sample code for unpacking zip (archive.zip) file:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>process-resources</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>foo</groupId>
                        <artifactId>archive</artifactId>
                        <version>1.0-SNAPSHOT</version>
                        <type>zip</type>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

File archive.zip should be installed into maven repository first. For example with task Attach artifact org.codehaus.mojo:build-helper-maven-plugin:build-helper:attach-artifact

Arie Laxed
  • 103
  • 1
  • 4
MariuszS
  • 30,646
  • 12
  • 114
  • 155
  • 18
    But that only unzips artefacts, not arbitrary files. – Ondra Žižka Jul 20 '12 at 11:37
  • @OndraŽižka With maven you can treat any "arbitrary" file as artifact, just look at `build-helper:attach-artifact`. – MariuszS Jan 11 '14 at 00:14
  • 2
    @MariuszS how does one do it with an arbitrary file on the local filesystem? – tojofo Mar 20 '14 at 05:53
  • 4
    Maybe ant is not cool, but requires much fewer lines and produces the same result. Also, if you attach an artifact with build-helper, it generates many other unpleasant side effects – Alexander Pogrebnyak Jul 07 '14 at 17:34
  • When building with Eclipse Juno, the `unpack` goal is not supported by m2e 1.3.1. – Stephan Dec 29 '15 at 15:01
  • In general binary (zip) files should be stored outside VCS (GIT) repository, for example can be deployed to artifact repository (nexus). After this, you can unpack such artifact as described above. – MariuszS Apr 21 '16 at 14:04
  • 1
    I did the test and this WON'T unzip inner zip files. It's better to use the Ant plugin as described in the other answer. – Sergio Jun 07 '16 at 14:04
  • With `maven-dependency-plugin:unpack` on a zip generated in another module of the same project, I need 2 builds for changes to go through (probably `unpack` takes the zip from the local repository which is updated in a later phase). This problem obviously doesn't occur with [TrueZip's answer](https://stackoverflow.com/a/30484505/7251133) (tested) or certainly with Ant (but not tested). – Florian H. Apr 06 '22 at 09:30
10

TrueZIP Maven Plugin also works well. Sample config:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>truezip-maven-plugin</artifactId>
    <version>1.2</version>
    <executions>
        <execution>
            <id>copy-package</id>
            <goals>
                <goal>copy</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <verbose>true</verbose>
                <fileset>
                    <directory>outer.zip</directory>
                    <outputDirectory>${project.build.directory}/outer</outputDirectory>
                </fileset>
                <fileset>
                    <directory>${project.build.directory}/outer/inner.zip</directory>
                    <outputDirectory>${project.build.directory}/inner</outputDirectory>
                </fileset>
            </configuration>
        </execution>
    </executions>
</plugin>

Official examples

sjoblomj
  • 110
  • 2
  • 10
palacsint
  • 28,416
  • 10
  • 82
  • 109
1

You can also use the plugin dependencies. There is a goal to unpack dependencies (see http://maven.apache.org/plugins/maven-dependency-plugin/unpack-dependencies-mojo.html)

benzen
  • 6,204
  • 4
  • 25
  • 37
  • 3
    this response is NOT correct, the question is about a file with an inner zip, NOT a dependency. – Sergio Jun 07 '16 at 13:48