Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Gradle Ear plug-in


May 25, 2021 Gradle


Table of contents


Ear plug-in

The Ear plug-in adds support for assembling EAR files for web applications. I t adds a default EAR archive task. It does not require a Java plug-in, but for projects that use the Java plug-in, it disables the build of the default JAR archive.

Usage

To use Ear's plug-in, include the following statement in the build script:

Use the Ear plug-in

build.gradle

apply plugin: 'ear'  

Tasks

The Ear plug-in adds the following tasks to the project.

Table 27.1. Ear plug-in - tasks

The name of the task Depends on Type Describe
ear compile (only when the Java plug-in is also configured) ear Assemble the application EAR file.

The Ear plug-in adds the following dependencies to the tasks that the underlying plug-in joins.

Table 27.2. Ear plug-in - additional task dependency

The name of the task Depends on
assemble ear

The project layout

Table 27.3. Ear plug-in - project layout

Directory Significance
src/main/application Ear resources, such as meta-INF directories

Rely on management

The Ear plug-in adds two dependency configurations: deploy and earlib. A ll dependencies in the deploy configuration are placed in the root of the EAR file and are not passable. All dependencies in the earlib configuration are placed in the "lib" directory of the EAR file and are deliverable.

Convention properties

Table 27.4. Ear Plug-in - Directory Properties

The name of the property Type The default Describe
appDirName String src/main/application The application source directory name relative to the project directory.
libDirName String into(<s2>'libs'</s2>) { The lib directory name in the generated EAR file.
deploymentDescriptor org.gradle.plugins.ear.descriptor.DeploymentDescriptor Deployment descriptor, which has a reasonable application.xml application The metadata used to generate the deployment descriptor file, such ear.deploymentDescriptor is ignored.

These properties are provided by an EarPluginConvention convention object.

Ear

Ear task's default behavior is to copy the contents of src/main/application to the root of the archive. If your application directory does not contain a meta-INF/application .xml deploy descriptor, you will be generated.

See also Ear.

Custom

Here's an example of the most important customization options:

Customization of the ear plug-in

build.gradle

apply plugin: 'ear'
apply plugin: 'java'
repositories { mavenCentral() }
dependencies {
    //following dependencies will become the ear modules and placed in the ear root
    deploy project(':war')
    //following dependencies will become ear libs and placed in a dir configured via libDirName property
    earlib group: 'log4j', name: 'log4j', version: '1.2.15', ext: 'jar'
}
ear {
    appDirName 'src/main/app'  // use application metadata found in this folder
    libDirName 'APP-INF/lib'  // put dependency libraries into APP-INF/lib inside the generated EAR;
                                // also modify the generated deployment descriptor accordingly
    deploymentDescriptor {  // custom entries for application.xml:
//      fileName = "application.xml"  // same as the default value
//      version = "6"  // same as the default value
        applicationName = "customear"
        initializeInOrder = true
        displayName = "Custom Ear"  // defaults to project.name
        description = "My customized EAR for the Gradle documentation"  // defaults to project.description
//      libraryDirectory = "APP-INF/lib"  // not needed, because setting libDirName above did this for us
//      module("my.jar", "java")  // wouldn't deploy since my.jar isn't a deploy dependency
//      webModule("my.war", "/")  // wouldn't deploy since my.war isn't a deploy dependency
        securityRole "admin"
        securityRole "superadmin"
        withXml { provider -> // add a custom node to the XML
            provider.asNode().appendNode("data-source", "my/data/source")
        }
    }
}  

You can also use custom options provided by the Ear task, such as from and metaInf.

Use a custom descriptor file

Suppose you already have .xml application and want to use it instead of configuring the ear.deploymentDescriptor snippet. G o and put the META-INF/application .xml the correct location in your source folder (see appDirName property). The contents of this existing file will be used, and the display configuration in ear.deploymentDescriptor will be ignored.