Gradle 使用最新版本的 TestNG 而不是声明的版本

Gradle uses latest version of TestNG instead of declared version

在我的 gradle 脚本中,我有以下依赖项:

dependencies {
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version:'3.9.1'
compile group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '2.2.4'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.6.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2'
compile group: 'org.uncommons', name: 'reportng', version:'1.1.4'
compile group: 'com.google.inject', name: 'guice', version:'4.0'
compile group: 'org.apache.commons', name:'commons-math3', version: '3.6.1'
testCompile group: 'org.testng', name: 'testng', version:'6.14.3'
testCompile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.25'}

如这里所见,我要求使用 6.14.3 作为 testng 版本。只有当我尝试 运行 我的脚本时,我才遇到问题:

Execution failed for task ':compileJava'.

我首先检查了 Java 版本是否正确,但没有问题。仅当我 运行 gradle --debug 时。我发现下载的testng jar是7.0.0-beta4版本的。但是因为我没有声明这个我不确定为什么要使用这个或者我可以强制使用正确的?

运行 ./gradlew dependencies 应该向您显示以下 compile 依赖项(以及许多其他项):

…
+--- org.uncommons:reportng:1.1.4
|    +--- org.testng:testng:[5.0,) -> 7.0.0-beta6
|    \--- velocity:velocity:1.4
|         \--- velocity:velocity-dep:1.4
…

换句话说,您声明的 compileorg.uncommons:reportng:1.1.4 的依赖具有对 TestNG 的传递依赖,后者 Gradle 解析为最新的 TestNG 版本——因为您没有明确声明任何compile 配置 的其他 TestNG 依赖项 。请注意,您仅直接依赖 testCompile 配置中的 TestNG!

您应该能够通过如下更改 TestNG 依赖项声明来解决此问题:

compile group: 'org.testng', name: 'testng', version:'6.14.3'