我需要分发我们的一个集成测试,它位于 src/test 下。所以我正在使用 gradle 构建一个指向此类的 jar。
这是我在网上查的结果:
task fatJar(type: Jar) {
zip64 = true
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': 1.3,
'Main-Class': 'org.example.AnIntegrationTest'
}
from sourceSets.test.output
}
当我尝试运行它时,我得到:
Error: Could not find or load main class org.example.AnIntegrationTest
main 方法在那里,'jar tf' 确实显示了包中的类。
我在这里错过了什么?
最佳答案
您的 jar 文件缺少测试运行时依赖项。一种可能的解决方案是将所有必需的依赖项也打包到您的 jar 中。请注意,这会增加可分发 jar 的大小。
向您的 jar 任务添加一个额外的 from
子句:
task fatJar(type: Jar) {
zip64 = true
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': 1.3,
'Main-Class': 'org.example.AnIntegrationTest'
}
from sourceSets.test.output
//collect all dependencies
from { configurations.testRuntime.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
关于java - 来自 src/test 的 Jar 文件中的主类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34907033/