Spring Boot 2 Gradle plugin without executable jar

2020-07-06 20:30发布

How do I configure Spring Boot Gradle plugin 2 to disable the Boot distribution in Gradle Script Kotlin.

The distribution i want is a assembly bundle zip with all dependencies (and the jar) in the lib folder.

I have enabled the Jar task and disabled the bootJar task.

Current source of my build file https://github.com/Skatteetaten/mokey/blob/825a81f20c21a2220876a09ebf7f01fe7c61f2fd/build.gradle.kts

Note that the aurora skatteetaten gradle plugin adds the distribution mechanism. https://github.com/Skatteetaten/aurora-gradle-plugin

5条回答
冷血范
2楼-- · 2020-07-06 21:17

This kotlin DSL version of @panser answer works for me (disable spring-boot bootJar task and enable jar task):

import org.springframework.boot.gradle.tasks.bundling.BootJar

tasks.getByName<BootJar>("bootJar") {
    enabled = false
}

tasks.getByName<Jar>("jar") {
    enabled = true
}
查看更多
3楼-- · 2020-07-06 21:24

When the application plugin is configured, Spring Boot creates an additional distribution that contains the application packages as a fat jar. The default distribution is left intact and you should be able to use it without disabling Boot's distribution.

The default distribution relies on the standard jar task. This task is disabled by Spring Boot's plugin by default as the assumption is that you will want to use the fat jar produced by bootJar instead. When that's not the case, you can re-enable the jar like this:

jar {
    enabled = true
}

With this change in place, you can run the distZip task:

$: ./gradlew clean distZip --console=plain
:clean
:bootBuildInfo
:compileJava
:processResources
:classes
:jar
:startScripts
:distZip

BUILD SUCCESSFUL in 1s
7 actionable tasks: 7 executed

It will create a zip that packages the application and all of its dependencies as separate jars in the lib directory of the distribution.

$: unzip -l build/distributions/application-distribution-0.0.1-SNAPSHOT.zip
Archive:  build/distributions/application-distribution-0.0.1-SNAPSHOT.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  03-19-2018 07:31   application-distribution-0.0.1-SNAPSHOT/
        0  03-19-2018 07:31   application-distribution-0.0.1-SNAPSHOT/lib/
     1860  03-19-2018 07:31   application-distribution-0.0.1-SNAPSHOT/lib/application-distribution-0.0.1-SNAPSHOT.jar
      588  03-01-2018 05:24   application-distribution-0.0.1-SNAPSHOT/lib/spring-boot-starter-web-2.0.0.RELEASE.jar
      645  03-01-2018 05:24   application-distribution-0.0.1-SNAPSHOT/lib/spring-boot-starter-json-2.0.0.RELEASE.jar
      592  03-01-2018 05:23   application-distribution-0.0.1-SNAPSHOT/lib/spring-boot-starter-2.0.0.RELEASE.jar
      590  03-01-2018 05:24   application-distribution-0.0.1-SNAPSHOT/lib/spring-boot-starter-tomcat-2.0.0.RELEASE.jar
  1117582  01-08-2018 11:43   application-distribution-0.0.1-SNAPSHOT/lib/hibernate-validator-6.0.7.Final.jar
   782155  02-19-2018 10:08   application-distribution-0.0.1-SNAPSHOT/lib/spring-webmvc-5.0.4.RELEASE.jar
  1244848  02-19-2018 10:08   application-distribution-0.0.1-SNAPSHOT/lib/spring-web-5.0.4.RELEASE.jar
  1145432  03-01-2018 05:23   application-distribution-0.0.1-SNAPSHOT/lib/spring-boot-autoconfigure-2.0.0.RELEASE.jar
   922427  03-01-2018 05:23   application-distribution-0.0.1-SNAPSHOT/lib/spring-boot-2.0.0.RELEASE.jar
      613  03-01-2018 05:23   application-distribution-0.0.1-SNAPSHOT/lib/spring-boot-starter-logging-2.0.0.RELEASE.jar
    26586  03-01-2018 05:23   application-distribution-0.0.1-SNAPSHOT/lib/javax.annotation-api-1.3.2.jar
  1079064  02-19-2018 10:08   application-distribution-0.0.1-SNAPSHOT/lib/spring-context-5.0.4.RELEASE.jar
   360034  02-19-2018 10:08   application-distribution-0.0.1-SNAPSHOT/lib/spring-aop-5.0.4.RELEASE.jar
   654022  02-19-2018 10:08   application-distribution-0.0.1-SNAPSHOT/lib/spring-beans-5.0.4.RELEASE.jar
   263410  02-19-2018 10:08   application-distribution-0.0.1-SNAPSHOT/lib/spring-expression-5.0.4.RELEASE.jar
  1216414  02-19-2018 10:08   application-distribution-0.0.1-SNAPSHOT/lib/spring-core-5.0.4.RELEASE.jar
   297518  10-24-2017 03:28   application-distribution-0.0.1-SNAPSHOT/lib/snakeyaml-1.19.jar
    33391  01-30-2018 09:16   application-distribution-0.0.1-SNAPSHOT/lib/jackson-datatype-jdk8-2.9.4.jar
    98261  01-30-2018 09:16   application-distribution-0.0.1-SNAPSHOT/lib/jackson-datatype-jsr310-2.9.4.jar
     8643  01-30-2018 09:16   application-distribution-0.0.1-SNAPSHOT/lib/jackson-module-parameter-names-2.9.4.jar
  1345154  01-30-2018 09:16   application-distribution-0.0.1-SNAPSHOT/lib/jackson-databind-2.9.4.jar
   257309  02-14-2018 08:07   application-distribution-0.0.1-SNAPSHOT/lib/tomcat-embed-websocket-8.5.28.jar
  3109223  02-14-2018 08:07   application-distribution-0.0.1-SNAPSHOT/lib/tomcat-embed-core-8.5.28.jar
   240498  02-14-2018 08:07   application-distribution-0.0.1-SNAPSHOT/lib/tomcat-embed-el-8.5.28.jar
    93107  01-08-2018 11:43   application-distribution-0.0.1-SNAPSHOT/lib/validation-api-2.0.1.Final.jar
    66469  02-20-2018 12:53   application-distribution-0.0.1-SNAPSHOT/lib/jboss-logging-3.3.2.Final.jar
    65100  09-19-2017 07:31   application-distribution-0.0.1-SNAPSHOT/lib/classmate-1.3.4.jar
   290339  06-16-2017 04:40   application-distribution-0.0.1-SNAPSHOT/lib/logback-classic-1.2.3.jar
    17519  11-28-2017 08:42   application-distribution-0.0.1-SNAPSHOT/lib/log4j-to-slf4j-2.10.0.jar
     4596  03-27-2017 12:22   application-distribution-0.0.1-SNAPSHOT/lib/jul-to-slf4j-1.7.25.jar
    15836  02-19-2018 10:08   application-distribution-0.0.1-SNAPSHOT/lib/spring-jcl-5.0.4.RELEASE.jar
    66519  08-03-2017 05:28   application-distribution-0.0.1-SNAPSHOT/lib/jackson-annotations-2.9.0.jar
   320923  01-30-2018 09:16   application-distribution-0.0.1-SNAPSHOT/lib/jackson-core-2.9.4.jar
   471901  06-16-2017 04:40   application-distribution-0.0.1-SNAPSHOT/lib/logback-core-1.2.3.jar
    41203  03-27-2017 12:22   application-distribution-0.0.1-SNAPSHOT/lib/slf4j-api-1.7.25.jar
   255485  11-28-2017 08:42   application-distribution-0.0.1-SNAPSHOT/lib/log4j-api-2.10.0.jar
        0  03-19-2018 07:31   application-distribution-0.0.1-SNAPSHOT/bin/
     7002  03-19-2018 07:31   application-distribution-0.0.1-SNAPSHOT/bin/application-distribution
     4042  03-19-2018 07:31   application-distribution-0.0.1-SNAPSHOT/bin/application-distribution.bat
---------                     -------
 15926900                     42 files
查看更多
你好瞎i
4楼-- · 2020-07-06 21:24

spring boot 2.x

My project is multi-module, each module can execute jar

root build.gradle

subprojects{
  apply plugin: 'idea'
  apply plugin: 'java'
  apply plugin: 'org.springframework.boot'  
  }

 bootJar { enabled = true }
 jar {enabled = true} 
}

Sub-module build.gradle

bootJar {
    mainClassName = 'space.hi.HelloApplication'
}

Then you can click the bootRun submodule in the 'Tasks/application or build' of the idea gradle plugin, or click bootJar to generate the jar.

查看更多
Fickle 薄情
5楼-- · 2020-07-06 21:30

In spring-boot 2, the gradle plugin reconfigures the build to include the boot tar and zip distributions in the uploadArchives task when you apply the application and maven plugins.

From what I can tell from your question, you want a single zip-file with all the jar files in it, similar to what the application plugin creates, but want to exclude everything "extra" that the spring boot plugin adds? If that is the case it is a simple matter of telling gradle to do exactly that;

apply plugin: 'application'
apply plugin: 'maven'

jar.enabled = true
[bootJar, distTar, bootDistTar, bootDistZip]*.enabled = false
configurations.archives.artifacts.removeIf { !it.archiveTask.is(distZip) }

This is groovy, but hopefully you are able to apply this in a similar way in your kotlin file.

查看更多
甜甜的少女心
6楼-- · 2020-07-06 21:32

this work for me (disable spring-boot bootJar task, and enable jar)

apply plugin: 'java'
bootJar {
  enabled = false
}
查看更多
登录 后发表回答