【问题标题】:Unpack Maven dependency into classes, keeping transitive dependencies将 Maven 依赖解包到类中,保持传递依赖
【发布时间】:2023-03-20 08:43:02
【问题描述】:

我正在尝试将 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 解压缩到嵌套在target 下的classes 文件夹中,并且还希望它所依赖的任何工件(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>

这确实在我的classes 文件夹中包含second-dependency 的内容,但在我的主要项目lib 目录中不包含third-dependencyfourth-dependency

有什么想法吗?

【问题讨论】:

    标签: java maven jar dependencies pom.xml


    【解决方案1】:

    尝试使用以下插件配置,基于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 中。

    只需定义工件本身(“${project.groupId}:${project.artifactId}”)和要包含的“第二个依赖项”。

    <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>
    

    【讨论】:

    • 那种工作。我真正想做的是将所有依赖项包含在第二个依赖项的 pom 中,将第二个依赖项解压缩到我的类文件夹中,同时不包括我的 lib 文件夹中的第二个依赖项。
    猜你喜欢
    • 2018-07-06
    • 1970-01-01
    • 2019-08-23
    • 2019-10-29
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多