You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Docker镜像时出现no main manifest attribute错误求助(Spring Boot+Gradle)

解决Docker运行Spring Boot应用时的"no main manifest attribute"错误

问题根源

你的配置存在两个核心问题:

  1. 自定义的Jar任务与Spring Boot Gradle插件默认的bootJar任务冲突,生成了不符合Spring Boot规范的普通jar(带-plain后缀),导致manifest中的Main-Class指向无效值。
  2. Dockerfile中使用target/*jar匹配jar包,可能误拷贝了普通jar而非Spring Boot的可执行bootJar。

解决方案

1. 修改gradle.kts,移除自定义Jar任务

Spring Boot Gradle插件已经提供了bootJar任务,自动生成可执行的fat jar并配置正确的manifest属性,无需手动编写Jar任务。修改后的配置如下:

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")

    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()
}

2. 修改Dockerfile,指定正确的bootJar路径

Spring Boot的bootJar任务默认将可执行jar生成在build/libs目录下,文件名带-boot.jar后缀。修改Dockerfile如下:

FROM eclipse-temurin:17-jdk-alpine

VOLUME /tmp

ARG JAR_FILE=build/libs/*-boot.jar

COPY ${JAR_FILE} app.jar

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

3. 重新构建并运行

  • 执行构建命令生成可执行bootJar:
    ./gradlew clean bootJar
    
  • 重新构建Docker镜像并运行:
    docker build -t myorg/myapp .
    docker run myorg/myapp
    

验证manifest

正确的Spring Boot可执行jar的MANIFEST.MF中,Main-Class应为org.springframework.boot.loader.JarLauncher,Start-Class才是你的应用主类com.example.effectiveMoblile.EffectiveMoblileApplication,这是Spring Boot的标准启动方式。

内容的提问来源于stack exchange,提问作者Rustam Saydam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 20:41:03