问题描述
我有使用maven构建的Java项目,该项目具有一些依赖性.这样的依赖项之一是另一个项目(我想到了Eclipse工作区),它仅在test
范围内.
I have Java project build with maven that has some dependencies. One of such dependencies is another project (I have Eclipse workspace in mind) and it is only in test
scope.
<dependency>
<groupId>my.group.id</groupId>
<artifactId>artifactid</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
我在项目根目录
mvn clean package -Dmaven.test.skip=true
我期望发生的事情是,maven将构建项目,而将测试依赖项抛在身后.但是,抛出未解决的依赖项异常(未解析到我的test
范围内的依赖项).当我使用-X
选项运行构建命令时,在项目构建计划中发现了以下报告:
what I expected to happen it that maven would build project leaving test dependencies behind. However unresolved dependency exception is thrown (unresolved to my test
scoped dependency). As I ran build command with -X
option, I spoted following report in my project build plan:
[DEBUG] Dependencies (collect): []
[DEBUG] Dependencies (resolve): [compile, runtime, test]
[DEBUG] Repositories (dependencies): [central (http://repo.maven.apache.org/maven2, releases)]
[DEBUG] Repositories (plugins) : [central (http://repo.maven.apache.org/maven2, releases)]
[DEBUG] -----------------------------------------------------------------------
因此,尽管跳过了测试甚至进行了编译(mvn clean compile
运行正常),但maven仍尝试解决测试依赖项.
So despite skipping tests and even compiling them (mvn clean compile
runs fine) maven tries to resolve test dependencies.
如何在跳过测试执行时从构建计划依赖关系解决步骤中排除测试依赖关系?
推荐答案
总之:不能.至少不容易. maven.test.skip
不会阻止surefire:test mojo的运行,它只会将插件配置为不执行任何操作. (与compile:testcompile相同).
In short: you can't. At least not easily. maven.test.skip
does not prevent the surefire:test mojo from running, it only configures the plugin to do nothing. (same for compile:testcompile).
这两个mojo都需要进行范围测试的依赖关系解析(mojo定义的一部分).
Both mojos need dependency resolving of scope test (part of the mojo definition).
因此,防止这些依赖关系得到解决的唯一选择是:
So, the only options of preventing those dependencies from being resolved would be either:
- 使用配置文件并重新绑定compile:testCompile以及surefire:test到不存在的阶段
- 将所有测试依赖项放在单独的配置文件中
但是,这两个选项都很脏.
However, both options are rather dirty.
所以最好的选择是先安装依赖项(使用mvn install
),然后简单地接受maven解决依赖关系的方式.
So your best bet would be to install your dependent artifacts first (using mvn install
) and simply accept the way maven resolves dependencies.
这篇关于如何从构建中排除{test}范围内的依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!