java - maven-dependency-plugin 不能排除测试范围依赖

标签 java maven maven-dependency-plugin

我正在使用 maven-dependency-plugin:copy-dependencies将所有依赖项复制到 target/dependency目录。我的pom.xml是:

          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
              </execution>
            </executions>
          </plugin>

插件版本最新:3.1.2 (在父 pom 中定义)。

此定义工作正常,但有一个异常(exception):它将所有测试依赖项复制到 target 中目录,我只需要运行目标 jar 所需的运行时依赖项。

我试图排除它 usgin <excludeScope>配置如 the documentation 中所述:

<configuration>
    <excludeScope>test</excludeScope>
</configuration>

但这会使构建失败并显示消息:

[INFO] --- maven-dependency-plugin:2.10:copy-dependencies (copy-dependencies) @ app ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.006 s
[INFO] Finished at: 2021-02-15T10:32:26+03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.10:copy-dependencies (copy-dependencies) on project app:  Can't exclude Test scope, this will exclude everything. -> [Help 1]

我真的不明白为什么排除测试范围会排除所有内容,因为不排除 test范围,目标目录也包含所有运行时依赖项(以及测试部门)。

排除测试依赖项可能有什么问题?如何正确操作?

PS:请不要建议我在这里使用程序集或其他 fat-jar 插件,因为我有意复制依赖 jar 以进行 Docker 镜像构建优化:一层用于依赖​​,另一层用于 jar,其中依赖层始终缓存直到任何依赖项更改:

COPY target/dependency  /usr/lib/app/lib
COPY target/${JAR_FILE} /usr/lib/app/target.jar

最佳答案

解决方案可能在 includeScope 中描述:

Scope to include. An Empty string indicates all scopes (default). The scopes being interpreted are the scopes as Maven sees them, not as specified in the pom. In summary:

  • runtime scope gives runtime and compile dependencies,
  • compile scope gives compile, provided, and system dependencies,
  • test (default) scope gives all dependencies,
  • provided scope just gives provided dependencies,
  • system scope just gives system dependencies.

这意味着我会尝试使用 <includeScope>runtime</includeScope> .

关于java - maven-dependency-plugin 不能排除测试范围依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66204340/

相关文章:

java - 是每个类一个记录器还是一组被整个应用程序访问的记录器?

java - 如何排除打包为应用程序的 uber jar 的 jar 文件

maven-2 - mvn依赖:copy-dependencies removes the versions

maven-dependency-plugin - 在复制依赖项之前删除旧的工件版本

maven-2 - 使用 Maven 强制重新下载发布依赖项

java - 为什么没有窗口关闭就会出现问题

java - 为什么Java中没有ceil(float)?

java - 如何针对不同类型的CRUD操作实现Hibernate拦截器并获取已修改的java对象的类名?

maven - 使用本地代理从受密码保护的 Nexus 存储库下载依赖项

eclipse - 启动 gwt 时出现编译器错误 :compile with gwt-maven-plugin