简体   繁体   English

没有主要清单属性,在 jar 中

[英]no main manifest attribute, in jar

I have a spring project with Gradle.我有一个 Gradle 的春季项目。 I created a task in the build.gradle :我在build.gradle中创建了一个任务:


task dockerFile(type: Dockerfile) {
    destFile.set(project.file('Dockerfile'))
    from "alpine:$alpineVersion"
    runCommand 'apk add --no-cache openjdk11'
    copyFile "build/libs/${jar.archiveFileName.get()}", '/app/'
    workingDir '/app/'
    entryPoint 'java'
    defaultCommand '-jar', "/app/${jar.archiveFileName.get()}"
}

Everything works fine.一切正常。 Dockerfile is generated.生成 Dockerfile。 But when I try to run the image it gives me the error: no main manifest attribute, in /app/demo-0.0.1-SNAPSHOT-plain.jar但是当我尝试运行图像时,它给了我错误: no main manifest attribute, in /app/demo-0.0.1-SNAPSHOT-plain.jar

Also here is my entire build.gradle:这也是我的整个 build.gradle:

import com.bmuschko.gradle.docker.tasks.image.Dockerfile

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.7.6'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
    id 'com.bmuschko.docker-remote-api' version '6.6.1' apply false
}

group = 'ms10gradle2'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation "org.springframework.boot:spring-boot-starter-data-jpa:$jpaVersion"
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'com.h2database:h2'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

task printJpaVer{
    print "JPA VERSION: ${jpaVersion}"
}

task printfiles {
   doLast {
       def files = "cmd.exe /c dir".execute().text.trim()
       println(files)
       project.getAllprojects().forEach(System.out::print)
   }
}

task printTasks{
    project.getTasks().forEach{
        println("task name: "+ it)
    }
}

task printTasksInsideSubModulesTrueOrFalse{
    project.getAllTasks(false).entrySet().forEach(System.out::println)
}


task printTaskVersion{
    project.getAllprojects()
        .forEach(p -> println p.name +"- "+p.getVersion())

    print "================================================================"
    project.getAllprojects().forEach(p -> println p.getVersion())
}


task dockerFile(type: Dockerfile) {
    destFile.set(project.file('Dockerfile'))
    from "alpine:$alpineVersion"
    runCommand 'apk add --no-cache openjdk11'
    copyFile "build/libs/${jar.archiveFileName.get()}", '/app/'
    workingDir '/app/'
    entryPoint 'java'
    defaultCommand '-jar', "/app/${jar.archiveFileName.get()}"
}


and Dockerfile:和 Dockerfile:

FROM alpine:3.11.2
RUN apk add --no-cache openjdk11
COPY build/libs/demo-0.0.1-SNAPSHOT-plain.jar /app/
WORKDIR /app/
ENTRYPOINT ["java"]
CMD ["-jar", "/app/demo-0.0.1-SNAPSHOT-plain.jar"]

Looks like you tried to run jar is not executable.看起来你试图运行 jar 是不可执行的。 Let's say you have main class in your code with name fully.qualified.MainClass There are 2 ways:假设您的代码中有一个名为fully.qualified.MainClass的主类,有两种方法:

  1. It it necessary to add manifest with main-class(preferable):有必要添加带有主类的清单(最好):

for maven:对于行家:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        ...
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>fully.qualified.MainClass</mainClass>
            </manifest>
          </archive>
        </configuration>
        ...
      </plugin>

maven documentation for more info. maven 文档以获取更多信息。

for gradle对于摇篮

it is necessary to add jar task with Main-Class: 有必要使用 Main-Class 添加 jar 任务:
 jar { manifest { attributes 'Main-Class': 'fully.qualified.MainClass' } }

Jar task documentation and also there was question about gradle executable jar: Creating runnable JAR with Gradle Jar 任务文档以及关于 gradle executable jar 的问题: Creating runnable JAR with Gradle

  1. run jar with your main class as param:以您的主类作为参数运行 jar:
 java -cp MyJar.jar fully.qualified.MainClass

How to run a class from Jar which is not the Main-Class in its Manifest file 如何从 Manifest 文件中不是 Main-Class 的 Jar 运行类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM