3

I created a Gradle project for a spring application. My build.gradle is as follows:

buildscript {
    ext {
        springBootVersion = '1.3.5.RELEASE'
    }
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath('org.asciidoctor:asciidoctor-gradle-plugin:1.5.3')
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'

jar {
    baseName = 'edge'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

bootRepackage {
    excludeDevtools = true
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-actuator-docs')
    compile('org.springframework.boot:spring-boot-starter-aop')
    compile('org.springframework.cloud:spring-cloud-starter-config')
    compile('org.springframework.cloud:spring-cloud-starter-eureka')
    compile('org.springframework.cloud:spring-cloud-starter-hystrix')
    compile('org.springframework.cloud:spring-cloud-starter-hystrix-dashboard')
    compile('org.springframework.cloud:spring-cloud-starter-ribbon')
    compile('org.springframework.cloud:spring-cloud-starter-zuul')
    compile('org.springframework.boot:spring-boot-starter-hateoas')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-redis')
    compile('org.projectlombok:lombok:1.16.6')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.session:spring-session')
    compile('org.springframework.boot:spring-boot-starter-web') {
        exclude module: 'spring-boot-starter-tomcat'
    }
    compile('org.springframework.boot:spring-boot-starter-undertow')
    compile('org.springframework.boot:spring-boot-starter-websocket')
    compile('com.h2database:h2')
    compile('org.springframework.boot:spring-boot-devtools')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Brixton.RELEASE"
    }
}

eclipse {
    classpath {
         containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
         containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
    }
}

Then every time I ran gradle build without any changes in the source code, several tasks were always executed while I think they should be up-to-date:

➜  octopus git:(master) ✗
➜  octopus git:(master) ✗ gradle :edge:build
:edge:compileJava UP-TO-DATE
:edge:processResources UP-TO-DATE
:edge:classes UP-TO-DATE
:edge:findMainClass
:edge:jar
:edge:bootRepackage
:edge:assemble
:edge:compileTestJava UP-TO-DATE
:edge:processTestResources UP-TO-DATE
:edge:testClasses UP-TO-DATE
:edge:test UP-TO-DATE
:edge:check UP-TO-DATE
:edge:build

BUILD SUCCESSFUL

Total time: 6.773 secs

This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.13/userguide/gradle_daemon.html
➜  octopus git:(master) ✗ md5 edge/build/libs/edge-0.0.1-SNAPSHOT.jar
MD5 (edge/build/libs/edge-0.0.1-SNAPSHOT.jar) = d0ff71c362d089559bbc627e78e2247a
➜  octopus git:(master) ✗ gradle :edge:build
:edge:compileJava UP-TO-DATE
:edge:processResources UP-TO-DATE
:edge:classes UP-TO-DATE
:edge:findMainClass
:edge:jar
:edge:bootRepackage
:edge:assemble
:edge:compileTestJava UP-TO-DATE
:edge:processTestResources UP-TO-DATE
:edge:testClasses UP-TO-DATE
:edge:test UP-TO-DATE
:edge:check UP-TO-DATE
:edge:build

BUILD SUCCESSFUL

Total time: 6.579 secs

This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.13/userguide/gradle_daemon.html
➜  octopus git:(master) ✗ md5 edge/build/libs/edge-0.0.1-SNAPSHOT.jar
MD5 (edge/build/libs/edge-0.0.1-SNAPSHOT.jar) = d1c3fc5c9d0c00e0130c8c65f23b6466
➜  octopus git:(master) ✗

As you can see, tasks 'findMainClass', 'jar', 'bootRepackage', 'assemble', and 'build' are considered as non up-to-date and always executed. And each time the generated jar file is different, as they have different MD5 digest.

So, why Gradle behave like this? How can I make them all up-to-date and speed up the build process?

Bourne Xi
  • 111
  • 1
  • 6

1 Answers1

2

By default, Spring Boot's bootRepackage task overwrites that jar that's created by the jar task. This means that the jar task is always considered to be out of date. You can avoid this by configuring bootRepackage with a classifier so that the repackaged fat jar is written to a separate location. For example:

bootRepackage  {
    classifier = 'exec'
}

You may also be interested in this issue which describes a number of improvements we'd like to make to Boot's Gradle plugin.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • Thanks. What actually bother me is that, I use `com.bmuschko:gradle-docker-plugin` to integrate with docker, which has a task to build docker image including the executable jar. Each time when I execute `gradle buildDockerImage` without change in source code, it will always build the docker image which is a time-consuming task. – Bourne Xi Jun 27 '16 at 11:56