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

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.

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.

In addition to the answer of khmarbaise I want to note that in order for your EJB Module to be able to access the libraries you have to configure it to include the dependencies inside the META-INF/MANIFEST.MF like this:
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
...
<configuration>
...
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
...
</plugin>

Related

Remove test dependencies from deployed POM

I have a fairly typical pom.xml which builds a jar:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mygroup</groupId>
<artifactId>my-lib</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>...</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I thought it'd be nice to remove the test dependencies (junit-jupiter and its dependencies) from the copy of the POM which is deployed with the jar, just to avoid imposing them on users of the jar. After all, test code isn't included in the deployed jar, so it shouldn't matter to users of the jar how the tests are written.
I figured this would be a common use case for maven-shade-plugin. But this use case doesn't seem to be mentioned in its documentation. And I wasn't able to make the shade plugin remove the junit-jupiter dependency from the reduced POM.
Is there a straightforward way to remove dependencies from the deployed POM? Am I worrying about nothing?
I saw this question, but it seems to be about removing test dependency contents from the uber jar. In my case, I'm not actually creating an uber jar. I'm just trying to use the shade plugin for its ability to rewrite the POM.
If you want to remove unnecessary parts from the deployed POM, you can use the flatten maven plugin:
https://www.mojohaus.org/flatten-maven-plugin/flatten-mojo.html
One of the features is to remove the test dependencies.

maven - not copying project jar outside /lib while EAR packaging

This is happening while packaging the EAR
My pom.xml is packaging the ear file where it should include projectA.jar file and projectB.war outside the /lib folder. Apparently, projectA.jar file is going inside the /lib folder which is not supposed to be. I have my application.xml which tells that these two projects should be outside the lib.
Question : how do i instruct maven not to bundle projectA.jar inside the /lib folder but to bundle it outside the /lib folder?.
My EAR structure should be:
MyWebEAR
\lib
\META-INF
projectA.jar
ProjectB.war
Below is my pom.
<dependencies>
<dependency>
<groupId>com.xxx.sms</groupId>
<artifactId>projectA</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.xxx</groupId>
<artifactId>projectB</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<earSourceDirectory>${basedir}</earSourceDirectory>
<earSourceIncludes>META-INF/*</earSourceIncludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<generateApplicationXml>false</generateApplicationXml>
<applicationXML>${basedir}/META-INF/application.xml</applicationXML>
</configuration>
</plugin>
</plugins>
<finalName>MyWebEAR</finalName>
</build>
Thanks for your time.
You need to specifically define a jarModule configuration in the modules section of your maven-ear-plugin configuration for the projectA dependency and explicitly set where you want the jar placed.
So your POM would be:
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<earSourceDirectory>${basedir}</earSourceDirectory>
<earSourceIncludes>META-INF/*</earSourceIncludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<generateApplicationXml>false</generateApplicationXml>
<applicationXML>${basedir}/META-INF/application.xml</applicationXML>
<modules>
<jarModule>
<groupId>com.xxx.sms</groupId>
<artifactId>projectA</artifactId>
<bundleDir>/</bundleDir>
</jarModule>
</modules>
</configuration>
</plugin>
The value (/) in the bundleDir tells the maven-ear-plugin to place projectA's jar in the root folder of the ear instead of the default location of lib.
You can see details on this in the plugins documentation here:
http://maven.apache.org/plugins/maven-ear-plugin/examples/customizing-module-location.html
I had the same problem in my project.
In my case, I was using an MDB so it was needed to be declared as <**ejbModule**> instead of jarModule. I also had to declare my dependency as of type ejb to be picked up Jboss (6.4):
<dependency>
<groupId>ab.cd</groupId>
<artifactId>mdb-publisher</artifactId>
<version>xxx-SNAPSHOT</version>
<type>**ejb**</type>
</dependency>

Maven - error while deploying ear to jboss 6.x - ClassFormatError

I have a maven project where i am package to EAR file and including all dependencies in /lib folder. But while deploying EAR file i am getting below 2 errors in jboss.
1)java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/jms/JMSException
For above error i learnt that i need to remove j2ee related jar files going inside the /lib folder.
2)java.lang.ClassCastException: com.xx.sms.ejb.ws.xxx.CoordinatorServiceBean cannot be cast to javax.servlet.Servlet
And this error i believe i should remove javax.servlet related jar files from /lib folder. Because this may be already provided by jboss servletContainer and you should exclude from your /lib folder.
I am new to maven world and somehow i managed to create a EAR.
Let me know how to exclude j2ee related and servlet related jar files during packing EAR.
Below is my pom.xml
<dependencies>
<dependency>
<groupId>com.xxx.sms</groupId>
<artifactId>CoordinatorBeans</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.xxx</groupId>
<artifactId>CoordinatorWeb</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<earSourceDirectory>${basedir}</earSourceDirectory>
<earSourceIncludes>META-INF/*</earSourceIncludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<generateApplicationXml>false</generateApplicationXml>
<applicationXML>${basedir}/META-INF/application.xml</applicationXML>
<modules>
<jarModule>
<groupId>com.xxx.sms</groupId>
<artifactId>CoordinatorBeans</artifactId>
<bundleDir>/</bundleDir>
<bundleFileName>CoordinatorBeans.jar</bundleFileName>
</jarModule>
<webModule>
<groupId>com.xxx</groupId>
<artifactId>CoordinatorWeb</artifactId>
<bundleDir>/</bundleDir>
<bundleFileName>CoordinatorWeb.war</bundleFileName>
</webModule>
</modules>
</configuration>
</plugin>
</plugins>
<finalName>CoordinatorApp</finalName>
</build>
After adding exclusions for below dependency it worked.
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>

Can't get EAR package structure correctly with Maven

I got a simple maven project and yet frustratingly fail for hours to get it right. The project contains 1 parent module, and 2 submodules (one for ear-packaging, the other for an ejb). Building works successfully, but the ear-packing just doesn't work as expected:
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>at.betrieb.projekt</groupId>
<artifactId>extended</artifactId>
<version>1.0</version>
</parent>
<artifactId>extended-ear</artifactId>
<packaging>ear</packaging>
<dependencies>
<dependency>
<groupId>at.betrieb.projekt</groupId>
<artifactId>extended-ejb</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.7</version>
<configuration>
<version>6</version>
<generateApplicationXml>false</generateApplicationXml>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<jarModule>
<groupId>at.betrieb.projekt</groupId>
<artifactId>extended-ejb</artifactId>
<bundleDir>/</bundleDir>
</jarModule>
</configuration>
</plugin>
</plugins>
</build>
</project>
Now when I look into the packaged folder I see this structure:
|-lib
extended-ejb-1.0.jar
activation-1.1.jar
javaee-api-7.0.jar
javax.mail-1.5.0.jar
|-META-INF
application.xml
What I expected however was this structure:
|-extended-ejb-1.0.jar
|-lib
activation-1.1.jar
javaee-api-7.0.jar
javax.mail-1.5.0.jar
|-META-INF
application.xml
So basically I wanted the ejb outside of the other libraries. These other 3 libraries come from the ejb-module which requires the javaee-api dependency for annotations. Unfortunately it also collects transitive the javax.mail.jar, activation.jar.
Now I really don't know why the structure just doesn't work as expected, by all means I tried to follow this guide step by step.
Ok, after I checked out a project from various maven archetypes I found the error... the minimal error... it's always a minimal error costing huge amounts of time...
In the dependencies section of the ear file, where I define my ejb as dependency, just add this:
<type>ejb</type>
so it's:
<dependency>
<groupId>at.betrieb.projekt</groupId>
<artifactId>extended-ejb</artifactId>
<version>1.0</version>
<type>ejb</type>
</dependency>
Afterwards it works correctly. Obviously it is wrong on the IBM page and on many other pages. Besides I found out the following section is also outdated, and can be completely removed from the ear-plugin section:
<jarModule>
<groupId>at.betrieb.projekt</groupId>
<artifactId>extended-ejb</artifactId>
<bundleDir>/</bundleDir>
</jarModule>

Custom jboss-app.xml using Maven

I declared a custom jboss-app.xml in my pom.xml, but the plugin is generating an internal and empty jboss-app.xml file into ear/META-INF.
I created my pom.xml based on this article with the following definition:
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<data-sources>
<data-source>${artifactId}/src/main/resources/mytest-ds.xml</data-source>
</data-sources>
<jboss>${artifactId}/src/main/resources/jboss-app.xml</jboss>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<modules>
<ejbModule>
<groupId>com.testproject</groupId>
<artifactId>ejb-project</artifactId>
</ejbModule>
</modules>
</configuration>
</plugin>
But, I can't handle my custom jboss-app.xml. A new is empty descriptor (without defined ejbs) is generated every time.
You should be able to simply include your jboss-app.xml in your source tree and Maven will pull it in.
Your project directory structure for the maven-ear-plugin should look like:
|-- pom.xml
`-- src
`-- main
`-- application
`-- META-INF
|-- application.xml
`-- jboss-app.xml
Then just run mvn package to create your EAR. Assuming the packaging for your pom.xml is 'ear' it will pull jboss-app.xml into your resulting ear.
Also, I believe your <datasources>...</datasources> configuration needs to be within the <jboss>...</jboss> tags.
See also: http://maven.40175.n5.nabble.com/Deployment-of-the-jboss-app-xml-file-td45009.html
From the docs, it looks like jboss tag triggers the generation of jboss-app.xml. How about omitting this tag and generating the ear?
Alternative would be to pull datasources via application.xml as described here. But as <jarModule> requires are full-blown maven artifact, that is longer way (perhaps for application servers other than JBoss):
• project/ds/pom.xml:
<artifactId>project-ds</artifactId>
<packaging>jar</packaging>
<name>My DataSource</name>
• project/ds/src/main/resources/my-ds.xml:
<datasources>
<local-tx-datasource>
...
• project/ear/pom.xml (assuming that driver is also packaged in EAR):
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>project-ds</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>project-war</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>${jtds.driver.version}</version>
</dependency>
</dependencies>
...
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<modules>
<jarModule>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<includeInApplicationXml>true</includeInApplicationXml>
</jarModule>
<jarModule>
<groupId>${project.groupId}</groupId>
<artifactId>project-ds</artifactId>
<unpack>true</unpack>
<uri>/</uri>
<bundleFileName>my-ds.xml</bundleFileName>
<includeInApplicationXml>true</includeInApplicationXml>
</jarModule>
<webModule>
<groupId>${project.groupId}</groupId>
<artifactId>project-war</artifactId>
<contextRoot>${context.path}</contextRoot>
</webModule>
</modules>
</configuration>
</plugin>
Will generate META-INF/application.xml:
<application>
<display-name>project-ear</display-name>
<description>...</description>
<module>
<java>jtds-1.2.4.jar</java>
</module>
<module>
<java>my-ds.xml</java>
</module>
<module>
<web>
<web-uri>project-war-1.0.0.war</web-uri>
<context-root>/</context-root>
</web>
</module>
</application>

Resources