Search code examples
javaspring-bootgradlejargradle-kotlin-dsl

Gradle set name for jar created with spring-boot's assemble


I want to create an executable jar with gradle (kotlin-dsl) and I want to give it a custom name. For the executable jar I'm using the spring boot plugin and ./gradlew :app1:assemble:

plugins {
    id("myproject.java-application-conventions")
    id("org.springframework.boot") version "2.2.2.RELEASE"
}
dependencies {
    implementation(project(":lib"))
}
application {
    mainClass.set("org.myproject.app.Main")
}

init created two more files, buildSrc/build.gradle.kts:

plugins {
    `kotlin-dsl`
}

repositories {
    gradlePluginPortal()
    mavenCentral()
}

and buildSrc/src/main/kotlin/myproject.java-application-conventions.gradle.kts:

plugins {
    id("lomboker.java-common-conventions")
    application
}

With ./gradlew :app1:assemble I can create an executable jar but I don't see how I can set its name.

This question deals with naming jars but I don't know how to apply any answers to my problem.

Adding a jar block to my gradle file does not work: Expression 'jar' cannot be invoked as a function. it is interpreted as sun.tools.jar.resources.jar. So I try tasks.jar instead. For

tasks.jar {
    archiveBaseName.set("myapp")
    archiveVersion.set("version")
}

./gradlew :app1:jar while building successful creates no jar (it wouldn't be executable anyway) and ./gradlew :app1:assemble ignores the properties and just creates ./app1/build/libs/app1.jar.

Since I'm not using jar but assemble I guess I should use a tasks.assemble block. But that doesn't recognize archiveBaseName or archiveVersion and I don't know what the API is.

This is the page: https://plugins.gradle.org/plugin/org.springframework.boot but I find no API.


Solution

  • assemble is a lifecycle task which means that it doesn’t create anything. Its role is to trigger other tasks that it depends upon and that do have some output. You can see those tasks by running your build with --console=plain.

    The task that creates the Spring Boot fat jar is named bootJar. As you can see from its javadoc, it’s a customization of Gradle’s Jar and can be configured in the same way:

    tasks.bootJar {
        archiveBaseName.set("myapp")
        archiveVersion.set("version")
    }