Maven ear project with ejb and war modules - java

im working on a web application with maven and Jboss 7 wich conatins 3 modules ejb ear and war so the war will have the ejb as dependancy and the ejb will be in the same time a module of the ear so when i do this i get the same ejb twice this tree
ear
...Mywar
........Myejb
...Myejb
is this structure is correct or i should change another
the pom.xml for the war :
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tn.war.ep</groupId>
<artifactId>businessModule</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
.....
<dependency>
<groupId>tn.linckia.epgp</groupId>
<artifactId>ejbModule</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>ejb</type>
</dependency>
</dependencies>
</project>
the pom.xml for the ear :
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tn.war.ep</groupId>
<artifactId>earModule</artifactId>
<version>0.0.1</version>
<packaging>ear</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.6</version>
<configuration>
<modules>
<webModule>
<groupId>tn.war.ep</groupId>
<artifactId>businessModule</artifactId>
<bundleFileName>businessModule.war</bundleFileName>
<contextRoot>/businessModule</contextRoot>
</webModule>
<ejbModule>
<groupId>tn.war.ep</groupId>
<artifactId>ejbModule</artifactId>
<bundleFileName>ejbModule.jar</bundleFileName>
</ejbModule>
</modules>
<displayName>Security</displayName>
</configuration>
</plugin>
</plugins>
<finalName>AuthModule</finalName>
</build>
<dependencies>
<dependency>
<groupId>tn.war.ep</groupId>
<artifactId>businessModule</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>tn.war.ep</groupId>
<artifactId>ejbModule</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>ejb</type>
</dependency>
</dependencies>
</project>

Right way to configure EAR Module is to have the EJB jar dependency with "provided" scope in WAR module and having EJB jar directly under EAR like the below:
EAR
|
|-lib/someutil.jar
|-EJB.jar
|-my-web.war
| |-WEB_INF/lib
| |-coolutil.jar
|-EJB2.jar
But the my-web.war can dependent on the any EJB.jar, but its resolved in runtime by container. So, mark that ejb dependency as "provided" (by container) in WAR's pom.xml.

Option : #1
Yon don't even need a ear.
You can just put all your EJBs as jars inside the war.
Just add the EJB projects as dependencies in your War project.
Option : #2
If you still want EAR. All EJB projects output should be jars. And web project output should be war. And at last these EJB jars and web war would be placed in one EAR. This is a old fashion way, to keep it simple you could follow the method which I explained above in Option #1.

I had this issue and changing the ejb packaging tag from ejb to jar in the ejb modules pom.xml fixed the issue. No idea why!

Related

import own .jar file as dependency via Maven and have install discover and retrieve it's dependencies

So this is probably a stupid question, but I have created an own library "testlib" which I want to include in other of my own Maven projects.
This is my own librarys .pom
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.testlib</groupId>
<artifactId>testlib</artifactId>
<version>1.0-SNAPSHOT</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>testlibrary</description>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
In order to include my library in another new project, I have created a local repository in my new projects .pom
...
<repositories>
<repository>
<id>data-local</id>
<name>data</name>
<url>file://${project.basedir}/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.test.testlib</groupId>
<artifactId>testlib</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
...
Installing my own library "testlib" via mvn install is working fine for testlib itself. The problem is, that Maven will not detect that "testlib" requires org.slf4j, hence won't get it when I run install.
I have checked other dependencies in my .m2 folder and saw they have a .pom file with the same name as the .jar (for log4j that'd be log4j-1.2.17.pom). I tried copying testlibs .pom next to its .jar and changed the name accordinly, but that doesn't do it.
What do I have to do in order to get the same functionality as any other library from maven central? In other words, I don't want a fat .jar that has all dependencies included. I want a Maven project that adds my library as a dependency to discover that it needs sl4j and include it when mvn install is run.

EAR with WARs containing different versions of dependencies

Here is my requirement
EAR
WAR (Code)
----Dependency A v1
WAR (Code same as WAR above)
----Dependency A v2
AS the code for WAR project is same I don't want to create multiple code base for WAR project. So I'm looking for EAR pom.xml in such a way that maven builds the WAR with dependency A v1 which can be passed as some property while building WAR.
EAR pom.xml
...
(modules)
(webmodule)
(artifact)WAR(/artifact)
dependency version v1
(content-path)/warwithv1(/content-path)
(/webmodule)
(webmodule)
(artifact)WAR(/artifact)
dependency version v2
(content-path)/warwithv2(/content-path)
(/webmodule)
...
Thanks
Sorry for the XML Tags
EDIT: Answer changed after clarification in comments.
The question as I now understand it is: How does one use a single POM to create 2 WAR files that both have the same source code but different dependencies.
The solution I propose is to create a parent POM that will specify one module per WAR. One of the modules will contain the source code for the war and specify one version of dependencies. The second module will refer to the first module for its source code and will specify a second version of dependencies.
Here is my high level project structure:
Here is the top level (parent) POM:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>maven-war-diff-depend</groupId>
<artifactId>maven-war-diff-depend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
</project>
Module1 will specify v2.3 of log4j2, here is its POM:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>maven-war-diff-depend</groupId>
<artifactId>maven-war-diff-depend</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>module1</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
</project>
Module2 will specify v2.8.1 of log4j2 and point to module1 for its source code and web.xml file. Note that you may need to do additional work to refer to any other resources in module1 such as adding more maven plugins and configuration.
Here is the module2 POM:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>maven-war-diff-depend</groupId>
<artifactId>maven-war-diff-depend</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>module2</artifactId>
<packaging>war</packaging>
<build>
<sourceDirectory>../module1/src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<webXml>../module1/src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
</project>
Note that I only have a single Java class in module1 just to show that in fact this class is compiled and included in both module1 and module2:
Here are the results of a simple clean install build:
Once you have the two WAR files built it's fairly simple to add another module (this would be your EAR) which will include both of the WAR files - I believe that is your final goal.
So, create a third module:
Add the new module to your root level (parent) POM:
<module>module3</module>
Create the necessary configuration in your new module's POM. This involves adding dependencies on the other two artifacts you create with module1 and module2 and configuring the ear plugin as needed.
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>maven-war-diff-depend</groupId>
<artifactId>maven-war-diff-depend</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>module3</artifactId>
<packaging>ear</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10</version>
<configuration>
<applicationXml>${basedir}/target/application.xml</applicationXml>
<modules>
<webModule>
<groupId>maven-war-diff-depend</groupId>
<artifactId>module1</artifactId>
</webModule>
<webModule>
<groupId>maven-war-diff-depend</groupId>
<artifactId>module2</artifactId>
</webModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>maven-war-diff-depend</groupId>
<artifactId>module1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>maven-war-diff-depend</groupId>
<artifactId>module2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
</dependencies>
</project>
Now when you build it will create an EAR that includes the 2 WARs generated by module1 and module2. The ear in this example is simply called module3:
EDIT: Original answer re-added per request in comments.
Here is the POM from the original proposal - using two profiles to specify different dependency versions. As before please note that it's not a good idea to include servlet api inside of a WAR file, it's just being used here as a visual example to show how common dependencies are specified.
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>maven-war-diff-depend</groupId>
<artifactId>maven-war-diff-depend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>v2.3</id>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>v2.8.1</id>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>

Create Jar file using maven Plugin

I'm having some issues while making the JAR file which project type is pom and adding this jar in other project as dependency.
I'm using maven shaded plugins to make a JAR . It compiles well but it creates a jar file in project target directory but not in .m2 folder. Moreover, when i extract the file it doesn't have classes and resources etc
here are two projects i have
1) ProjectTestB
Project B is a independent project which have various modules like scheduler, service, web module which adds as dependency in other project
POM.XML
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TestProjectB</groupId>
<artifactId>ProjectTestB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>classworlds:classworlds</exclude>
<exclude>junit:junit</exclude>
<exclude>jmock:*</exclude>
<exclude>*:xml-apis</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
<exclude>log4j:log4j:jar:</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<modules>
<module>scheduler</module>
</modules>
</project>
2) ProjectA
Project A contains a dependency of ProjectB and some other services and business classes. Only Project A Contains Main class which will create executable JAR file
Pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>projectA</groupId>
<artifactId>projectA</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>TestProjectB</groupId>
<artifactId>ProjectTestB</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
So the questions are
1) How to make jar which have all the classes, resources etc if project package is POM (Project B)?
2) How can i add Project B jar file in Project A class path to access it modules?
As I see you try do something strange.
Your projectB has many modules, so your projectA should has dependency to those modules. Propably module of projecB has type jar, so you can ease depend on it without doing magic with projectB

Spring application context unable to find property files under maven resources folder

I am using maven in my project & I've put database.properties file under
maven resources folder.
I am accessing it in spring application context
<context:property-placeholder location="classpath:database.properties" />
But I don't understand why I am getting this file not found error on server start.
Could not load properties; nested exception is
java.io.FileNotFoundException: class path resource
[database.properties] cannot be opened because it does not exist
To my knowledge whatever resource is put under resources folder it is added to the classpath automatically by maven.
pom.xml
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myapp</groupId>
<artifactId>myapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>myapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>myapp</finalName>
</build>
</project>
NOTE: Seems like there is some problem with the M2E plugin of eclipse luna. Executed the same code in Juno & it worked fine.
That's not enough to build a deployable war file with maven. You need at least the war plugin. There's a thorough tutorial here:
http://crunchify.com/how-to-create-a-war-file-from-eclipse-using-maven-plugin-apache-maven-war-plugin-usage/
Your pom will look something like below, and you'll need to run mvn clean install against that.
Also I see you have a spring app, where are the spring dependencies in the pom file?
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CrunchifyTutorial</groupId>
<artifactId>CrunchifyTutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</project>

maven 3 interproject depedency with war packaging

I have Eclipse Indigo and M2E plugin installed.
So essentially I have a standard maven web project (let's call it proj-service) that is built into a war file in the package phase. This all works fine. My issue comes in when I have my other project (lets call it proj1) that needs to use classes from proj-service. I know that this is possible in maven+eclipse but it does not seem to be working at the moment. I have the following in proj1's pom right now:
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.foo</groupId>
<artifactId>proj1</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>proj1</name>
<properties>
<spring.version>3.1.0.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Maven Repo Libraries -->
.........
<!-- Interproject dependencies -->
<dependency>
<groupId>com.mycompany.foo</groupId>
<artifactId>proj-service</artifactId>
<version>1.0</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<finalName>lsoap</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Unfortunately with Maven's war packaging you can't reuse classes from war project, because there is no direct build artifact you can use for the class path.
So, in order to do share classes properly you need to extract those common classes into a 3rd common project (jar packaging) and make it as dependency in both of your other projects.
First you have to change the configuration of your proj-service project in the way to change the configuration of the maven-war-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<attachClasses>true</attachClasses>
<archiveClasses>true</archiveClasses>
...
</configuration>
</plugin>
This will it make possible to use the classes from the proj-service project in other projects via the following dependencies:
<dependency>
<groupId>myGroup</groupId>
<artifactId>myArtifact</artifactId>
<version>myVersion</myVersion>
<classifier>classes</classifier>
</dependency>
This will result in changing your dependency from:
<dependency>
<groupId>com.mycompany.foo</groupId>
<artifactId>proj-service</artifactId>
<version>1.0</version>
<type>war</type>
</dependency>
into:
<dependency>
<groupId>com.mycompany.foo</groupId>
<artifactId>proj-service</artifactId>
<version>1.0</version>
<classifier>classes</classifier/>
</dependency>

Categories

Resources