没有主要清单属性,位于 /app.jar / gradle.kts/sprinboot

问题描述 投票:0回答:1

当我运行 docker run myorg/myapp 时 - 我收到错误

没有主要清单属性,在/app.jar中,我在此搜索答案无法执行jar-文件:“没有主要清单属性”

但对我来说没有任何作用..

这是 gradle.kts 文件

plugins {
    java
    id("org.springframework.boot") version "3.2.0"
    id("io.spring.dependency-management") version "1.1.4"

}

group = "com.example"
version = "0.0.1-SNAPSHOT"

java {
    sourceCompatibility = JavaVersion.VERSION_17
}



repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.springframework.boot:spring-boot-starter-validation")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("io.jsonwebtoken:jjwt-api:0.11.5")
    implementation("io.jsonwebtoken:jjwt-impl:0.11.5")
    implementation("io.jsonwebtoken:jjwt-jackson:0.11.5")
    implementation ("org.apache.logging.log4j:log4j-api:2.22.0")
    implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0")
    implementation("org.apache.maven.plugins:maven-jar-plugin:3.3.0")




    //developmentOnly("org.springframework.boot:spring-boot-docker-compose")
    runtimeOnly("org.postgresql:postgresql")
    runtimeOnly("io.jsonwebtoken:jjwt-impl:0.11.5")
    runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.11.5")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.springframework.security:spring-security-test")
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.1")
    testImplementation("org.mockito:mockito-core:5.8.0")

}

tasks.withType<Test> {
    useJUnitPlatform()


}

tasks.withType<Jar>() {

    duplicatesStrategy = DuplicatesStrategy.EXCLUDE

    manifest {
        attributes["Main-Class"] = "com.example.effectiveMoblile.EffectiveMoblileApplication"
    }
    configurations["compileClasspath"].forEach { file: File ->
        from(zipTree(file.absoluteFile))
    }
}

这是我的 docker 文件

FROM eclipse-temurin:17-jdk-alpine

VOLUME /tmp

ARG JAR_FILE=target/*jar

COPY ${JAR_FILE} app.jar

CMD ["sh", "-c", "java ${JAVA_OPTS} -jar /app.jar ${0} ${@}"]


在jar文件manifest.mf中有下一个文本

Manifest-Version: 1.0
Main-Class: build.libs.effectiveMoblile-0.0.1-SNAPSHOT-plain
Start-Class: com.example.effectiveMoblile.EffectiveMoblileApplication
Spring-Boot-Version: 3.2.0
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Spring-Boot-Layers-Index: BOOT-INF/layers.idx
Build-Jdk-Spec: 17
Implementation-Title: effectiveMoblile
Implementation-Version: 0.0.1-SNAPSHOT

我不明白我能做什么,那工作......

我在此搜索答案无法执行jar文件:“没有主要清单属性”

docker attributes manifest gradle-kotlin-dsl
1个回答
0
投票

您的设置有两个问题。

  1. 你所做的复制有问题,你正在将任何

    target/*.jar
    复制到
    app.jar
    ,但你应该具体,否则如果有多个jar文件,行为将不一致。

  2. (以及失败的原因)是您使用的是 Kotlin,它将 Kt 附加到您的类名中,因此它应该是 effectiveMoblileApplicationKt,而不是“EffectiveMoblileApplication”。 (即

    com.example.effectiveMoblile.EffectiveMoblileApplicationKt

顺便说一句,我不建议编写

manifest.mf
,而是使用 shadow jar 插件。

为此:

  • 删除您创建的清单
  • 将以下内容添加到您的 Gradle 文件中:
plugins {
  // rest of the plugins
  id("com.github.johnrengelman.shadow") version "7.0.0"
}

// will rename the resulting package
tasks.withType<ShadowJar> {
    archiveFileName.set("app.jar")
}

这样,您还可以按照

COPY target/app.jar app.jar
进行复制,以防您由于 Gradle 附加版本而执行
target/*.jar

© www.soinside.com 2019 - 2024. All rights reserved.