javaspring-bootgradlespring-boot-gradle-pluginspring-boot-migration-1.5.x-2.1.x

How can I exclude the jars using BootJar similar to customConfiguration in BootRepackage?


I have recently done the spring boot migration from 1.5.8 to 2.1.14-RELEASE and using gradle as a build script. I am using spring-boot-gradle-plugin and spring-boot-dependency-management plugins. We are creating 4 executable jars (eureka, oAuth, apiGateWay, abcApplication) using gradle tasks and packaging them in tar file. Before the migration, during the jars generation it was excluding some jars using customConfiguration of BootRepackage. Because of this our tar file size was 650mb. Now after the migration tar file size increased to 850mb. When I checked the tar file, it is adding all the jars from the runtimeclasspath. So the size got increased. Could you please help me to exclude the jars mentioned in the configurations property using BootJar. Right now we are using compile configuration for all of the dependencies in gradle. I have mentioned the complete gradle file.

buildscript {
    repositories {
        maven {
            url '...'
            ...
        }

    }

    ext {
        springBootVersion = '2.1.14.RELEASE'
        springCloudVersion = 'Greenwich.SR4'
        springRetryVersion = "1.2.3.RELEASE"
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.13.0"
    }
    ext['tomcat.version'] = '8.5.64'
    ext['jackson.version'] = '2.11.2'
    ext['hibernate.version'] = '5.4.24.Final'
    ext['snakeyaml.version'] = '1.26'
}
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'idea'
apply plugin: "com.jfrog.artifactory"
apply plugin: 'io.spring.dependency-management'

sourceCompatibility = 1.8

ext {
    excludeCLoudJar = '**spring-cloud-config-server*'
    exludedJarsForEureka = ['**spring-cloud-config-server*', '**poi-ooxml*', '**guava*', '**ojdbc8*', '**springfox-swagger2*', '**springfox-swagger-ui*']
}

configurations.all {
    resolutionStrategy {
        force 'org.bouncycastle:bcpkix-jdk15on:1.64'
    }
}
configurations {
    all*.exclude group: 'aopalliance'
    cloudconfigexclusion.exclude group: 'org.springframework.cloud', module: 'spring-cloud-config-server'
    cloudconfigexclusion.extendsFrom(compile)
}
configurations {
    eurekaconfiguration.exclude group: 'org.springframework.cloud', module: 'spring-cloud-config-server'
    eurekaconfiguration.exclude group: 'org.springframework.cloud', module: 'spring-cloud-config-client'
    eurekaconfiguration.exclude group: 'org.springframework.cloud', module: 'spring-cloud-starter-config'
    eurekaconfiguration.exclude group: 'org.springframework.cloud', module: 'spring-cloud-starter-oauth2'
    eurekaconfiguration.exclude group: 'org.springframework.cloud', module: 'spring-cloud-starter-zuul'
    eurekaconfiguration.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-data-jpa'
    eurekaconfiguration.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-aop'
    eurekaconfiguration.exclude group: 'org.springframework.security', module: 'spring-security-config'
    eurekaconfiguration.exclude group: 'org.springframework.security', module: 'spring-security-crypto'
    eurekaconfiguration.exclude group: 'org.springframework.security', module: 'spring-security-core'
    eurekaconfiguration.exclude group: 'org.springframework.security', module: 'spring-security-web'
    eurekaconfiguration.exclude group: 'org.apache.poi'
    eurekaconfiguration.exclude group: 'com.opencsv'
    eurekaconfiguration.exclude group: 'org.springframework.retry'
    eurekaconfiguration.exclude group: 'com.oracle', module: 'ojdbc8'
    eurekaconfiguration.extendsFrom(compile)
}

configurations {
    zuulconfiguration.exclude group: 'org.springframework.cloud', module: 'spring-cloud-config-server'
    zuulconfiguration.exclude group: 'org.springframework.cloud', module: 'spring-cloud-config-client'
    zuulconfiguration.exclude group: 'org.springframework.cloud', module: 'spring-cloud-starter-oauth2'
    zuulconfiguration.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-data-jpa'
    zuulconfiguration.exclude group: 'org.springframework.security', module: 'spring-security-config'
    zuulconfiguration.exclude group: 'org.springframework.security', module: 'spring-security-crypto'
    zuulconfiguration.exclude group: 'org.springframework.security', module: 'spring-security-core'
    zuulconfiguration.exclude group: 'org.springframework.security', module: 'spring-security-web'
    zuulconfiguration.exclude group: 'org.apache.poi'
    zuulconfiguration.exclude group: 'com.opencsv'
    zuulconfiguration.exclude group: 'com.oracle', module: 'ojdbc8'
    zuulconfiguration.extendsFrom(compile)
}

configurations {
    cloudconfigconfiguration.exclude group: 'org.springframework.cloud', module: 'spring-cloud-starter-oauth2'
    cloudconfigconfiguration.exclude group: 'org.springframework.cloud', module: 'spring-cloud-config-client'
    cloudconfigconfiguration.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-data-jpa'
    cloudconfigconfiguration.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-aop'
    cloudconfigconfiguration.exclude group: 'org.springframework.security', module: 'spring-security-config'
    cloudconfigconfiguration.exclude group: 'org.springframework.security', module: 'spring-security-crypto'
    cloudconfigconfiguration.exclude group: 'org.springframework.security', module: 'spring-security-core'
    cloudconfigconfiguration.exclude group: 'org.springframework.security', module: 'spring-security-web'
    cloudconfigconfiguration.exclude group: 'org.apache.poi'
    cloudconfigconfiguration.exclude group: 'com.opencsv'
    cloudconfigconfiguration.exclude group: 'org.springframework.retry'
    cloudconfigconfiguration.exclude group: 'com.oracle', module: 'ojdbc8'
    cloudconfigconfiguration.extendsFrom(compile)
}

mainClassName = "..."

// During Migration changed from Jar to BootJar
task eurekaAppJar(type: BootJar) {
    baseName = 'eurekaJar'
    version = '0.0.1'
    println sourceSets.main.output
    manifest {
        attributes 'Main-Class': "org.springframework.boot.loader.JarLauncher"
        attributes 'Start-Class': "com.abc.abcCompany.service.eurekaApp.EurekaApplication"
        attributes 'Implementation-Version': "001"
    }
    bootJar {
        mainClassName = "com.abc.abcCompany.service.eurekaApp.EurekaApplication"
    }
    classpath sourceSets.main.runtimeClasspath
}
// During Migration changed from Jar to BootJar
task oAuthConfigJar(type: BootJar) {
    baseName = 'oAuthConfigJar'
    version = '0.0.1'

    manifest {
        attributes 'Main-Class': "org.springframework.boot.loader.JarLauncher"
        attributes 'Start-Class': "com.abc.abcCompany.service.authserver.AuthServerApplication"
        attributes 'Implementation-Version': "001"

    }
    springBoot {
        mainClassName = "com.abcCompany.service.authserver.AuthServerApplication"
    }
    classpath sourceSets.main.runtimeClasspath
}
// During migration changed from BootRepackage to BootJar
task eurekaBoot(type: BootJar, dependsOn: eurekaAppJar) {
    mainClassName = 'com.abc.abcCompany.service.eurekaApp.EurekaApplication'
// During migration commented the below code. The below code helped to exclude jars using BootRepackage
//        customConfiguration = "eurekaconfiguration"
//        withJarTask = eztrackerEurekaJar
}


// During migration changed from BootRepackage to BootJar
task oAuthConfigJarBoot(type: BootJar, dependsOn: oAuthConfigJar) {
    mainClassName = 'com.abc.abcCompany.service.authserver.AuthServerApplication'
// During migration commented the below code. The below code helped to exclude jars using BootRepackage
//        customConfiguration = "zuulconfiguration"
//        withJarTask = eztrackerApiGatewayJar
}




dependencies {
    runtime("org.springframework.boot:spring-boot-properties-migrator")
    compile("org.springframework.cloud:spring-cloud-starter-netflix-eureka-server")
    compile("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")

    compile group: 'com.google.guava', name: 'guava', version: '30.0-jre'

    compile("org.springframework.cloud:spring-cloud-starter-netflix-zuul")

    implementation('org.springframework.cloud:spring-cloud-starter-oauth2')
    implementation('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile(group: 'com.oracle.jdbc', name: 'ojdbc8', version: '12.2.0.1')
    compile("io.springfox:springfox-swagger2:2.9.2")
    compile("io.springfox:springfox-swagger-ui:2.9.2")
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile("org.apache.poi:poi-ooxml:4.1.2")
    compile 'com.opencsv:opencsv:5.2'
    compile "javax.mail:mail:1.4.4"
    compile("org.springframework.cloud:spring-cloud-config-server")
    compile("org.springframework.cloud:spring-cloud-config-client:1.4.6.RELEASE") {
        force = true
    }

    compile("org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}")
    compile("org.springframework.boot:spring-boot-starter-aop")
    compile("org.springframework.retry:spring-retry:${springRetryVersion}")
    compile("org.springframework.boot:spring-boot-starter-web-services")
    compile('com.thoughtworks.xstream:xstream:1.4.10') {
        force = true
    }
    compile('commons-io:commons-io:2.7') {
        force = true
    }
    compile('io.netty:netty-transport:4.1.63.Final') {
        force = true
    }
    compile('io.netty:netty-transport-native-epoll:4.1.63.Final') {
        force = true
    }
    compile('org.dom4j:dom4j:2.1.3') {
        force = true
    }
    compile("wsdl4j:wsdl4j:1.6.1")
    compile('io.netty:netty-codec:4.1.63.Final') {
        force = true
    }
    compile('org.apache.commons:commons-math3:3.6') {
        force = true
    }
    compile('org.springframework:spring-expression:5.2.12.RELEASE') {
        force = true
    }
        compile('io.netty:netty-codec-http:4.1.63.Final') {
        force = true
    }
    compile('com.thoughtworks.xstream:xstream:1.4.16') {
        force = true
    }
    compile('commons-beanutils:commons-beanutils:1.9.4') {
        force = true
    }
    

    compile group: 'org.postgresql', name: 'postgresql', version: '42.2.13'

    compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
}

    bootJar.enabled = false // disable default bootRepackage since we have custom repackage tasks
//    bootJar.withJarTask = jar // use custom Jar repackaging using the generated jar from the jar tasks
bootJar.dependsOn = [oAuthConfigJarBoot, eurekaBoot, ...]

distTar() {

    from('scripts') {
        include '**/*'
        into 'bin'
    }
    from(oAuthConfigJar.archivePath)
    from(eurekaJar.archivePath)
    from(abcApplicationApiGatewayJar.archivePath)
    from(cloudConfigServerJar.archivePath)

    archiveName 'abcApplicationService.tar'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}
 

Solution

  • Assuming that eurekaconfiguration contains all of the dependencies that should be packaged inside your eurekaAppJar, you can use it and the output of the main source set as eurekaAppJar's classpath:

    task eurekaAppJar(type: BootJar) {
        baseName = 'eurekaJar'
        version = '0.0.1'
        manifest {
            attributes 'Main-Class': "org.springframework.boot.loader.JarLauncher"
            attributes 'Start-Class': "com.abc.abcCompany.service.eurekaApp.EurekaApplication"
            attributes 'Implementation-Version': "001"
        }
        bootJar {
            mainClassName = "com.abc.abcCompany.service.eurekaApp.EurekaApplication"
        }
        classpath configurations.eurekaconfiguration.plus(sourceSets.main.output)
    }
    

    This will result in BOOT-INF/lib containing the jars of all of the dependencies in eurekaconfiguration and BOOT-INF classes containing the resources from src/main/resources and the classes produced by compiling everything in src/main/java.