application.jar中没有主要清单属性

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了application.jar中没有主要清单属性相关的知识,希望对你有一定的参考价值。

我在CI环境中的spring boot jar时遇到以下错误

java -jar application.jar

no main manifest attribute, in application.jar

奇怪的是,它没有在我的本地或jenkins奴隶问题。它开始没事。

当我将jar上传到Nexus artifactory并在我的CI环境中下载时,它会面临问题。

建筑罐使用

gradle clean build -x test

我的gradle.build文件

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
        jcenter()
        mavenLocal()
        mavenCentral()

    }
    dependencies {
        classpath 'org.akhikhl.gretty:gretty:1.2.4'
        classpath 'org.ajoberstar:gradle-jacoco:0.1.0'
        classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:1.2'
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE")
    }
}

repositories {
    mavenCentral()
    mavenLocal()
    maven {
        credentials {
            username "hello"
            password "world"
        }
        url "Nexus URL"
    }
}

apply plugin: 'maven-publish'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'spring-boot'
apply plugin: "org.sonarqube"
apply plugin: 'jacoco'

group = 'com.company.pod'

/* Determining version from jenkins pipeline, otherwise is set to 1.0.0-SNAPSHOT */
version = new ProjectVersion(1, 0, System.env.SOURCE_BUILD_NUMBER, System.env.RELEASE_TYPE)

println(version)


class ProjectVersion {
    Integer major
    Integer minor
    String build
    String releaseType

    ProjectVersion(Integer major, Integer minor, String build, String releaseType) {

        this.major = major
        this.minor = minor
        this.build = build
        this.releaseType = releaseType
    }

    @Override
    String toString() {
        String fullVersion = "$major.$minor"

        if(build) {
            fullVersion += ".$build"
        }
        else{
            fullVersion += ".0"
        }

        if(releaseType) {
            fullVersion += "-RELEASE"
        }
        else{
            fullVersion += "-SNAPSHOT"
        }

        fullVersion
    }
}

/*Sonarqube linting of your repository.*/
sonarqube {
    properties {
        property "sonar.language", "java"
                }
        }


/* Please don't comment out the part below
To run the same on your laptops/prod boxes/CUAT boxes, just edit the gradle.properties file.
(It will be present in the home directory of the user you are using to run gradle with.`sudo` means root user and likewise)
Enter the following lines(and yes, it will run without values, thank you gradle!)

nexusUrl=
nexusRelease=
nexusSnapshot=
nexusUsername=
nexusPassword=

*/

uploadArchives {
    repositories {
        mavenDeployer {
                repository(url: nexusUrl+"/"+nexusRelease+"/") {
                        authentication(userName: nexusUsername, password: nexusPassword)
                        }
                snapshotRepository(url: nexusUrl+"/"+nexusSnapshot+"/"){
                        authentication(userName: nexusUsername, password: nexusPassword)
                        uniqueVersion = false
                        }
                }
        }
}






publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

publishing {
    publications {
        maven(MavenPublication) {
            groupId 'com.company.something'/*This is different from group variable before*/
            artifactId 'something'
            version '2.0.0'
            from components.java
        }
    }
}

/*
publishing {
    repositories {
        maven {
            url "~/.m2/repository/"
        }
    }
}
*/

task wrapper(type: Wrapper) {
    gradleVersion = '2.9'
    distributionUrl = "https://services.gradle.org/distributions/gradle-$GradleVersion-all.zip"
}

dependencies {
    compile('org.projectlombok:lombok:1.16.6')
    compile("com.company.commons:company-app:0.0.1-RELEASE")
    compile group: 'com.google.guava', name: 'guava', version: '19.0'
    compile("com.squareup.retrofit2:retrofit:2.0.1")
    compile("com.squareup.retrofit2:converter-jackson:2.0.1")

    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.7'
    compile("com.getsentry.raven:raven-logback:7.2.2")
    compile("org.springframework.boot:spring-boot-starter-actuator")

    testCompile("org.springframework.boot:spring-boot-starter-test")
    testCompile("com.h2database:h2")
    testCompile("junit:junit")
//  testCompile("org.springframework.boot:spring-boot-starter-test")
//  testCompile group: 'org.hibernate', name: 'hibernate-validator', version: '4.2.0.Final'
}

文章咨询没有徒劳1. Gradle- no main manifest attribute 2. http://www.vogella.com/tutorials/Gradle/article.html#specifying-the-java-version-in-your-build-file 3. Can't execute jar- file: "no main manifest attribute"

答案

默认打包的jar文件不包含MANIFEST文件。我不知道如何在Gradle中配置,但在Maven中添加插件时:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>vn.dung.Application</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
        </plugin>

它对我有用。您可以将Maven插件配置更改为Gradle。

另一答案

我必须在构建jar后运行gradle bootRepackage并git我可以执行的jar!

另一答案

在build.gradle中,我注意到启动重新打包已禁用,因此我启用了它:

bootRepackage {
    enabled = true
}
另一答案

嗨我有完全相同的问题,我找到了答案:Spring Boot Upload BootRepackage Executable Jar

我使用了解决方案2

publish {
   dependsOn assemble
} 
另一答案

它合作

gradle upload -x jar

以上是关于application.jar中没有主要清单属性的主要内容,如果未能解决你的问题,请参考以下文章

错误:“Elevator.jar 中没有主要清单属性”

使用 Intellij 2016 创建 Jar - 没有主要清单属性

gdx-setup.jar 中没有主要清单属性

无法运行 docker 映像 - app.jar 中没有主要清单属性 [重复]

Gradle - 没有主要清单属性

Java jar - 没有主要清单属性