简体   繁体   English

如何使用 maven 将依赖项 jar(没有测试 jar)复制到目录?

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

I see maven-dependency-plugin does this;我看到maven-dependency-plugin这样做; however, it seems to copy everything (including test jars) to the destination directory.但是,它似乎将所有内容(包括测试 jar)复制到目标目录。 Anyone know how to configure this plugin to exclude test jars?任何人都知道如何配置此插件以排除测试 jars?

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.我认为 Mike 的用例与我的用例类似,我想复制我依赖的所有 jar 以及我自己的 jar,以便创建足以执行程序的目录层次结构,而无需将这些依赖项直接包含到我自己的 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.该指令进入 maven-dependency 插件的 pom.xml 部分。 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. excludeScope 将不起作用,因为排除测试会中止构建并排除所有可能的范围。 Instead the included scope needs to be adjusted.相反,需要调整包含的范围。

It is not clear if you wanted to exclude jars with test scope or test related jars ( test classifier ).目前尚不清楚您是要排除具有test范围的jar 还是测试相关的 jar( test分类器)。 In either case, there are two properties of dependency:copy-dependencies which can help you.在任何一种情况下, dependency都有两个属性:copy-dependencies可以帮助您。

  • excludeClassifiers Comma Separated list of Classifiers to exclude. excludeClassifiers逗号分隔的要排除的分类器列表。 Empty String indicates don't exclude anything (default).空字符串表示不排除任何内容(默认)。
  • excludeScope Scope to exclude. excludeScope要排除的范围。 An Empty string indicates no scopes (default).空字符串表示没有范围(默认)。

Documentation says: The scopes being interpreted are the scopes as Maven sees them, not as specified in the pom.文档说:被解释的范围是 Maven 看到的范围,而不是 pom.xml 中指定的范围。

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:根据我的经验,如果您只想使用在项目 pom.xml 文件中指定的编译范围依赖项运行您的类,则必须添加-DincludeScope=runtime java 系统设置,如下所示:

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

Regards问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM