我正在尝试使用 gradle 6.0 版在 java 中构建 uber jar,它构建得很好。但是当我运行 Uber Jar 时,它会抛出以下错误
root@9393134c8941:/home/gradle/checkout-service# java -jar build/libs/checkout-service-1.0-SNAPSHOT-uber.jar
Error: Could not find or load main class com.noths.runner.Runner
Caused by: java.lang.ClassNotFoundException: com.noths.runner.Runner
我的 build.gradle 文件如下所示
// Shadow Plugin https://github.com/johnrengelman/shadow
plugins {
id 'java'
}
group 'com.noths'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
compileOnly 'org.projectlombok:lombok:1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'
testCompileOnly 'org.projectlombok:lombok:1.18.20'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
implementation 'com.google.code.gson:gson:2.8.8'
implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
testImplementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.2.3'
testImplementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
}
test {
useJUnitPlatform()
}
task uberJar(type: Jar) {
manifest {
attributes 'Main-Class': 'com.noths.runner.Runner'
}
archiveClassifier = 'uber'
from sourceSets.main.output
dependsOn configurations.runtimeClasspath
from {
configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
}
}
我正在使用以下步骤构建 uber jar
cd /home/gradle/checkout-service/
rm -rf .gradle
gradle clean
gradle build --refresh-dependencies
gradle uberJar;
输出中的 Uber 罐子
root@9393134c8941:/home/gradle/checkout-service# ls -lh build/libs/
total 736K
-rw-r--r-- 1 root root 261 Aug 25 05:07 checkout-service-1.0-SNAPSHOT.jar
-rw-r--r-- 1 root root 729K Aug 25 05:08 checkout-service-1.0-SNAPSHOT-uber.jar
我也打开了 uber jar,它的 Runner 类与 build.gradle 中给出的路径相同。
谁能告诉我我错过了什么?