3

I am getting the following error while spring boot jar in CI environment

java -jar application.jar

no main manifest attribute, in application.jar

The weird thing is, it does not give the issue in my local or jenkins slave. It starts all right.

It faces issue when I upload the jar to Nexus artifactory and download it on my CI environment.

Building jar using

gradle clean build -x test

My gradle.build file

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'
}

Articles consulted with no vain 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"

Community
  • 1
  • 1
Munai Das Udasin
  • 510
  • 1
  • 10
  • 24

5 Answers5

1

The default packaged jar file does not contain MANIFEST file. I don't know how to configure in Gradle, but in Maven when I added plugin:

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

And it works for me. You could change that Maven plugin config to Gradle.

dungvo
  • 289
  • 4
  • 7
1

i had to run gradle bootRepackage after building the jar and git a jar that I could execute!

Henning
  • 1,515
  • 1
  • 14
  • 24
0

It worked with

gradle upload -x jar
Munai Das Udasin
  • 510
  • 1
  • 10
  • 24
0

In build.gradle, I noticed boot repackaging was disabled so I enabled it :

bootRepackage {
    enabled = true
}
JP Tétreault
  • 600
  • 5
  • 15
-1

Hi I had exactly same problem, and I found the answer in: Spring Boot Upload BootRepackage Executable Jar

I used the solution 2

publish {
   dependsOn assemble
} 
Community
  • 1
  • 1
Liu Xiaolu
  • 241
  • 1
  • 2
  • 8