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

时间:2016-11-30 19:56:37

标签: java unit-testing testing gradle shadowjar

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

build.gradle我只有

apply plugin: "com.github.johnrengelman.shadow"

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

}

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

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

2 个答案:

答案 0 :(得分:1)

来自官方文档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。

答案 1 :(得分:0)

官方文档似乎与插件的最新 (v7.0.0) 版本过时了。使用这个版本和最新版本的gradle(7.0),我这样做:

task testJar(type: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
    archiveClassifier.set("alltests")
    from sourceSets.main.output, sourceSets.test.output
    configurations = [project.configurations.testRuntimeClasspath]
}

文档中的 from 子句和“configurations”子句都错误。

  • 作为对其他答案提到的评论,jar 中缺少主要类(通常是您测试的类),因此您需要添加 sourceSets.main.output
  • project.configurations.testRuntime 没有按照文档工作,它告诉我 testImplementation' is not allowed as it is defined as 'canBeResolved=false