How to copy dependencies jars (without test jars) to a directory using maven?

后端 未结 3 1684
陌清茗
陌清茗 2021-01-01 09:57

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 t

相关标签:
3条回答
  • 2021-01-01 10:40

    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

    0 讨论(0)
  • 2021-01-01 10:41

    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).
    0 讨论(0)
  • 2021-01-01 10:51

    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.

    0 讨论(0)
提交回复
热议问题