gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:18:37 26 4
gpt4 key购买 nike

问题:给定以下多项目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 的问题已得到解答 here .在这里,我需要复制一个动态生成的构建工件的内容(通常将其复制到 main/resources 中就可以了,因为后者将被打包到 jar 中).

基本原理 subproject-B 是一个独立的 Java 应用程序,运行 tomcat-embed-serverJavaFX WebViewlocalhost 上访问 subproject-B 的网络应用程序,将其他网络应用程序变成一种独立的桌面应用程序。这 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/**'
}

关于java - 在多项目 gradle 构建中,如何将 subproject-A.war 的内容打包到 subproject-B.jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32544667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com