如何使用类似于 BootRepackage 中的 customConfiguration 的 BootJar 排除 jar?

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

我最近从 1.5.8 to 2.1.14-RELEASE 完成了 spring 引导迁移,并使用 gradle 作为构建脚本。我正在使用 spring-boot-gradle-plugin 和 spring-boot-dependency-management 插件。我们正在使用 gradle 任务创建 4 个可执行 jar(eureka、oAuth、apiGateWay、abcApplication)并将它们打包到 tar 文件中。在迁移之前,在 jars 生成期间,它使用 customConfiguration of BootRepackage 排除了一些 jars。因此,我们的 tar 文件大小为 650mb。现在迁移后 tar 文件大小增加到 850mb。当我检查 tar 文件时,它正在添加来自运行时类路径的所有 jar。所以尺寸变大了。你能帮我用 BootJar 排除配置 属性 中提到的 jar 吗?现在我们正在为 gradle 中的所有依赖项使用编译配置。我已经提到了完整的 gradle 文件。

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}"
    }
}
 

假设 eurekaconfiguration 包含所有应该打包在你的 eurekaAppJar 中的依赖项,你可以使用它并且主源的输出设置为 eurekaAppJar 的类路径:

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

这将导致 BOOT-INF/lib 包含 eurekaconfigurationBOOT-INF 中所有依赖项的 jar 类 包含来自 src/main/resources 和类 通过编译 src/main/java 中的所有内容生成。