如何将测试类包含到 shadowJar 中?

Dim*_*ims 8 java testing unit-testing gradle shadowjar

我正在使用 shadow Gradle 插件来构建 JAR,其中包含所有引用的 jar。

在我的build.gradle我只有

apply plugin: "com.github.johnrengelman.shadow"
Run Code Online (Sandbox Code Playgroud)

jar {
    manifest {
        attributes 'Main-Class': 'MYCLASS'
    }

}
Run Code Online (Sandbox Code Playgroud)

与此有关。我不知道,它是如何知道的,要构建什么,但它有效。

现在,是否有可能也包含测试类?

Eya*_*oth 4

来自官方文档https://imperceptiblethoughts.com/shadow/custom-tasks/

隐藏测试源和依赖项

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar task testJar(type: ShadowJar) { classifier = 'tests' from sourceSets.test.output configurations = [project.configurations.testRuntime] }

上面的代码片段将生成一个隐藏的 JAR,其中包含主源和测试源以及所有运行时和 testRuntime 依赖项。该文件输出到 build/libs/--tests.jar。