在Maven中解压依赖

29
我在maven中有以下的依赖关系。
<dependency>
  <groupId>org.hyperic</groupId>
  <artifactId>sigar-dist</artifactId>
  <version>1.6.5.132</version>
  <type>zip</type>
</dependency>

这将在我的存储库中创建 sigar-dist-1.6.5.132.zip 文件。 我在这里看到了一个相关的问题,但我仍然无法让它工作。

我该如何解压sigar-dist.zip并将内容放置在我的项目目录中?我需要执行哪个mvn命令才能使其工作?

2个回答

47

你可以使用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>

参考资料:


1
感谢您的快速回复。我尝试了这个方法,在执行dependency:unpack-dependencies和mvn install之后,出现了[ERROR] BUILD ERROR [INFO] ------------------------------------------------------------------------ [INFO] 'unpack-sigar' was specified in an execution, but not found in the plugin的错误提示。 - Shervin Asgari
这个解包后的代码是被编译并链接到项目中替代了原始依赖项吗?还是只是解包了,以便您可以看到代码? - yucer
最好提到,此插件标签应该在 build 标签下面。 - Sireesh Yarlagadda
@yucer,这取决于您将unpack-dependencies目标绑定到的阶段以及未打包内容的outputDirectory。我认为原始依赖项没有执行“unlink”。如果您不希望使其具有传递性,则可以在引用中添加<scope>provided</scope> - Ilya Serbis

4

关于@Sean Patrick Floyd的回答的跟进:

这是我的最终pom.xml文件,用于下载和解压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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接