Site icon Antonio's Blog

Because I always forget how to use maven-ear-plugin

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 :

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 :

[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 :

Next time I’ll need the ear plugin I’ll remember to read my own post.

Exit mobile version