简体   繁体   English

Gradle 构建:从 Spring 引导文件中排除资源文件 Jar 文件

[英]Gradle build: Exclude resources files from Spring Boot Jar file

I would like to exclude all config files from the jar file since they will be provided at the provisioning and having a different version in the build path may create some runtime issues.我想从 jar 文件中排除所有配置文件,因为它们将在配置时提供,并且在构建路径中具有不同的版本可能会产生一些运行时问题。 I am using the following Gradle build script, but for some reason, I can still see whatever exist in the resources directory to be copied into the built Jar. Which means for some reason the provided Gradle build is not working as expected.我正在使用以下 Gradle 构建脚本,但出于某种原因,我仍然可以看到资源目录中存在的任何内容被复制到构建的 Jar 中。这意味着由于某种原因,提供的 Gradle 构建没有按预期工作。

apply plugin: 'distribution'

    distributions {
        main {
            baseName = "${project.name}"
            contents {
                into('/conf'){
                    from('src/main/resources')
                    exclude("application.yml")
                }
                into('/lib'){
                    from('build/libs')
                }
                into('/bin'){
                    from('../bin')
                }
            }
        }
    }


    processResources {
        # Not sure how I need to point to the resources, so I included both. However, none is working.
        exclude('resources/*')
        exclude('src/main/resources/*')
    }

    bootJar{
        # Not sure how I need to point to the resources, so I included both. However, none is working.
        exclude('resources/*')
        exclude('src/main/resources/*')    
    }

    distTar {
        dependsOn bootJar
    }

    tasks.withType(Tar) {
        compression = Compression.GZIP
        extension = "tar.gz"
    }

    configurations {
        customArch
    }

    artifacts {
        customArch file(distTar.archivePath)
    }

I was able to exclude resources from appearing in the Jar file by using processResources.enabled = false , so the build file is as follows. 通过使用processResources.enabled = false ,我能够排除资源使其不显示在Jar文件中,因此构建文件如下。

apply plugin: 'distribution'

distributions {
    main {
        baseName = "${project.name}"
        contents {
            into('/conf'){
                from('src/main/resources')
                exclude("application.yml")
            }
            into('/lib'){
                from('build/libs')
            }
            into('/bin'){
                from('../bin')
            }
        }
    }
}

processResources.enabled = false

distTar {
    dependsOn bootJar
}

tasks.withType(Tar) {
    compression = Compression.GZIP
    extension = "tar.gz"
}

configurations {
    customArch
}

artifacts {
    customArch file(distTar.archivePath)
}

I found that this solution works for me:我发现这个解决方案对我有用:

processResources {
    exclude('logback-spring.xml')
}

...where logback-spring.xml is located in src/main/resources ...其中logback-spring.xml位于src/main/resources

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM