0

I want to package a message-driven bean using Maven. The bean has various dependencies (external libraries) that should be packaged with it. In the end, the resulting package should be deployed on an application server (such as Wildfly).

I tried to create multi-module Maven project where I have a module with "ear" packaging that depends on the actual message-driven bean module which uses "jar" packaging (I also tried "ejb" here). However, when the message-driven bean is called it is not able to acccess its dependencies (no NoClassDefFoundError).

The following change to my "ear" pom fixed this issue because the dependencies are now accessible to the message-driven bean.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.9.1</version>
            <configuration>
                <defaultLibBundleDir>lib</defaultLibBundleDir>
            </configuration>
        </plugin>
    </plugins>
</build>

While it basically works now, I feel like I am doing something wrong. Do I really need to change my pom like this? If I have to place the dependencies in the lib directory, why is Maven not doing this by default when building an EAR file.

Renke Grunwald
  • 855
  • 1
  • 9
  • 15
  • http://www.developerscrappad.com/1272/java/java-ee/maven/building-and-deploying-java-ee-ear-with-maven-to-java-ee-application-server-part-2-where-to-put-your-source-codes-and-pom-xml-ejb-mdb-web-enterprise-application-client/ – Konstantin V. Salikhov Oct 17 '14 at 08:19

1 Answers1

0

the EAR/lib folder is the default folder for libraries and every *.jar file inside this folder is automatically loaded by the ear classloader (Java EE 7 Specification)

i think that the EAR/lib folder is come in later specifications of java ee and the maven ear plugin is not updated to this "java ee defaults".

with "defaultLibBundleDir" in maven-ear-plugin all the transitive dependencies are put inside this declared folder. the second is that inside the application.xml folder maven is configuring the "library-directory" element. and this is not required because the "lib" folder is default loaded with the ear classloader.

and this can solved by the setting the maven ear configuration libraryDirectoryMode to NONE

i think it is also best practice to use the fileNameMapping to no-version configuration. this removes the maven versions out of the file names. the maven version is also inside the jar/META-INF/MANIFEST.MF this is for the jndi name of the modules because the module name came from the jar/war name without the file ending.

your configuration whould look like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.9.1</version>
            <configuration>
                <defaultLibBundleDir>lib</defaultLibBundleDir>
                <libraryDirectoryMode>NONE</libraryDirectoryMode>
                <fileNameMapping>no-version</fileNameMapping>
            </configuration>
        </plugin>
    </plugins>
</build>
StefanHeimberg
  • 1,455
  • 13
  • 22
  • Thanks for your helpful answer. It's not exactly easy to find out why a plugin behaves in a particular way. The plugin not being up to date with the latest Java EE standards makes sense (for whatever reasons, anyway). – Renke Grunwald Jan 22 '15 at 13:30