Simo
Simo

Reputation: 2424

Use the gradle bootJar instead of jar task and build fails in Jenkins

After I started using the spring boot gradle plugin in my gradle.build file, the build fails on jenkins.

classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.2.RELEASE")

Things work fine locally including build, test and webapp runs fine with Jetty. The only problem is the build fails on Jenkins in the task artifactoryPublish. It says:

File '/var/lib/jenkins/jobs/release-my-project/workspace/build/libs/workspace-0.2.1-SNAPSHOT.jar' does not exists, and need to be published!

Not sure what's going with the gradle artifactoryPublish task. I think the task comes from Jenkins.

Before using the spring boot gradle plugin, my jar task in gradle.build is as follows:

jar {
    baseName = 'my-project'
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
        configurations.runtime.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    manifest {
        attributes 'Main-Class':'com.example.Application'
    }
    // Exclude manifest signature files
    exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/LICENSE'
}   

Since the spring boot gradle plugin disables the jar task, and replaces it with the bootJar task, so I configured the bootjar task as follows:

bootJar {
    baseName = 'my-project'
    mainClassName = 'com.example.Application'
    // Exclude manifest signature files
    exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/LICENSE'
}

One thing I noticed from jenkins log is that it says the file workspace-0.2.1-SNAPSHOT.jar does not exist. Seems like it is looking for the wrong file. Previously, it looked for the correct file my-project-0.2.1-SNAPSHOT.jar. When I built locally, this jar file was created. Not sure what made jenkins look for workspace-0.2.1-SNAPSHOT.jar. It is supposed to be my-project as I did define baseName inside the bootJar task.

Any idea what's wrong here? Thanks.

Upvotes: 4

Views: 26761

Answers (2)

Peter S.
Peter S.

Reputation: 591

I would want to extend Peter's answer, it is recommended by the authors of Gradle that we should use settings.gradle

Define the root project name in the settings file: The ´rootProject.name´ effectively assigns a name to the build as a whole, which is used in reports like build scans. If the root project name is not set, the name will be the container directory name, which can be unstable (i.e. you can check out your project to any directory).

Upvotes: 0

Peter Ledbrook
Peter Ledbrook

Reputation: 4492

Unless you explicitly define the name of the project, Gradle will use the directory name as the project name. On Jenkins, the project directory is called "workspace". artifactoryPublish is presumably using the project name to determine the name of the JAR file to publish. That's not good practice if that's the case.

Anyway, you really should set the name of your project. You won't have to explicitly set baseName on the Jar tasks then. Simply add a settings.gradle file in the root of the project, i.e. next to the build.gradle file, and set its content to:

rootProject.name = "my-project"

That should hopefully fix the problem, although it really depends on what the artifactoryPublish task is doing.

Upvotes: 5

Related Questions