August 19, 2009

Bundle a Common Jar Into EJB Jar with Maven Plugin maven-ejb-plugin

Yesterday I had some problem with packing an EJB3 jar with Maven plugin maven-ejb-plugin. The problem seemed quite common. To bundle a common jar dependency into an EJB jar, but after reading the documentation for the maven-ejb-plugin at http://maven.apache.org/plugins/maven-ejb-plugin/, it seemed clear that the plugin lacked this capability. After several hour of failure I ran into a workaround . Instead of configure the maven-ejb-plugin I used the maven-dependency-plugin to copy the desired dependency jar into the target/classes directory and then configure maven-dependency-plugin to run before the package phase.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<generateClient>false</generateClient>
<ejbVersion>3.0</ejbVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>process-classes</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>se.msc.development</includeGroupIds>
<includeArtifactIds>common</includeArtifactIds>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

And if you need to add the common.jar to the classpath in the MANIFEST file add the following configuration to maven-ejb-plugin.
<archive>
<manifest>
<addclasspath>true</addclasspath>
</manifest>
<manifestfile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestfile>
</archive>

9 comments:

fedor_bel said...

Thanks alot, Pal!!!

That is exactly what I have been looking for!

I am a newbie in Maven and tryed to achieve the exactly same task! I just wanted to make that stupid maven include the damn jar into resulting jar as a dependency or something.

I spend entire day looking for a solution, I have read almost all the documentation for maven - so much bullshit! and nothing that answered such a simple and commonly asked question! I am sure the question is VERY common - because MAVEN is about dependencies! Well it excels in managing dependencies at build time. But what about run-time? Did they forget about it? That is exactly the issue we have here.

Well adding a working biuld-time dependency is as simple as adding a "dependency" in POM.

But how to achieve that same result for run-time? This is the next logical question for every normal person out there, trying to make his code to RUN after such an easy and successful build.

Thank you once again!

Anonymous said...

I've been googling for hours to find anything helpful on this topic. Not to mention the hours I've spent trying to solve it myself.

Thanks man!!!

Luc Bui said...
This comment has been removed by the author.
Magnus K Karlsson said...

A good reference article from SUN can be found here:

http://java.sun.com/j2ee/verified/packaging.html

Fritz said...

Thank you for the great help, but I have a small addtion.

The example to add the classpath to the manifest must be like this:

<archive>
<manifest>
<addClasspath>true</addClasspath>

</manifest>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>

Anonymous said...

You can do this by configuration of maven-ejb-plugin. You need to add configuration to this plugin:





group
art-id

lib

...

Anonymous said...

Hi everybody,

You don't need add common jar into an ejb jar, just add common jar in classpath of the manifest file, see example of the link http://maven.apache.org/plugins/maven-ejb-plugin/faq.html

Bye

Luigi Fugaro said...

Thank you sir!
I just copied you plugin tag and putted under my build/plugins tag... and all went well!

Thanks again!

maddy said...

I done it by just paste the jar file in apache maven dependency the it automatically show the dependency that actually what i want....any way thanks