gradle: 切换到 implementation/testImplementation 导致访问配置时出错

huangapple go评论123阅读模式
英文:

gradle: switching to implementation/testImplementation causing errors when accessing configuration

问题

def jmockit = configurations.testCompile.files.find { it.name.contains("jmockit") }.absolutePath

它会报错 无法在空对象上获取属性'absolutePath'

当我尝试像这样更改它时

def jmockit = configurations.testImplementation.files.find { it.name.contains("jmockit") }.absolutePath

我会得到错误

不允许直接解决配置'testImplementation'

根据其他相关问题,我尝试了

def jmockit = configurations.compileClasspath.files.find { it.name.contains("jmockit") }.absolutePath

这再次导致了无法在空对象上获取属性'absolutePath'的错误。

测试依赖项(testImplementation)进入哪个配置?我如何以这种方式访问它们?

不确定是否相关,但这是IntelliJ中的Java项目。

英文:

I have a project which was building fine when I was using the depricated compile and testCompile, but when I switch it to implementation and testImplementation, this reference breaks.

def jmockit = configurations.testCompile.files.find { it.name.contains("jmockit") }.absolutePath

It gives the error Cannot get property 'absolutePath' on null object

When I try to change it like so

def jmockit = configurations.testImplementation.files.find { it.name.contains("jmockit") }.absolutePath

I get the error

Resolving configuration 'testImplementation' directly is not allowed

Based on other related questions, I've tried

def jmockit = configurations.compileClasspath.files.find { it.name.contains("jmockit") }.absolutePath

which again gives the Cannot get property 'absolutePath' on null object error.

What configuration are testImplementation dependencies going into? How do I access them in this way?

not sure if it's relevant but this is a java project in IntelliJ.

答案1

得分: 3

它出错是因为testCompile不是继承自testImplementation,事实上是相反的。

你可以在Dependency configurations文档中的Figure 3. Java plugin - test source set dependency configurations中查看完整的测试配置层次结构。

你必须使用testCompileClasspath来获取你所寻找的absolutePath。下面是完整的示例(Kotlin DSL):

plugins {
    id("java")
}

repositories {
    mavenCentral()
}

group = "io.mateo.test"

dependencies {
    testImplementation("org.apache.commons:commons-lang3:3.11")
    testImplementation("org.jmockit:jmockit:1.49")
}

val jmockit = configurations.testCompileClasspath.get().files.filter { it.name.contains("jmockit") }[0]

println(jmockit.absoluteFile)

通过上述代码,你将看到你所需的路径:

$ ./gradlew assemble

> Configure project :
C:\Users\fmate\.gradle\caches\modules-2\files-2.1\org.jmockit\jmockit.49\e281fc6778c43060402505f062f6515c2adeb037\jmockit-1.49.jar

BUILD SUCCESSFUL in 731ms
2 actionable tasks: 2 up-to-date
英文:

It breaks because testCompile does not extend from testImplementation. In fact it's the other way around.

You can see the full test configuration hierarchy in Figure 3. Java plugin - test source set dependency configurations from
Dependency configurations docs.

You must use the testCompileClasspath to retrieve the absolutePath you seek. Full example below (Kotlin DSL):

plugins {
    id("java")
}

repositories {
    mavenCentral()
}

group = "io.mateo.test"

dependencies {
    testImplementation("org.apache.commons:commons-lang3:3.11")
    testImplementation("org.jmockit:jmockit:1.49")
}

val jmockit = configurations.testCompileClasspath.get().files.filter { it.name.contains("jmockit") }[0]

println(jmockit.absoluteFile)

With the above, you will see the path you're after:

$ ./gradlew assemble

> Configure project :
C:\Users\fmate\.gradle\caches\modules-2\files-2.1\org.jmockit\jmockit.49\e281fc6778c43060402505f062f6515c2adeb037\jmockit-1.49.jar

BUILD SUCCESSFUL in 731ms
2 actionable tasks: 2 up-to-date

huangapple
  • 本文由 发表于 2020年7月23日 07:21:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63044549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定