将Maven依赖项解压缩到类中,保持传递依赖性

时间:2015-01-30 21:20:33

标签: java maven jar dependencies pom.xml

我正在尝试将maven依赖jar的内容解压缩到我的classes文件夹中,同时包含传递依赖项。我也不想解压所有项目的依赖项。只有一个会好,如果我能做到这一点就更好。找到了类似的解决方案,但没有解决我的确切问题。

示例主项目Pom:

.
.
.
<dependencies>
  <dependency>
    <groupId>com.test.dep</groupId>
    <artifact>first-dependency</artifact>
  </dependency>
  <dependency>
    <groupId>com.test.dep</groupId>
    <artifact>second-dependency</artifact>
  </dependency>
</dependencies>
.
.
.

第二依赖性示例Pom:

.
.
.
<dependencies>
  <dependency>
    <groupId>com.test.dep</groupId>
    <artifact>third-dependency</artifact>
  </dependency>
  <dependency>
    <groupId>com.test.dep</groupId>
    <artifact>fourth-dependency</artifact>
  </dependency>
</dependencies>
.
.
.

我希望将second-dependency解压缩到嵌套在classes下的target文件夹中,并且还希望它依赖于任何工件(third-dependencyfourth-dependency)仍然包含在我的lib文件夹中(未解包)。

我尝试了以下内容(不包括我的依赖项中的工件):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>generate-resources</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>com.test.dep</groupId>
                        <artifactId>second-dependency</artifactId>
                        <type>jar</type>
                        <overWrite>true</overWrite>
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        <includes>**/*</includes>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

这包括我second-dependency文件夹中classes的内容,但在我的主项目third-dependency目录中未包含fourth-dependencylib

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试使用以下插件配置,基于所描述的dependency:unpack-dependencies参数,而不是:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>unpack</id>
      <phase>generate-resources</phase>
      <goals>
        <goal>unpack</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.directory}/classes</outputDirectory>
        <includeArtifactIds>second-dependency</includeArtifactIds>
      </configuration>
    </execution>
  </executions>
</plugin>

我不确定你的用例。

但是如果你想构建jar,那听起来像maven-shade-plugin的用例。这个插件能够将项目本身的类和资源以及一组指定的工件(依赖项)打包到一个jar中。

只需定义工件本身(&#34; $ {project.groupId}:$ {project.artifactId}&#34;)和&#34;第二个依赖关系&#34;被包括。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>2.1</version>
  <configuration>
    <artifactSet>
      <includes>
        <include>${project.groupId}:${project.artifactId}</include>
        <include>com.test.dep:second-dependency</include>
      </includes>
    </artifactSet>
  </configuration>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
    </execution>
  </executions>
</plugin>