How to create an EAR file for an existing Maven Project
I have developed a web application that up till now I was testing locally using Tomcat as the server.
I now wish to move to Oracle WebLogic Server 11gR1 (10.3.5) for further use. I looked up and found out that there are .ear files such as .war file that are supported. Since I am using Eclipse, I have downloaded and installed the Weblogic Plugin in it and successfully added the server too.
However, if I wish to deploy a .ear file on WebLogic what are the steps that I need to follow? Is there a specific file structure that my existing Maven Project needs to have?
I am not successfully able to export the .ear file from eclipse. I understand that once the .ear file is ready, I can move the same to my WebLogic C:\*BaseDirectry*\*BaseName*\applications folders and run it from there.
But how do I successfully create a .ear file of my existing maven project?
The structure I would suggest in these cases is to have a maven multimodule project providing your application module (jar packaging), a war module (war packaging) and an ear module (ear packaging) in order to have a clear separation of concerns and have maven build a single artifact per module.
If your project is currently a classic maven project, you can create an aggregator module project (pom packaging) as following:
<?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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>your.group.id</groupId> <artifactId>your.artifact.id-aggregator</artifactId> <packaging>pom</packaging> <version>your.version</version> <modules> <module>your.artifact.id-application</module> <module>your.artifact.id-war</module> <module>your.artifact.id-ear</module> </modules> </project>
This means the aggregator project will point to maven modules existing in subfolders your.artifact.id-application, your.artifact.id-war and your.artifact.id-ear. The application module is a simple Maven project providing your application logic and building a jar file out of it. It is not mandatory, but it can also point as a parent to the aggregator project (aggregation and inheritance are two different things in Maven, which can be mixed together or used differently). You can declare the parent project in modules as following:
<parent> <groupId>your.group.id</groupId> <artifactId>your.artifact.id-aggregator</artifactId> <version>your.version</version> </parent>
In the application module, you don't need to specify packaging, since by default it will jar, while in the ear and war module, packaging will be respectively ear and war.
The war module will need to have the application module as its dependency. The WAR module is also a good location to place integration tests. You could also configure here a jetty maven plugin start/stop as part of pre/post-integration-test phase. The war module will also provide the required src\main\webapp\WEB-INF structure
The ear module will need to have the war module as its dependency and it is important to specify its type, war, as following:
<dependencies> <dependency> <groupId>your.group.id</groupId> <artifactId>your.application.id-war</artifactId> <version>your.version</version> <type>war</type> </dependency> </dependencies>
Note the war. The ear module will also provide the required src\main\application\META-INF structure. The ear module can configure the maven-ear-plugin simply as following:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-ear-plugin</artifactId> <configuration> <modules> <webModule> <groupId>your.group.id</groupId> <artifactId>your.artifact.id-war</artifactId> <bundleFileName>your.artifact.id.war</bundleFileName> <contextRoot>/your.artifact.id</contextRoot> </webModule> </modules> </configuration> </plugin> </plugins> </build>
its application.xml file (under src\main\application\META-INF) can simply look like below:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE application PUBLIC "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN" "http://java.sun.com/dtd/application_1_3.dtd"> <application> <display-name>your.group.id.your.artifact.id_ear</display-name> <module> <web> <web-uri>your.artifact.id.war</web-uri> <context-root>/your.artifact.id</context-root> </web> </module> </application>
It might look over-complicated as a structure, but it gives you control over modules and artefacts, meets maven best practices and improves your focus (i.e. application code only on the application module, integration tests only on the war module).