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

时间:2015-09-12 23:08:11

标签: java tomcat gradle build.gradle multi-project

问题:鉴于以下多项目 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)

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

基本原理: subproject-B是运行 tomcat-embed-server 的独立Java应用程序,其中 JavaFX WebView 访问网络 localhost 上的subproject-B应用将其他网络应用转变为一种独立的桌面应用。它是20行代码,在Eclipse中运行良好,但似乎对分发提出了包装挑战。

1 个答案:

答案 0 :(得分:1)

我终于找到了一个解决方案,所以在这里发布它是为了面对类似问题的人的利益:

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