英文:
Build an EAR with Gradle sharing dependencies between WARs
问题
我有一个类似这样的Java EE项目
/project/war1/build.gradle
/project/war2/build.gradle
/project/jar1/build.gradle
/project/jar2/build.gradle
/project/ear/build.gradle
war1和war2都依赖于jar1和jar2模块。
jar1和jar2模块依赖于第三方库(托管在mavenCentral),我们称其为third-party1.jar和third-party2.jar。
war1和war2还依赖于我想在它们之间共享的third-party3.jar库。
我期望的ear结构如下:
META-INF/MANIFEST.MF
META-INF/application.xml
war1.war
war2.war
lib/jar1.jar
lib/jar2.jar
lib/third-party1.jar
lib/third-party2.jar
lib/third-party3.jar
这样正确吗?我如何通过gradle实现这一点?Gradle的ear插件文档并没有解释这一点,尽管这似乎是最常见的模式。
英文:
I have a java-ee project like this
/project/war1/build.gradle
/project/war2/build.gradle
/project/jar1/build.gradle
/project/jar2/build.gradle
/project/ear/build.gradle
Both war1 and war2 depend on jar1 and jar2.
The jar1 and jar2 modules depend on third-party libraries (hosted by mavenCentral), let's call it third-party1.jar and third-party2.jar.
war1 and war2 also depend on third-party3.jar library I'd like to share between them.
What I would expect is an ear like this:
META-INF/MANIFEST.MF
META-INF/application.xml
war1.war
war2.war
lib/jar1.jar
lib/jar2.jar
lib/third-party1.jar
lib/third-party2.jar
lib/third-party3.jar
is this correct? How can I achieve this with gradle? The gradle ear plugin docs does not explain this at all, which seems to me the most common pattern.
答案1
得分: 1
所以我认为我终于弄清楚了:
ear/build.gradle 应该像这样:
plugins {
id 'java'
id 'ear'
}
dependencies {
/* jars */
earlib project(path: ':jar1')
earlib project(path: ':jar1', configuration: 'compile')
earlib project(path: ':jar2')
earlib project(path: ':jar2', configuration: 'compile')
/* wars */
deploy project(path: ':war1', configuration: 'archives')
earlib project(path: ':war1', configuration: 'earshared')
deploy project(path: ':war2', configuration: 'archives')
earlib project(path: ':war2', configuration: 'earshared')
}
这样我们就能在 .ear 归档文件的根目录中得到 war 文件,earshared 依赖项会被放在 /lib 目录下。
jar1 和 jar2 的构件及其依赖项也会被放在 /lib 下。
earshared 是一个自定义的配置,war 模块的 build.gradle 如下所示:
configurations {
earshared
}
apply plugin: 'war'
jar.enabled = true
description = 'war1'
dependencies {
providedCompile project(':jar1')
providedCompile project(':jar2')
earshared group: 'org.example', name: 'third-party3', version:'1.0.0'
}
sourceSets {
main { compileClasspath += configurations.earshared }
}
而 jar 模块的 build.gradle 如下所示:
description = 'jar1';
dependencies {
compile group: 'org.example', name: 'third-party1', version:'2.6.0'
compile group: 'org.example', name: 'third-party2', version:'2.6.0'
}
英文:
so I think I finally figured it out:
the ear/build.gradle should be like this:
plugins {
id 'java'
id 'ear'
}
dependencies {
/* jars */
earlib project(path: ':jar1')
earlib project(path: ':jar1', configuration: 'compile')
earlib project(path: ':jar2')
earlib project(path: ':jar2', configuration: 'compile')
/* wars */
deploy project(path: ':war1', configuration: 'archives')
earlib project(path: ':war1', configuration: 'earshared')
deploy project(path: ':war2', configuration: 'archives')
earlib project(path: ':war2', configuration: 'earshared')
}
this way we get the wars in the root of the .ear archive, the earshared dependencies are put under the /lib directory.
The jar1 and jar2 artifacts and their dependencies are also put under /lib
earshared is a custom configuration the wars' build.gradle looks like this:
configurations {
earshared
}
apply plugin: 'war'
jar.enabled = true
description = 'war1'
dependencies {
providedCompile project(':jar1')
providedCompile project(':jar2')
earshared group: 'org.example', name: 'third-party3', version:'1.0.0'
}
sourceSets {
main { compileClasspath += configurations.earshared }
}
while the jars' build.gradle looks like this:
description = 'jar1'
dependencies {
compile group: 'org.example', name: 'third-party1', version:'2.6.0'
compile group: 'org.example', name: 'third-party2', version:'2.6.0'
}
专注分享java语言的经验与见解,让所有开发者获益!

评论