Sometimes you write a blog to express an idea, to ask some feedback from the community, to share a very nice trick… or because you always forget something and want to write it down so you can remember later on. That’s the reason of this post. I never remember how to use the maven-ear-plugin, so I’m writing it down. There is something funny with Maven and I. I spend hours trying to figure out to do something with Maven, and then, I automatically forget it. That’s the beauty of human’s brain : it doesn’t hold junk information for long.
Let’s say you have a project made of a Web module, an EJB module and external Lucene libraries. You then need to package this entire application into a ear file plus a few configuration files (login-config.xml and the application.xml deployment descriptor). With Maven each module is defined into a separate directory with its own pom.xml. So you will end up with the following structure :
- a root pom.xml
- a web module with its own pom.xml
- an EJB module with its own pom.xml
- an ear module that will be used only to package the entire application
I will not define what’s in the pom.xml of the web and EJB module, but only the interesting part : the pom.xml of the ear module, that’s where you define the maven-ear-plugin :
- The first part of the pom.xml is quite normal, nothing special except the packaging that is set to ear (<packaging>ear<packaging>).
- Then you have the dependencies. Here you define the web and EJB modules as well as the Lucene libraries. Note that it’s very important to specify <type>war</type> for the web module. If you don’t Maven will look for a jar file.
- Then comes the definition and configuration of the maven-ear-plugin. Notice that I redefine the artifacts that depend on the application but I add the extra information of the module : <webModule>, <ejbModule> or <jarModule>. Also note that I’ve decided to package the Lucene libraries into a subdirectory called lucenelib.
[sourcecode language=”xml”]
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example.mvnearplg</groupId>
<artifactId>ear</artifactId>
<packaging>ear</packaging>
<version>1.0</version>
<parent>
<groupId>org.example</groupId>
<artifactId>mvnearplg</artifactId>
<version>1.0</version>
</parent>
<dependencies>
<!– web and ejb modules –>
<dependency>
<groupId>org.example.mvnearplg</groupId>
<artifactId>ejb</artifactId>
<version>1.0</version>
<type>ejb</type>
</dependency>
<dependency>
<groupId>org.example.mvnearplg</groupId>
<artifactId>web</artifactId>
<version>1.0</version>
<type>war</type>
</dependency>
<!– external lucene libraries –>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>2.4.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.3.2</version>
<!– configuring the ear plugin –>
<configuration>
<modules>
<webModule>
<groupId>org.example.mvnearplg</groupId>
<artifactId>web</artifactId>
</webModule>
<ejbModule>
<groupId>org.example.mvnearplg</groupId>
<artifactId>ejb</artifactId>
</ejbModule>
<!–extra lucene libs into lucenelib directory –>
<jarModule>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<bundleDir>lucenlib</bundleDir>
</jarModule>
<jarModule>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<bundleDir>lucenelib</bundleDir>
</jarModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
</project>
[/sourcecode]
The ear plugin looks for its resources under the src/main/application directory. That’s where you typically put your META-INF directory with the application.xml file and some extra descriptors. So the result is that :
- The external Lucene libraries are under the lucenelib directory (that’s because I’ve specified the <bundleDir>, otherwise they would have been under the root directory)
- The META-INF/application.xml and the /login-config.xml files are automatically packaged in the ear file (that’s because they were under the default resource directory src/main/application).
- The ejb module (ejb-1.0.jar) and web module (web-1.0.war) are located under the root directory
Next time I’ll need the ear plugin I’ll remember to read my own post.
