How to make the Maven EAR Plugin automatically manage the classpath for dependencies?

James picture James · Aug 16, 2011 · Viewed 8.1k times · Source

I started using the maven ear plugin about 12 months ago and want to find out if there are any alternatives. One of the benefits of Maven is the dependency management however you seem to almost completely lost this with the ear plugin. It builds all the dependant jar's into the ear but won't actually put any of them on the classpath with out adding the configuration below:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-ear-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <version>6</version>
        <modules>
            <ejbModule>
                <groupId>com.mycompany.app</groupId>
                <artifactId>MyApplication-ejb</artifactId>                          
            </ejbModule>

            <jarModule>
                <groupId>axis</groupId>
                <artifactId>axis</artifactId>
                <bundleDir>lib</bundleDir>
            </jarModule>
            <jarModule>
                <groupId>commons-discovery</groupId>
                <artifactId>commons-discovery</artifactId>
                <bundleDir>lib</bundleDir>
            </jarModule>
            <jarModule>
                <groupId>axis</groupId>
                <artifactId>axis-wsdl4j</artifactId>
                <bundleDir>lib</bundleDir>
            </jarModule>
        </modules>
    </configuration>
</plugin>

Am I missing something does a more recent version of the plugin eliminate the need for this, is there an alternative that manages this for you? I can't believe each time I add a dependency to a module I need to add it to the ear pom configuration. The most frustrating thing is even if I remember to add a dependant library to the above configuration, if that is in turn dependent on something else (as was the case with axis) I am only finding out when I deploy the ear.

Answer

khmarbaise picture khmarbaise · Aug 16, 2011

First you should have a separate module for the ear (and of course ear ) which looks like the following:

root
  +-- client
  !     +--- pom.xml
  +-- service
  !     +--- pom.xml
  +-- ear
        +--- pom.xml

Second you should update the version of the ear plugin, cause the current version is 2.6. Furthermore define your parts as dependencies

 <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-ear-plugin</artifactId>
        <version>2.5</version>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>webgui</artifactId>
      <version>${project.version}</version>
      <type>war</type>
    </dependency>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>service</artifactId>
      <version>${project.version}</version>
      <type>ejb</type>
    </dependency>
  </dependencies>

The configuration you are using is intended for supplemental 3rd party libs which should be packaged.