Unpack inner zips in zip with Maven
Asked Answered
I

4

39

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?

Irrational answered 16/7, 2010 at 10:51 Comment(0)
F
44

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>
Freestone answered 16/7, 2010 at 11:6 Comment(5)
I take it that this is put into your pom.xml file?Inoculum
Yes, this is a part of your Maven pom.xml file.Wilie
How can you in this example unzip a file which matches a regular expression? E.g. <unzip src="zips/archive[0-9].zip" dest="output/" />Madea
Try with the wildcard: <unzip src="zips/archive*.zip" dest="output/" />Wilie
Nope this will not work. I had better chance with a fileset instead: <unzip dest="output"> <fileset dir="zips"> <include name="archive*.zip" /> </fileset> </unzip>Madea
T
23

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

Telescopium answered 4/10, 2011 at 12:55 Comment(8)
But that only unzips artefacts, not arbitrary files.Attentive
@OndraŽižka With maven you can treat any "arbitrary" file as artifact, just look at build-helper:attach-artifact.Telescopium
@Telescopium how does one do it with an arbitrary file on the local filesystem?Clung
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 effectsMicrominiaturization
When building with Eclipse Juno, the unpack goal is not supported by m2e 1.3.1.Puli
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.Telescopium
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.Cloistral
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 (tested) or certainly with Ant (but not tested).Metallography
S
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

Shaynashayne answered 27/5, 2015 at 13:50 Comment(2)
Codehaus is shutting down, so the links now go to their "we are reorganizing" page.Steersman
@Shaynashayne what is the license of above plugin, I can't find it in their docsFraenum
S
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)

Saiva answered 1/3, 2011 at 14:57 Comment(1)
this response is NOT correct, the question is about a file with an inner zip, NOT a dependency.Cloistral

© 2022 - 2024 — McMap. All rights reserved.