问题:考虑到以下多项目 gradle 构建

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

我正在寻找一种配置subproject-B的方法,以解压缩由war生成的subproject-A的内容,并将解压后的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)

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

原理: subproject-B是运行tomcat-embed-server的独立Java应用程序,带有JavaFX WebView,可访问本地主机上subproject-B的Web应用程序,从而将Web应用程序转变为独立的桌面应用程序。在Eclipse中可以很好地运行的是20行代码,但似乎对分发构成了包装方面的挑战。

最佳答案

我终于找到了解决方案,因此将其发布在这里,以帮助面临类似问题的人们:

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/**'
}

09-27 17:09