Spring Boot Gradle构建:从Sping Boot Jar文件中排除资源文件

vuktfyat  于 2022-12-12  发布在  Spring
关注(0)|答案(2)|浏览(303)

我希望从jar文件中排除所有配置文件,因为它们将在配置时提供,并且构建路径中的不同版本可能会产生一些运行时问题。我正在使用以下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)
    }
tkqqtvp1

tkqqtvp11#

我能够通过使用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)
}
nqwrtyyt

nqwrtyyt2#

我发现这个解决方案很适合我:

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

...其中logback-spring.xml位于src/main/resources

相关问题