Java 如何在 Gradle 中列出测试的类路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26824761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:21:13  来源:igfitidea点击:

How to list classpath for tests in Gradle

javagradlegroovyclasspath

提问by zizhuo64062kw

When I try running gradle test, I get the following output:

当我尝试运行时gradle test,我得到以下输出:

$ gradle test
:ro:compileJava UP-TO-DATE
:ro:processResources UP-TO-DATE
:ro:classes UP-TO-DATE
:ro:jar
:compileJava
:processResources UP-TO-DATE
:classes
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test

ro.idea.ToggleTest > testIsAd FAILED
    java.lang.NoClassDefFoundError at ToggleTest.java:13
        Caused by: java.lang.ClassNotFoundException at ToggleTest.java:13

ro.idea.ToggleTest > testToggle FAILED
    java.lang.NoClassDefFoundError at ToggleTest.java:13

2 tests completed, 2 failed
:test FAILED

So I want to check my classpath to see whether my classpath is wrong or not.

所以我想检查我的类路径以查看我的类路径是否错误。

My question is:How can I list the classpath at testtime with a Gradle task?

我的问题是:如何在test使用 Gradle 任务时列出类路径?

回答by Peter Niederwieser

You can list test runtime dependencies with:

您可以使用以下命令列出测试运行时依赖项:

gradle dependencies --configuration=testRuntime

Or, if you want to see the actual files:

或者,如果您想查看实际文件:

task printClasspath {
    doLast {
        configurations.testRuntime.each { println it }
    }
}

回答by specialk1st

Also, to list the classpath for the main (non-test) application run use:

此外,要列出主(非测试)应用程序运行的类路径,请使用:

run << {
    doLast {
        configurations.runtime.each { println it }
    }
}

回答by vdshb

Worked for me (Gradle 6.3, Kotlin DSL):

为我工作(Gradle 6.3,Kotlin DSL):

tasks.withType<Test> {
    this.classpath.forEach { println(it) }
}