gpt4 book ai didi

java - 从单个项目或源中生成不同的 gradle 工件

转载 作者:行者123 更新时间:2023-12-03 06:09:34 29 4
gpt4 key购买 nike

我有一个巨大的遗留 EAR 项目。当前的构建过程使用 Ant,我正在尝试转换为 gradle。

旧的 Ant 构建使用单个源文件夹,所有内容都在其中(EJB 和 WAR 代码一起);然后 Ant 使用不同的任务来构建 EJB-JAR 和 WAR 工件,按感兴趣的包过滤(my.web.* 用于 WAR,my.ejb.* 用于 EJB)。

不言而喻,EJB 和 WAR 相互大量引用,我想这就是为什么即使生成了单独的工件,它们也会被编译在一起的原因。

我尝试创建一个父 EAR 项目,然后分离 my.ejb.* 在 EJB 子项目中以及 my.web.* 在 WAR 子项目中,但 gradle 立即提示循环依赖,我还没有找到解决方法。

如果上述方法不可行,那么我正在寻找有关如何从将完全编译的相同代码库生成 EJB-JAR 和 WAR 工件的建议,然后将它们作为“部署”依赖项包含在 EAR 文件中。

我在谷歌上搜索过,但我对 gradle 不是很聪明(显然)。任何帮助将不胜感激。

最佳答案

我认为您在第一次尝试将现有的 ant 项目转换为 Gradle 时陷入了我们许多人所遇到的陷阱 - 您正在以同样的方式思考事情。

相反,让 Gradle 为您完成工作,它会更容易,生成的 build.gradle 文件会更小更简洁。

然而,考虑拥有一个父耳朵项目和一个单独的 war 项目是很好的想法。但是,您应该利用 Gradle 提供的插件来简化构建文件的任务。

使用“耳朵插件”:
https://docs.gradle.org/current/userguide/war_plugin.html

例如:

    apply plugin: 'ear'
apply plugin: 'java'

repositories { mavenCentral() }

dependencies {
// The following dependencies will be the ear modules and
// will be placed in the ear root
deploy project(':war')

// The following dependencies will become ear libs and will
// be placed in a dir configured via the libDirName property
earlib group: 'log4j', name: 'log4j', version: '1.2.15', ext: 'jar'
}

ear {
appDirName 'src/main/app' // use application metadata found in this folder
// put dependent libraries into APP-INF/lib inside the generated EAR
libDirName 'APP-INF/lib'
deploymentDescriptor { // custom entries for application.xml:
// fileName = "application.xml" // same as the default value
// version = "6" // same as the default value
applicationName = "customear"
initializeInOrder = true
displayName = "Custom Ear" // defaults to project.name
// defaults to project.description if not set
description = "My customized EAR for the Gradle documentation"
// libraryDirectory = "APP-INF/lib" // not needed, above libDirName setting does this
// module("my.jar", "java") // won't deploy as my.jar isn't deploy dependency
// webModule("my.war", "/") // won't deploy as my.war isn't deploy dependency
securityRole "admin"
securityRole "superadmin"
withXml { provider -> // add a custom node to the XML
provider.asNode().appendNode("data-source", "my/data/source")
}
}
}

然后对于你的 war ,使用“ war 插件”。您可以将 war 创建为单独的项目:
    configurations {
moreLibs
}

repositories {
flatDir { dirs "lib" }
mavenCentral()
}

dependencies {
compile module(":compile:1.0") {
dependency ":compile-transitive-1.0@jar"
dependency ":providedCompile-transitive:1.0@jar"
}
providedCompile "javax.servlet:servlet-api:2.5"
providedCompile module(":providedCompile:1.0") {
dependency ":providedCompile-transitive:1.0@jar"
}
runtime ":runtime:1.0"
providedRuntime ":providedRuntime:1.0@jar"
testCompile "junit:junit:4.12"
moreLibs ":otherLib:1.0"
}

war {
from 'src/rootContent' // adds a file-set to the root of the archive
webInf { from 'src/additionalWebInf' } // adds a file-set to the WEB-INF dir.
classpath fileTree('additionalLibs') // adds a file-set to the WEB-INF/lib dir.
classpath configurations.moreLibs // adds a configuration to the WEB-INF/lib dir.
webXml = file('src/someWeb.xml') // copies a file to WEB-INF/web.xml
}

还有一篇关于将它们创建为单个项目的有用帖子,但这肯定有点“冒险”:
https://discuss.gradle.org/t/single-project-with-jar-ejb-war-and-ear/5874

关于java - 从单个项目或源中生成不同的 gradle 工件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36023353/

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