31

I see maven-dependency-plugin does this; however, it seems to copy everything (including test jars) to the destination directory. Anyone know how to configure this plugin to exclude test jars?

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
Mike
  • 3,515
  • 10
  • 44
  • 67

3 Answers3

40

Mike answered their own question in a comment above. I think Mike's use case is similar to mine where I want to copy all of the jars I depend upon as well as my own jar in order to create a directory hierarchy sufficient to execute the program without including those dependencies directly into my own jar.

The answer to achieve this is:

<includeScope>compile</includeScope>

This directive goes into the section of the pom.xml for the maven-dependency plugin. For example:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.4</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <includeScope>compile</includeScope>
            </configuration>
        </execution>
    </executions>
</plugin>

excludeScope won't work because excluding test aborts the build and excludes all possible scopes. Instead the included scope needs to be adjusted.

fuzzyBSc
  • 823
  • 8
  • 7
  • 12
    Using `runtime` may be better yet, as it copies compile and runtime dependencies. For instance, if you use SLF4J logging, then the `slf4j-api` would be a compile time dependency, whereas the bridges (`jcl-over-slf4j`, `jul-to-slf4j`) and the backend (e.g. `slf4j-log4j12`) would be runtime dependencies. – zwets May 11 '17 at 11:05
  • 1
    Just a note that you can pass this on the command-line too (just like excludeScope): `mvn clean dependency:copy-dependencies -DincludeScope=runtime` – lmsurprenant Aug 13 '19 at 17:38
24

It is not clear if you wanted to exclude jars with test scope or test related jars (test classifier). In either case, there are two properties of dependency:copy-dependencies which can help you.

  • excludeClassifiers Comma Separated list of Classifiers to exclude. Empty String indicates don't exclude anything (default).
  • excludeScope Scope to exclude. An Empty string indicates no scopes (default).
Raghuram
  • 51,854
  • 11
  • 110
  • 122
  • 10
    Thanks for the suggestions, i use compile and it works. Thanks. – Mike Nov 29 '11 at 13:59
  • 4
    @Raghuram Mike here did raise a point, one cannot exclude scope test. see http://stackoverflow.com/questions/5850788/filter-dependencies-copied-by-mavens-copy-dependency – Dudi Jun 23 '14 at 07:49
  • I am using `` for provided, transitive dependencies don't get copied for the default scope, even though `` is false – Akash Agarwal Oct 23 '17 at 08:51
11

Documentation says: 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

According to my experience, if you just wanna run your classes with compile scoped dependencies, specified in project pom.xml file, you must add -DincludeScope=runtime java system setting, like so:

mvn compile dependency:copy-dependencies -DincludeScope=runtime
java -cp "target/dependecy/*:target/classes" com.example.Main args...

Regards

Maksim Kostromin
  • 3,273
  • 1
  • 32
  • 30