在 Spring Boot 中构建分层 jar 时,如何将多模块项目 jar 包含在一个层中?

When building layered jars in Spring Boot, how do you include a multi-module projects jars in a layer?

根据 Spring Boot gradle plugin reference,我应该能够将特定模式的 jar 打包到特定层中(以便制作更好的 docker 文件)。

我对文档中使用的模式匹配感到困惑。这是一个例子:

tasks.getByName<BootJar>("bootJar") {
    layered {
        isIncludeLayerTools = true
        application {
            intoLayer("spring-boot-loader") {
                include("org/springframework/boot/loader/**")
            }
            intoLayer("application")
        }
        dependencies {

            intoLayer("module-dependencies") {
                include("com*:*:*")
            }

            intoLayer("dependencies")
        }
        layerOrder = listOf("dependencies", "spring-boot-loader", "module-dependencies", "application")
    }
}

我不明白的是这个模式匹配匹配的是什么:

intoLayer("模块依赖") { include("com*::") }

是jar的group、artifact、version吗?是罐子的名字吗?

如果我有一个包含模块 aa、ab 和 ac 的多模块项目,相当于 aa.jar、ab.jar 和 ac.jar 以及一个外部依赖项 org.something :anartifact:25 等同于 anartifact-25.jar 我需要添加什么模式以在一层中包含 aa、ab 和 ac 以及在另一层中包含所有其他依赖项?

对于模块依赖性,模式是 <group>:<artifactid>:<version>。您可以使用尾随通配符来匹配项目的子集或完全省略该项目以匹配所有内容。例如,com.fasterxml.jackson:: 将匹配 com.fasterxml.jackson 组中的所有工件和所有版本。

对于 multi-module 项目,默认情况下 artifactid 是项目名称,groupgroup 值集的值在你的 build.gradle.

通常在根项目的 build.gradle 文件中定义组,例如:

allprojects {
  group "com.example"
  version = '0.0.1-SNAPSHOT'
  repositories {
    mavenCentral()
  }
}

然后您可以在应用程序模块中按如下方式定义图层模式:

bootJar {
    layered {
        application {
            intoLayer("spring-boot-loader") {
                include("org/springframework/boot/loader/**")
            }
            intoLayer("application")
        }
        dependencies {
            intoLayer("module-dependencies") {
                include("com.example:*:*")
            }
            intoLayer("dependencies")
        }
        layerOrder = [ "dependencies", "spring-boot-loader", "module-dependencies", "application" ]
    }
}

我已经将示例上传到 https://github.com/philwebb/mutli-module-layered-gradle-example,它在一个完整的项目中展示了这一点。