Unzip dependency in maven

2019-01-07 21:25发布

问题:

I have the following dependency in maven

<dependency>
  <groupId>org.hyperic</groupId>
  <artifactId>sigar-dist</artifactId>
  <version>1.6.5.132</version>
  <type>zip</type>
</dependency>

This creates sigar-dist-1.6.5.132.zip in my repository. I have seen this question here, however I still can't make it work.

How can I unzip the sigar-dist.zip and place the content in a directory in my project? What is the mvn call I have to do to make it work?

回答1:

You can do it with dependencies:unpack-dependencies:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.2</version>
    <executions>
      <execution>
        <id>unpack-sigar</id>
        <phase>package<!-- or any other valid maven phase --></phase>
        <goals>
          <goal>unpack-dependencies</goal>
        </goals>
        <configuration>
          <includeGroupIds>org.hyperic</includeGroupIds>
          <includeArtifactIds>sigar-dist</includeArtifactIds>
          <outputDirectory>
             ${project.build.directory}/wherever/you/want/it
             <!-- or: ${project.basedir}/wherever/you/want/it -->
          </outputDirectory>
        </configuration>
      </execution>
    </executions>
</plugin>

Reference:

  • Unpacking project dependencies
  • dependency:unpack-dependencies


回答2:

just a follow up on answer from @Sean Patrick Floyd

this is my final pom.xml to download and unpack tomcat

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.koushik.javabrains</groupId>
    <artifactId>tomcat</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>tomcat</name>

    <properties>
        <tomcat.version>8.0.27</tomcat.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>unpack-tomcat</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeGroupIds>org.apache.tomcat</includeGroupIds>
                            <includeArtifactIds>tomcat</includeArtifactIds>
                            <outputDirectory>
                                ${project.build.directory}
                                <!-- or: ${project.basedir}/wherever/you/want/it -->
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat</artifactId>
            <version>${tomcat.version}</version>
            <type>zip</type>
        </dependency>
    </dependencies>
</project>