简体   繁体   中英

Build an EAR with Gradle sharing dependencies between WARs

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.

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 war s 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'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM