Gradle Tip: ear packaging

Do you have trouble creating the ear layout you like to see? Maybe this little tip will help.

Let’s assume we have an ejb.jar which should go into the ear root. Its dependencies should be placed in the lib folder of the ear.

app.ear
|-- ejb.jar
\-- lib
    |-- dep.jar
    |-- ...

The ear plugin does have two configurations which are used to control what is put where into the ear.

  1. first is the deploy configuration. All dependencies will be placed into the ear root. deploy is not transitive, ie. if we use deploy project(':ejb'), ejb.jar will be placed into the root of the ear and it will ignore all dependencies of project(':ejb'):
    app.ear
    \-- ejb.jar
    
  2. second is the earlib configuration. All dependencies will be placed into the lib folder of the ear. earlib is transitive, ie. if we use earlib project(':ejb'), ejb.jar and all dependencies of project(':ejb') will be placed into lib:
    app.ear
    |-- lib
        |-- ejb.jar
        \-- dep.jar
    

Looks like both do not what we need.

The trick is to use both.

deploy to put project(':ejb') into the root and earlib to put its dependencies into lib:

dependencies {
    deploy project(':ejb')
    earlib project(':ejb')
}

The result will look like this:

app.ear
|-- ejb.jar
\-- lib
    |-- ejb.jar  // ups...!
    |-- dep.jar
    |-- ...

Better but still not what we want. We now have an ejb.jar in root and one in lib. How do we get rid of the duplicate ejb.jar?

The trick is to explicitly select the compile configuration of project(':ejb')

The compile configuration does only contain the dependencies and not the ejb.jar artifact. Exactly what should go into the lib folder (we can’t use the runtime configuration because it does include the ejb.jar, we will need it at runtime.. :-)):

dependencies {
    deploy project(':ejb')
    earlib project(path: ':ejb', configuration: 'compile')
}

Which does create the ear we want:

app.ear
|-- ejb.jar
\-- lib
    |-- dep.jar
    |-- ...

Finally!

3 thoughts on “Gradle Tip: ear packaging

  1. We are missing now some of the runtime dependencies of ejb.jar in the lib folder. Obviously these runtime-only dependencies do not appear in the compile configuration. How to cope with that?

Leave a comment