当前位置: 首页 > 知识库问答 >
问题:

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

方野
2023-03-14

我最近完成了从1.5.8到2.1.14-RELEASE的Spring Boot迁移,并使用gradle作为构建脚本。我正在使用spring-boot-gradle插件和spring-boot依赖管理插件。我们正在使用gradle任务创建4个可执行jar(eureka、oAuth、apiGateWay、abcApplication),并将它们打包到tar文件中。在迁移之前,在jar生成期间,它使用BootRepackage的customConfiguration排除了一些jar。因此,我们的tar文件大小为650mb。现在迁移后,tar文件大小增加到850mb。当我检查tar文件时,它正在添加runtimeclasspath中的所有jar。所以尺寸变大了。你能帮我用BootJar排除配置属性中提到的jar吗。现在我们正在为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}"
    }
}
 

共有1个答案

姬衡
2023-03-14
匿名用户

假设< code>eurekaconfiguration包含应打包在< code>eurekaAppJar中的所有依赖项,您可以使用它和主源集的输出作为< code>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包含eurekaconfiguration classes中所有依赖项的jar,这些依赖项包含 src/main/resourcessrc/main/java

 类似资料:
  • null c)从命令行如何(PostgreSQL ones?): 编辑MAC OS#默认密码重置 已替换(md5为信任)

  • 我有一些代码,我不希望包含在jar文件基于一个条件。 我的构建脚本看起来像 现在,当我运行脚本时,除了危险的java文件,所有文件的绝对路径都被打印出来,但它们仍然包含在jar中。 如果我删除启动插件并运行任务,危险的类文件仍然包含在jar中。 如果我在jar任务中添加一个子句,则不会打印危险文件,它们也不会包含在jar中。 如果我启用启动插件,并使用bootJar任务(它继承自Jar任务)(),

  • 我正在为rest控制器编写junits。我只希望加载与控制器相关的最小上下文,我认为这就是加载上下文的方式。但是junit正在加载完整的Spring上下文,它失败了,因为一些bean无法创建。 我已经搜索并阅读了许多关于stackoverflow的问题,没有一个解决方案能够排除特定的配置。在为控制器编写junits时,如何加载最小的上下文?或者有没有办法排除一些配置类?我使用的是Spring bo

  • 问题内容: 如何用Java编写等效的代码? 问题答案: 您可能还希望将类本身公开,但这实际上是将字面量转换为Java。 C ++模板和Java泛型之间还有其他区别,但是对于您的示例,这些都不是问题。

  • 我有一个类,其中可能有几个枚举 每个枚举都应该有一个与每个条目关联的字符串值。为了实现这一点,我添加了一个参数化构造函数,一个支持字符串类的成员,并重写了toString方法。 然而,可以看到我的两个枚举之间的50%的代码是相同的。它只是支持将字符串映射到Enum值的代码。 如何将此代码移动到公共位置并避免代码重复? 编辑:当我写《美国》时,用例是很容易获得“纽约”。国家。纽约。 以下是我尝试的,

  • 有没有可能在科特林做类似跟随的事情?