How to exclude jars from ear - maven

I am bundling EAR from a WAR file. The JARS are placed both in WARFile/WEB-INF/lib folder as well as EARFile/lib folder with duplication.
Do we need the JARS again in EARFile/lib folder inspite of having in WARFile/WEB-INF/lib?
If we do not need in EAR/lib filder, How can we remove them? The EAR file size became double due to duplication JARs presence in both WAR and EAR.
Please help me.
Thanks

Good read here on "skinny" wars. Basically, you put the jars in the ear, but not the war files:
http://maven.apache.org/plugins/maven-war-plugin/examples/skinny-wars.html

You have to define the dependency to your war project in the following way:
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>xyz-web</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
and furthermore you have to configure the ear plugin like the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<version>6</version>
<modules>
<!-- Register our War as a web module, and set the context root -->
<webModule>
<groupId>${project.groupId}</groupId>
<artifactId>xyz-web</artifactId>
<contextRoot>/xyz</contextRoot>
</webModule>
</modules>
</configuration>
</plugin>

Related

Maven EAR plugin: Generated EAR file does not contain WAR or EJB or RAR modules

I've been using the EAR plugin to package a WAR, EJB & RAR module, however, while they all package successfully when i run mvn clean package, I notice the following gets output in the target directory.
application.xml (generated application.xml)
my-application-ear-1.0-SNAPSHOT (directory with correctly packaged WAR, EJB & RAR dependencies, META-INF with generated application.xml and additional resources from the EAR project)
my-application-ear-1.0-SNAPSHOT.ear (9kb .ear file that only has the META-INF with the generated application.xml & additional resources from the EAR project)
None of the modules are packaged in the final EAR file.
Here is the <build> node of the EAR project's POM.
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<finalName>my-application-ear</finalName>
<version>7</version>
<earSourceDirectory>src/main/application</earSourceDirectory>
<packagingIncludes>**/*</packagingIncludes>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<filtering>false</filtering>
<packagingIncludes>Default_HTTP_WC/**,Default_IIOP_WC/**,Default_JMS_WC/**,META-INF/**,</packagingIncludes>
<modules>
<ejbModule>
<groupId>com.abc.def</groupId>
<artifactId>ejb-module</artifactId>
<bundleDir>/</bundleDir>
<bundleFileName>ejbModule.jar</bundleFileName>
<moduleId>EjbModule_1</moduleId>
</ejbModule>
<rarModule>
<groupId>com.abc.def</groupId>
<artifactId>rar-module</artifactId>
<bundleDir>/</bundleDir>
<bundleFileName>RARModule</bundleFileName>
<uri>/RARModule.rar</uri>
<unpack>true</unpack>
<moduleId>RARModule_1</moduleId>
</rarModule>
<webModule>
<groupId>com.abc.def</groupId>
<artifactId>my-web-app</artifactId>
<uri>/WebApplication.war</uri>
<bundleDir>/</bundleDir>
<bundleFileName>WebApplication.war</bundleFileName>
<contextRoot>/foo</contextRoot>
<moduleId>WebModule_1</moduleId>
</webModule>
</modules>
</configuration>
<goals>
<goal>ear</goal>
</goals>
</plugin>
</plugins>
Am i missing something? Any assistance is appreciated.
I figured out my problem, I had completely overlooked the use of <packagingIncludes> from an earlier iteration. removed it and its fine.

Is it possible to have multiple context roots in Maven generated application.xml?

Maven is able to generate the application.xml file for an EAR and puts the context root in it that has been defined in the EAR's pom.xml.
Is it possible to have an application.xmlgenerated that has multiple context roots?
Or is the only possible way to achieve this to write the application.xml myself and use this rather than the generated one?
Sure, use applicationXml option in goal ear of Maven ear plugin. For more information on how to use it take a look at its usage page.
Regards.
I assume you mean different context roots for different WARs inside your ear (since each web module can have exactly one context-root). In that case, you can explicilty configure your war modules inside the ear's pom:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.9</version>
<configuration>
<modules>
<webModule>
<groupId>artifactGroupId</groupId>
<artifactId>module1</artifactId>
<contextRoot>/context-root-module1</contextRoot>
</webModule>
<webModule>
<groupId>artifactGroupId</groupId>
<artifactId>module2</artifactId>
<contextRoot>/context-root-module2</contextRoot>
</webModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>

maven EAR plugin configuration

i have problem with ear plugin configuration, exactly i want to place one of jar dependency in root directory in ear and rest of them in lib directory. Can someone help me with this?
Update:
I have 4 projects, one of them is a container, pom type. One of them is a ear with two dependencies, war and jar. I want to create ear with directory structure:
/ - a.war and b.jar projects
/lib/ - all dependencies for 'a' and 'b' projects
/META-INF/ - MANIFEST.MF
part of pom.xml
<configuration>
<!-- <artifactTypeMappings>
<artifactTypeMapping type="war" mapping="webModule" />
</artifactTypeMappings> -->
<webModule>
<groupId>a</groupId>
<artifactId>a</artifactId>
<contextRoot>/a</contextRoot>
<bundleFileName>a.war</bundleFileName>
<bundleDir>lib</bundleDir>
</webModule>
<jarModule>
<groupId>b</groupId>
<artifactId>b</artifactId>
<bundleDir>/</bundleDir>
<bundleFileName>/b.jar</bundleFileName>
</jarModule>
<defaultLibBundleDir>lib</defaultLibBundleDir>
</configuration>
A simple look into the documentation would have helped. You just remove the bundleDir entry from the entry you like to place into root location.
<jarModule>
<groupId>b</groupId>
<artifactId>b</artifactId>
<bundleFileName>/b.jar</bundleFileName>
</jarModule>

Maven : Remove duplicate jars from EAR

We are using maven to build an EAR that contains WAR, the JARS are duplicated on both EAR's root and WAR's lib folder, I read about the skinny jars to remove the jars from the WAR but I don't want this as I want to keep the WAR as stand alone unit,
IS there a way to do it the other way around and remove the JARs from the EAR's root folder ?
attached is the EAR POM
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.6</version>
<configuration>
<modules>
<webModule>
<groupId>com.csv.xyz</groupId>
<artifactId>web</artifactId>
</webModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.csv.xyz</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
<scope>compile</scope>
</dependency>
</dependencies>
The "maven-skinny-war" solution isn't any drawback in your case. Also have a look at this question and my answer: How to make maven place all jars common to wars inside the same EAR to EAR root?
With this solution you will first get WARs packaged with their own dependencies and second, an EAR module with skinny WARs. So everything is deployable on its own and nothing is duplicated.
I don't think this would be a good idea because of the class loader architecture. If a JAR is part of a WAR, it's defined by the web app class loader, which may result in multiple instances. If it's part of an EAR, it's defined by the enterprise app class loader, which is a different class loader.
To remove a dependency from the EAR lib folder, declare the dependencies as provided in the EAR pom, for example to remove commons-lang:
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
Just make sure if you have EJB modules that they don't require the dependency! Also webModules do not contribute to the EAR's root lib folder, do you have more dependencies in you ear pom?

creating war file using maven-ear-plugin and defining it in one pom.xml

I am at the starter level of the maven usage. I hope I can explain my problem clearly, I want to create an ear file which contains war file inside it. And I planned to use to create a war file from the start. Also I want to do it in my pom.xml at my project and there is only one pom.xml, here is the problem;
Can I create ear file and which contains this war that I created at the same time in one pom.xml file?
when I try to create war file in webmodule tag, here is the problem that I encounter " Artifact[war:denem.denem:denem] is not a dependency of the project." I understood so that's why I added dependency for this file in the same pom.xml but this time I encountered that problem
(By the way my command to build this pom is "mvn clean package" )
"1 required artifact is missing.
for artifact:
com.denem.denem:com.denem.de2:ear:v0.1"
It tries to find this war file but I want to create it not to find it. Here the code in my pom.xml file;
<parent>
<groupId>denem.denem</groupId>
<artifactId>com.denem.denem</artifactId>
<version>v0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>denem.denem</groupId>
<artifactId>com.denem.de2</artifactId>
<version>v0.1</version>
<packaging>ear</packaging>
<properties>
<cxf.version>2.2.5</cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>denem.denem</groupId>
<artifactId>denem</artifactId>
<version>v0.1</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.5</version>
<configuration>
<finalName>edu</finalName>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<modules>
<webModule>
<groupId>denem.denem</groupId>
<artifactId>denem</artifactId>
<contextRoot>/WebContent</contextRoot>
</webModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
I guess I am doing lots of things wrong. But If you can help me I will be glad. Thank you anyway.
You need to create a modular project.
Create:
a parent project of type "pom";
a child project of type "war";
if needed, child projects of type "ejb";
if needed, child projects of type "jar" (common libraries);
one project of type "ear", that has all of the above as dependencies.
In the latter you need to configure the ear plugin putting all the modules that you need.

Resources