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

时间:2019-06-17 04:09:22

标签: java spring-boot gradle build.gradle

我想从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)
    }

1 个答案:

答案 0 :(得分:0)

通过使用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)
}