3

I read this question on SO - How to add WAR inside EAR with Maven

In top voted answer by "joelittlejohn" -

"Now create a parent module (with pom) 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."

But why create a parent pom ? Why not just created a standard maven project with just a single pom. This single pom file contains the dependencies & modules, then generate the .ear file from this one pom file ?

Something like :

<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>

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

</plugin>
Community
  • 1
  • 1
blue-sky
  • 51,962
  • 152
  • 427
  • 752

1 Answers1

2

It's typical in maven to have a module for each war you want to build. And then the .ear file will contain multiple wars. So having a module each just provides a nice separation of duties. The parent pom just wraps it all together.

Michael
  • 6,141
  • 2
  • 20
  • 21
  • What does the parent pom wrap together that the child pom does not ? – blue-sky Mar 08 '12 at 19:53
  • Depends what features you use. It allows you to have a bunch of config for submodules that apply to all. For example: dependency management, resource filtering, plugins for build tasks, etc. It also allows you to release all the modules at once. The downside to one big pom with no submodules is that any of your submodules can't be used alone. So if you had a project with a jar that you wanted used elsewhere (say you defined an apis module for a webservice with some interfaces), you would want other projects to reference that module perhaps. – Michael Mar 08 '12 at 19:56
  • Thanks, in your opinion in above example is it redundant using a parent/child hierarchy just to create an ear file when all of the pom definitions that create the ear are contained in child pom ? – blue-sky Mar 08 '12 at 20:04
  • Not really, because you end up using the parent pom for a lot of common stuff. – Michael Mar 09 '12 at 01:58