Unpack inner zips in zip with Maven

2019-01-08 21:11发布

问题:

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?

回答1:

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>


回答2:

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



回答3:

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



回答4:

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)