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

maven-dependency-plugin can't exclude test-scope dependencies

我正在使用 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 中定义)。

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

我尝试使用 <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 插件,因为我是为了 Docker 图像构建优化而故意复制依赖项 jar:一层用于依赖​​项,另一层用于 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>

排除在测试范围内的依赖项是使用 includeScope 运行时而不是 excludeScope 作为测试手段的插件文档 'everything'