23

I have EAR with an application and I need to extend this app with my own code that is packaged as a WAR. Is there a maven plugin that can help me with putting the WAR inside the EAR?

The manual procedure is to put WAR inside EAR and add module to application.xml. I would like to automate that.

EDIT: small clarification - the WAR project is using maven but for EAR I have only the binary file nothing more.

Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
stalker
  • 1,300
  • 1
  • 13
  • 16

2 Answers2

48

I'd create a new module that has <packaging>ear</packaging>.

In the dependencies for this ear module, include your war module:

<dependency>
    <groupId>com.your.group.id</groupId>
    <artifactId>your-war-artifact</artifactId>
    <version>your-war-version</version>
    <type>war</type>
</dependency>

Now in the build plugins for this ear module, include the maven-ear-plugin like, e.g.:

<plugin>
    <artifactId>maven-ear-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <finalName>MyEarFile</finalName>
        <version>5</version>
        <generatedDescriptorLocation>${basedir}/src/main/application/META-INF</generatedDescriptorLocation>
        <modules>
            <webModule>
                <groupId>com.your.group.id</groupId>
                <artifactId>your-war-artifact</artifactId>
                <uri>YouWarFile.war</uri>
                <bundleFileName>YouWarFile.war</bundleFileName>
                <contextRoot>/appname</contextRoot>
            </webModule>
        </modules>
    </configuration>
</plugin>

You can change the specific configuration values for the webModule as required.

Now create a parent module (with <packaging>pom</packaging>) and add the war module and the ear module to it. Make sure you set the <parent> of the war and ear modules corrently.

When you run mvn package for this new parent, a war file will be built by the war module and an ear file (containing the war) will be built by the ear module.

joelittlejohn
  • 11,665
  • 2
  • 41
  • 54
  • Do I have to add WAR as dependency in EAR pom.xml? How it will know the location of my WAR which is in web-module? – Tushar Oct 25 '12 at 13:31
  • Have you tried it? Yes you need the dependency in the ear module, as described above. The maven-ear-plugin doesn't need the exact location/path to a war file, because it refers to it as a maven artifact (using the groupId and artifactId specified in the webModule element). – joelittlejohn Oct 26 '12 at 11:05
  • @joelittlejohn Why do I need to declare the the artifact `your-war-artifact` two times? Once in the dependency section and again in the `webModule` section? Could you please explain? – Geek Jul 11 '16 at 13:02
  • @Geek The war file is a *dependency* that will be built if it doesn't already exist, maven will use the war pom file configuration to build the war artifact. The webModule reference is so that the war artifact is included inside the ear file. – egallardo Oct 30 '17 at 18:47
0

Add WAR as dependency and use maven ear plugin. Then use ear:generate-application-xml goal and finally ear:ear goal.

Koziołek
  • 2,791
  • 1
  • 28
  • 48
  • Maybe I am missing something but I don't see how to use maven ear plugin. The thing is I don't have the source for that EAR. – stalker Mar 02 '11 at 12:13
  • 1
    if you have EAR file you can unpack it: http://maven.apache.org/plugins/maven-ear-plugin/examples/unpacking-a-module.html and then add war, change application.xml and pack it again. – Koziołek Mar 02 '11 at 12:24