Maven ear Plugin

From NovaOrdis Knowledge Base
Revision as of 17:24, 7 April 2017 by Ovidiu (talk | contribs) (→‎JEE Deployment Descriptor Generation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

External

Internal

TODO

Deplete https://home.feodorov.com:9443/wiki/Wiki.jsp?page=MavenTemplateEnterprise

Overview

Each Maven module creates just one artifact, and as EARs are aggregations of multiple artifacts, a project that produces an EAR has at least two modules: the module that produces the EAR and the module that produces the actual content (WAR, JAR, etc.).

Examples

WAR Dependencies

A WAR dependency must be explicitly declared of type "war" in the <dependencies> section, otherwise the dependency management will attempt to find a JAR and fail. The corresponding JEE module declaration is <webModule>.

EJB Dependencies

An EJB dependency must be explicitly declared of type "ejb" in the <dependencies> section, otherwise the dependency management will attempt to find a JAR and fail. The corresponding JEE module declaration is <ejbModule>.

JEE Deployment Descriptor Generation

The EAR plugin automatically generates an application.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE application PUBLIC
	"-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN"
	"http://java.sun.com/dtd/application_1_3.dtd">
<application>
  <display-name>ear-example</display-name>
  <module>
    <web>
      <web-uri>ear-embedded-web-application-1.war</web-uri>
      <context-root>/example</context-root>
    </web>
  </module>
  ...
</application>

If <jboss> is specified in the plugin's configuration, Maven will also generate a JBoss-specific deployment descriptor. Versions seem to be supported by default up to 5.

If we want to use a custom application.xml that is specified in the source tree, the plugin must be explicitly configured to include it, as shown below. If not configured as such, the plugin will generate application.xml even if a file src/main/resources/META-INF/application.xml exists.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-ear-plugin</artifactId>
    <version>2.10.1</version>
    <configuration>
        <applicationXml>${basedir}/src/main/resources/META-INF/application.xml</applicationXml>
        ...
    </configuration>
</plugin>

Including Libraries within an EAR

Java libraries can be included within an EAR as java modules or as JARs within the EAR's /lib directory.

Java Modules

Specify <includeInApplicationXml>true</includeInApplicationXml>. This will make Maven to generate <java> <module> configuration in application.xml:

<application>
  ...
  <module>
    <java>ear-embedded-lib-1.jar</java>
  </module>
  ...
</application>

Note that this type of deployment is subject to JEE EAR module visibility rules. For a discussion on this subject, for WildFly, see EAR Deployments and WildFly.

This is how the pom.xml should be configured:

<plugin>
  <artifactId>maven-ear-plugin</artifactId>
    <configuration>
      <modules>
        <jarModule>
          <groupId>io.novaordis.playground.jee.ear.war-and-lib</groupId>
          <artifactId>ear-embedded-lib</artifactId>
          <!--
                 If the following flag is not specified, the JAR will be embedded in the EAR's root
                 but no corresponding <java> <module> will be added to application.xml module list.
           -->
          <includeInApplicationXml>true</includeInApplicationXml>
        </jarModule>
        ...
      </modules>
   </configuration>
</plugin>

JAR Files in the /lib Directory

The EAR plugin can be configured to place <jarModule> artifacts in a specific EAR directory - usually /lib and not declare them as Java module in application.xml:

<plugin>
  <artifactId>maven-ear-plugin</artifactId>
    <configuration>
      <!--
             We have a choice of installing third-party libraries as JEE JAR modules,
             or as simple (non-module) libraries, in /lib.
      -->
      <defaultJavaBundleDir>lib/</defaultJavaBundleDir>
      <modules>
        <jarModule>
          <groupId>io.novaordis.playground</groupId>
          <artifactId>someLib</artifactId>
        </jarModule>
      </modules>
    </configuration>
</plugin>

Custom EE Module Filename

By default, Maven uses the implicit deployment artifact name, which includes the version information for all JEE modes (EJB, WARs, etc.) The name can be customized using <bundleFileName> as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-ear-plugin</artifactId>
    <version>2.10.1</version>
    <configuration>
        ... 
        <modules>
            <ejbModule>
                <groupId>artifactGroupId</groupId>
                 <artifactId>artifactId</artifactId>
                 <bundleFileName>some-another-name.jar</bundleFileName>
             </ejbModule>
             ...
        </modules>
    </configuration>
</plugin>