在多项目gradle构建中如何将子项目-A.war的内容打包到子项目-B.jar中

In a multi-project gradle build how do I pack the content of the subproject-A.war into the subproject-B.jar

问题:给定以下多项目gradle构建

superproject
    subproject-A -> war
    subproject-B -> jar

我正在寻找一种方法来配置 subproject-B 以解压缩 subproject-A 生成的 war 的内容,并具有解压缩的 webapp 目录的内容( 类 和用于部署到容器中的资源)在根级别与 类、资源和后者的依赖项一起打包到 subproject-B 的应用程序分发中。因此,为 subproject-B 生成的分布结构应如下所示:

subproject-B-0.1.0
    bin/...
    lib/
        META-INF/ (<-- from B)
        WEB-INF/  (<-- from A.war)
        css/      (<-- from A.war)
        js/       (<-- from A.war)
        *.jar     (code and dependencies of B)

已回答将资源从 A 复制到 B 的问题 。在这里我需要复制一个动态生成的构建工件的内容(通常复制到 main/resources 就可以了,因为后者将被打包到 jar)。

基本原理: subproject-B 是一个独立的 Java 应用 运行ning tomcat-嵌入-server 使用 JavaFX WebViewlocalhost 上访问 subproject-B 的 Web 应用程序否则网络应用程序变成一种独立的桌面应用程序。这是 20 行代码,运行 在 Eclipse 中很好,但似乎对分发打包提出了挑战。

我终于找到了解决方案,因此将其张贴在这里,以供面临类似问题的人受益:

subprojectA/build.gradle

apply plugin: "java"
apply plugin: "war"

archivesBaseName = "subprojectA"

sourceCompatibility = 1.8
compileJava.options.encoding = "utf-8"

war {
    manifest {
        archiveName = "$baseName.$extension"
        attributes "Implementation-Title": archivesBaseName, 
                   "Implementation-Version": version
    }
}

// declare configuration to refer to in superprojectB
configurations {
    subprojectAwar
}
// make this configuration deliver the generated war
dependencies {
    subprojectAwar files(war.archivePath)
}

subprojectB/build.gradle

apply plugin: "java"
apply plugin: 'application'

archivesBaseName = "subprojectB"

sourceCompatibility = 1.8
compileJava.options.encoding = "utf-8"

// declare configuration to take files from
configurations {
    subprojectAwar
}

dependencies {
    // bind the configuration to the respective configuration in subprojectA
    subprojectAwar project(path: ":subprojectA", configuration: "subprojectAwar")

    compile "org.apache.tomcat.embed:tomcat-embed-core:$tomcatEmbedVersion"
    compile "org.apache.tomcat.embed:tomcat-embed-websocket:$tomcatEmbedVersion"
    compile "org.apache.tomcat.embed:tomcat-embed-logging-log4j:$tomcatEmbedVersion"
    compile "org.apache.tomcat.embed:tomcat-embed-jasper:$tomcatEmbedVersion"
}

mainClassName = 'org.project.subprojectB.StartServer'

jar {
    // make sure this project is assembled after the war is generated
    dependsOn(":subprojectA:assemble")

    manifest {
        archiveName = "$baseName.$extension"
        attributes "Implementation-Title": archivesBaseName,
                   "Implementation-Version": version
        attributes 'Main-Class': '$mainClassName'
    }
    // if you need to copy the content of the war into the jar:
    // (otherwise only to the distribution, see below)
    /*
    from(zipTree(configurations.subprojectAwar.collect { it }[0])) {
        into ""
        exclude '**/META-INF/**'
    }
    */
}

// copy the content of the war excluding META-INF into the lib of superprojectB
applicationDistribution.from(zipTree(configurations.subprojectAwar.collect { it }[0])) {
     into "lib"
     exclude '**/META-INF/**'
}