Detailed examples of specifying modules to put in EAR?

I’m trying to use the ear plugin to assemble a conventional EAR file. The “Ear” chapter of the user guide seems incomplete.

Is there any way to specify the required modules? The single example in the chapter doesn’t cover this. My Maven build didn’t require an “application.xml” file, as I specify everything I need in the POM plugin configuration.

Modules you wan to include in your EAR need only be added to the ‘deploy’ configuration. See here for details.

Additionally, as with Maven, you do not have to explicitly provide an application.xml file. Gradle will generate one for you, as described here.

Ah. What if my application is a single WAR, and I want to build the WAR and EAR at the same time? I don’t have a separate module to specify in the “deploy” configuration.

Yes, your project can apply both ‘war’ and ‘ear’ plugins. Take a look at this StackOverflow question for an example.

Ok. From this, I tried the following:

ext {
 warPath = "${buildDir}/libs/" + group + "-" + "war-" + version + ".war"
}
...
dependencies {
 deploy files(warPath)
 ...
}

I don’t like that file path expression very much. Can the “war” plugin tell me the path to the artifact it intends to produce?

In addition, the EAR this produced had more than I expected. In addition to containing the WAR file, it also had the entire tree of compiled classes and the web content tree.

No need to explicitly construct the path to the WAR. You can simply depend on the ‘war’ task itself. Gradle will determine the output of the task. Also, you can simply tell the ‘ear’ task to exclude all files contained in the main sourceset.

dependencies {

deploy files(war)

}

ear {

exclude { it.file in sourceSets.main.output.asFileTree.files }

}

Could you clarify what “files(war)” is doing, piece by piece? I don’t understand what the “war” represents here. I’m afraid this is where Groovy’s “expressiveness” is approaching APL. :slight_smile:

That exclusion took care of the classes, but I think it is still creating the folders from that tree, although all of them are empty. it also apparently has all the folders from “src/main/resources”, but no files.

Part of a more general question, but since the Eclipse Gradle plugin doesn’t do completions very well, are there some reasonable shortcuts for quickly seeing available properties and methods at the point of any particular dotted expression?

IDE support is still evolving but IntelliJ tends to have the best support for the Gradle DSL.

As far as the syntax ‘files(war)’, basically the ‘dependencies {…}’ block delegates to an instance of ‘DependencyHandler’. Dependencies can be declared a number of ways. One of which is a file dependency. Declaring a file dependency requires passing a ‘FileColleciton’, which can be constructed by calling the ‘Project.files()’ and passing a compatible argument type, one of which is a task. In this snippet, ‘war’ refers to a task added by the war plugin.

Ok, and how about excluding the folders from classes and src/main/resources?

Not sure where the stray classes would be coming from. You can ignore the empty directories however.

ear {

includeEmptyDirs false

}

Ok. One more element.

I don’t have a hand-coded application.xml, but I do have a “weblogic-application.xml” that has to go in the META-INF folder of the EAR. I have this file in “src/main/resources”, but not in a META-INF subfolder of that.

I tried setting the “appDirName” property to “src/main/resources”, but that had no effect. Even if that did copy the file, I imagine it would have put it in the root, not the META-INF folder.

What would be the best way to make this happen, without modifying the structure of the current application?

The ‘appDirName’ property is effectively ignored since you are additionally adding the ‘war’ plugin to the project. That is primarily used for a traditional EAR project which packages WARs from other projects.

If you want to add files to META-INF just call ‘metaInf()’ on the ‘ear’ task. However, you may have to move that file to another directory otherwise our main source set exclusion might filter it out.

ear {

metaInf {

from ‘src/main/metainf’

}

}

Ok, that worked. I also had to add an “include” property setting to “metainf” so it only includes that one file. Looks good.