Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 965
Tien Do Nam Avatar asked May 23 '21 19:05

Tien Do Nam


People also ask

What is plain jar in spring boot?

app-plain. jar is the archive produced by the jar task. This is a plain or standard jar file that contains only the module's classes and resources. You can learn a bit more about this in the documentation for Spring Boot's Gradle plugin. Follow this answer to receive notifications.

What is a plain jar file?

as per the link, "simple" JAR is one that ain't any of: standalone EJB module, standalone Web application module, multiple EJB modules packaged in an Enterprise Application, multiple Web application modules package in an Enterprise Application; if the app is not packaged as any of those, then the JAR is "plain".

Does spring boot create fat jar?

Behind the scenes, spring-boot packages all the project dependencies inside the final artifact along side project classes (hence the “fat” jar). An embedded Tomcat server is also built-in.


3 Answers

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.

like image 166
Tien Do Nam Avatar answered Oct 19 '22 11:10

Tien Do Nam


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
like image 25
LiZongbo Avatar answered Oct 19 '22 11:10

LiZongbo


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("")
}
like image 8
0case Avatar answered Oct 19 '22 10:10

0case