Kirill
Kirill

Reputation: 8311

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

I'm using maven-dependency-plugin:copy-dependencies to copy all dependencies into target/dependency directory. My pom.xml is:

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

version of plugin is latest: 3.1.2 (defined in parent pom).

This definition is working fine with one exception: it copies all test dependencies into target directory, where I need only runtime dependencies required for running target jar.

I tried to exclude it usgin <excludeScope> configuration like described in the documentation:

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

But it makes the build failing with message:

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

I don't really understand why excluding the test scope will exclude everything, since without excluding test scope, the target directory contains all runtime dependencies too (along with test deps).

What could be the problem with excluding test dependencies? How to do it correctly?

PS: Please don't suggest me using assembly or other fat-jar plugins here, since I'm copying dependency jars intentionally for Docker image build optimization: one layer for dependencies, another for jar, where dependencies layer is always cached until any dependency changed:

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

Upvotes: 4

Views: 5661

Answers (2)

Dev
Dev

Reputation: 5

To exclude dependencies which are under test scope is to use includeScope runtime instead of the excludeScope as the plugin documentation for test means 'everything'

Upvotes: 0

J Fabian Meier
J Fabian Meier

Reputation: 35805

The solution is probably in the includeScope description:

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.

This means I would try with <includeScope>runtime</includeScope>.

Upvotes: 3

Related Questions