使用Gradle构建EAR,在WAR之间共享依赖项。

huangapple go评论117阅读模式
英文:

Build an EAR with Gradle sharing dependencies between WARs

问题

我有一个类似这样的Java EE项目

  1. /project/war1/build.gradle
  2. /project/war2/build.gradle
  3. /project/jar1/build.gradle
  4. /project/jar2/build.gradle
  5. /project/ear/build.gradle

war1war2都依赖于jar1jar2模块。
jar1jar2模块依赖于第三方库(托管在mavenCentral),我们称其为third-party1.jarthird-party2.jar
war1war2还依赖于我想在它们之间共享的third-party3.jar库。

我期望的ear结构如下:

  1. META-INF/MANIFEST.MF
  2. META-INF/application.xml
  3. war1.war
  4. war2.war
  5. lib/jar1.jar
  6. lib/jar2.jar
  7. lib/third-party1.jar
  8. lib/third-party2.jar
  9. lib/third-party3.jar

这样正确吗?我如何通过gradle实现这一点?Gradle的ear插件文档并没有解释这一点,尽管这似乎是最常见的模式。

英文:

I have a java-ee project like this

  1. /project/war1/build.gradle
  2. /project/war2/build.gradle
  3. /project/jar1/build.gradle
  4. /project/jar2/build.gradle
  5. /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:

  1. META-INF/MANIFEST.MF
  2. META-INF/application.xml
  3. war1.war
  4. war2.war
  5. lib/jar1.jar
  6. lib/jar2.jar
  7. lib/third-party1.jar
  8. lib/third-party2.jar
  9. 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 应该像这样:

  1. plugins {
  2. id 'java'
  3. id 'ear'
  4. }
  5. dependencies {
  6. /* jars */
  7. earlib project(path: ':jar1')
  8. earlib project(path: ':jar1', configuration: 'compile')
  9. earlib project(path: ':jar2')
  10. earlib project(path: ':jar2', configuration: 'compile')
  11. /* wars */
  12. deploy project(path: ':war1', configuration: 'archives')
  13. earlib project(path: ':war1', configuration: 'earshared')
  14. deploy project(path: ':war2', configuration: 'archives')
  15. earlib project(path: ':war2', configuration: 'earshared')
  16. }

这样我们就能在 .ear 归档文件的根目录中得到 war 文件,earshared 依赖项会被放在 /lib 目录下。

jar1jar2 的构件及其依赖项也会被放在 /lib 下。

earshared 是一个自定义的配置,war 模块的 build.gradle 如下所示:

  1. configurations {
  2. earshared
  3. }
  4. apply plugin: 'war'
  5. jar.enabled = true
  6. description = 'war1'
  7. dependencies {
  8. providedCompile project(':jar1')
  9. providedCompile project(':jar2')
  10. earshared group: 'org.example', name: 'third-party3', version:'1.0.0'
  11. }
  12. sourceSets {
  13. main { compileClasspath += configurations.earshared }
  14. }

jar 模块的 build.gradle 如下所示:

  1. description = 'jar1';
  2. dependencies {
  3. compile group: 'org.example', name: 'third-party1', version:'2.6.0'
  4. compile group: 'org.example', name: 'third-party2', version:'2.6.0'
  5. }
英文:

so I think I finally figured it out:

the ear/build.gradle should be like this:

  1. plugins {
  2. id 'java'
  3. id 'ear'
  4. }
  5. dependencies {
  6. /* jars */
  7. earlib project(path: ':jar1')
  8. earlib project(path: ':jar1', configuration: 'compile')
  9. earlib project(path: ':jar2')
  10. earlib project(path: ':jar2', configuration: 'compile')
  11. /* wars */
  12. deploy project(path: ':war1', configuration: 'archives')
  13. earlib project(path: ':war1', configuration: 'earshared')
  14. deploy project(path: ':war2', configuration: 'archives')
  15. earlib project(path: ':war2', configuration: 'earshared')
  16. }

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:

  1. configurations {
  2. earshared
  3. }
  4. apply plugin: 'war'
  5. jar.enabled = true
  6. description = 'war1'
  7. dependencies {
  8. providedCompile project(':jar1')
  9. providedCompile project(':jar2')
  10. earshared group: 'org.example', name: 'third-party3', version:'1.0.0'
  11. }
  12. sourceSets {
  13. main { compileClasspath += configurations.earshared }
  14. }

while the jars' build.gradle looks like this:

  1. description = 'jar1'
  2. dependencies {
  3. compile group: 'org.example', name: 'third-party1', version:'2.6.0'
  4. compile group: 'org.example', name: 'third-party2', version:'2.6.0'
  5. }

huangapple
  • 本文由 发表于 2020年4月8日 01:44:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/61086156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定