126

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()
}

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Tien Do Nam
  • 3,630
  • 3
  • 15
  • 30
  • 9
    It helps to read [the spring boot documentation](https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#packaging-executable.and-plain-archives): you can disable it if you don't need it. – Thomas Kläger May 23 '21 at 20:02
  • 3
    @ThomasKläger Yep, thanks. I didn't expect this breaking change. The CI server expected only one file in libs. – Tien Do Nam May 23 '21 at 20:06
  • refer [answer](https://stackoverflow.com/a/62645933/3303074) – Prasanth Rajendran Jun 08 '21 at 15:12
  • 1
    commenting out the version line `//version = "0.0.1-SNAPSHOT"` and using `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 – Manishoaham Mar 19 '22 at 18:16
  • Same problem here. The second jair crashed or build pipelines. – spyro Apr 26 '22 at 08:55

6 Answers6

146

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.

Tien Do Nam
  • 3,630
  • 3
  • 15
  • 30
  • 2
    thanks for the Kotlin example. `tasks.jar { enabled = false }` worked for me; not sure if there's a difference. – Alexander Taylor Jul 09 '21 at 17:14
  • 23
    With `jar {enabled = false }` it generates no jar at all (not only with `plain` postfix). How this can be an accepted answer? – srzhio Aug 20 '21 at 10:16
  • 8
    @srzhio strangely enough, this disables only the .plain jar, and not the full jar. https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#packaging-executable.and-plain-archives – Innokenty Aug 25 '21 at 10:25
  • 3
    @Innokenty, yep, that is what the documentation state, but only `bootJar.enabled = true; jar.enabled = false` worked for me – srzhio Aug 26 '21 at 11:30
  • 2
    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. – blacktide Mar 30 '22 at 13:47
37

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:

See:

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
LiZongbo
  • 379
  • 2
  • 2
  • 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. – Peter S. Jul 02 '21 at 10:51
  • 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 – Manishoaham Mar 19 '22 at 18:11
  • if bootjar is true, you want jar enabled false – Kalpesh Soni Mar 28 '23 at 19:45
11

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("")
}
Innokenty
  • 3,001
  • 1
  • 27
  • 30
0case
  • 443
  • 7
  • 13
  • @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 DeSales Sep 19 '21 at 04:35
  • 1
    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"` – Marcello DeSales Sep 19 '21 at 05:07
  • @MarcellodeSales this was my issue too. When running `docker build` I ended up with `When using ADD with more than one source file, the destination must be a directory and end with a /` when upgrading to Sprint Boot 2. – Sridhar Sarnobat Aug 30 '22 at 19:32
4

Tested solution. I was facing the same issue: just add below in your gradle

jar{
    archiveClassifier=''
    enabled = false
}
aaossa
  • 3,763
  • 2
  • 21
  • 34
sunny
  • 66
  • 3
3

Instead of using the build gradle task you could use bootJar. That will only build the bootable jar.

Keep in mind that bootJar won't run your tests before building.

slorinc
  • 2,264
  • 1
  • 11
  • 8
  • Damn, this would have been a nice solution if I wasn't using a common Jenkins build script that will screw up other repos. – Sridhar Sarnobat Aug 30 '22 at 19:31
  • That's actually not true, at least in my case: the bootJar task in my Kotlin project causes creation the both just xxx.jar and the xxx-plain.jar. – PavelPraulov Feb 26 '23 at 14:05
  • Worked for me. Am wondering if @PavelPraulov had some other option which forced the "build" to run. – Mitch1077487 Jul 11 '23 at 15:15
-1

I had the same problem today!

above springboot 2.5, use jar or war build project will append plain text to the name of target file

task.getArchiveClassifier().convention("plain")

so you can define this to resolve this problem

jar {
    classifier = ''
}

yeah, you can see that the author tell you how to config it in this url https://github.com/spring-projects/spring-boot/commit/ebdb046ca93684b45691e689b6a23562dd636b61#diff-5efe17833c4d5e21d01178734dea8859dc1669199669101a36448ed696413820.

  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. Please [edit] the answer with all relevant information. Make sure to use your own words, [answers comprised entirely of a quote (sourced or not) will often be deleted since they do not contain any original content](/help/referencing). – Adriaan Mar 29 '23 at 07:26