Spring Boot 2.5.0 generates plain.jar file. Can I remove it?

18,499

Solution 1

It was a change in Spring Boot 2.5.0.

As @ThomasKläger pointed out: You can set it in the build.gradle configuration.

build.gradle

jar {
    enabled = false
}

For Kotlin devs:

tasks.getByName<Jar>("jar") {
    enabled = false
}

Alternatively, you can run the bootJar task. It produces only the default runnable jar.

Solution 2

try use follow setting:

jar {
   enabled = true
   archiveClassifier = '' //use empty string
}

because org.springframework.boot.gradle.plugin.JavaPluginAction.java

        private void classifyJarTask(Project project) {
    project.getTasks().named(JavaPlugin.JAR_TASK_NAME, Jar.class)
            .configure((task) -> task.getArchiveClassifier().convention("plain"));
}

from spring-boot-gradle-plugin sources file: https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-gradle-plugin/2.5.0/spring-boot-gradle-plugin-2.5.0-sources.jar

see:
https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:archiveClassifier

Solution 3

This gradle config will produce myprogram-0.0.1.jar instead of myprogram-0.0.1-plain.jar

In your build.gradle.kts

// Build executable jar
tasks.jar {
    enabled = true
    // Remove `plain` postfix from jar file name
    archiveClassifier.set("")
}
Share:
18,499
Tien Do Nam
Author by

Tien Do Nam

Updated on June 07, 2022

Comments

  • Tien Do Nam
    Tien Do Nam almost 2 years

    After the Spring Boot 2.5.0 update, it generates the myprogram-0.0.1-plain.jar file alongside the usual myprogram-0.0.1.jar. Can I disallow gradle to generate the *.plain.jar file? I use Gradle 7.0.2.

    What I get:

    build/
      libs/
        myprogram-0.0.1.jar
        myprogram-0.0.1-plain.jar
    

    What I want:

    build/
      libs/
        myprogram-0.0.1.jar
    

    build.gradle:

    plugins {
        id 'org.springframework.boot' version '2.5.0'
        id 'io.spring.dependency-management' version '1.0.11.RELEASE'
        id 'java'
    }
    
    group = 'com.example'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '11'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
    }
    
    test {
        useJUnitPlatform()
    }
    
    
  • Peter S.
    Peter S. almost 3 years
    I needed this because I have the shared library across other projects and I use the spring boot plugin there too. So I cannot set up bootJar enabled to false.
  • Alexander Taylor
    Alexander Taylor almost 3 years
    thanks for the Kotlin example. tasks.jar { enabled = false } worked for me; not sure if there's a difference.
  • srzhio
    srzhio almost 3 years
    With jar {enabled = false } it generates no jar at all (not only with plain postfix). How this can be an accepted answer?
  • Innokenty
    Innokenty over 2 years
    @srzhio strangely enough, this disables only the .plain jar, and not the full jar. docs.spring.io/spring-boot/docs/current/gradle-plugin/refere‌​nce/…
  • srzhio
    srzhio over 2 years
    @Innokenty, yep, that is what the documentation state, but only bootJar.enabled = true; jar.enabled = false worked for me
  • Marcello de Sales
    Marcello de Sales over 2 years
    @Ocase you saved me here... Wow... Just migrated an entire app with automated docker builds to use the jars (UnmazedBoot) and I couldn't expect 2 jars LOL
  • Marcello de Sales
    Marcello de Sales over 2 years
    After using this, I got no manifest to it :( no main manifest attribute, in build/libs/microadmin-service-1.0.0-RELEASE.jar... So, my solution to bypass this was to run "gradle bootJar"
  • Manishoaham
    Manishoaham about 2 years
    commenting out the version line //version = "0.0.1-SNAPSHOT" and using just war { archiveClassifier.set("") } in build.gradle.kts file worked for me (wanted to rename war file in my case). It changed war file name from demo-0.0.1-SNAPSHOT-plain.war to demo.war
  • blacktide
    blacktide about 2 years
    Just tested with a brand new project from start.spring.io with Spring Boot 2.6.5 and adding jar.enabled = false and I can confirm that the boot jar is still generated when running ./gradlew build. The bootJar.enabled = true part is not required.