Sunday, June 26, 2011

Getting Started with Maven Enterprise Applications

There seems to be a little bit of a funny in the Enterpise application that NetBeans generates which can completely befuddle you if you aren't aware of it:

SEVERE: Expected to find an expanded directory for submodule MyEnterpriseApp-web-1.0-SNAPSHOT.war but found a JAR. If this is a directory deployment be sure to expand all submodules.

Seen that error before? Well stick with me and I'll show you how to get rid of it.

Lets create a new project
Projects -> Maven -> Enterprise Application



Create a Session Bean in your EJB project to make the JAR file valid.

package com.acme;

import javax.ejb.Stateless;
import javax.ejb.LocalBean;

/**
*
* @author Tim
*/
@Stateless
@LocalBean
public class SimpleSessionBean {

public String hello(String name) {
return "Hello " + name;
}

}


So far this is all pretty standard stuff, and if you try and run your application now you'll be met with the above mentioned error. However resolving the said error is actually quite simple.

Open the EAR project and edit the POM.xml - the build section before you edit it should look like this.

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<version>6</version>
<defaultLibBundleDir>lib</defaultLibBundleDir>
</configuration>
</plugin>
</plugins>
<finalName>MyEnterpriseApp-ear</finalName>
</build>


After you edit it, it should look as follows.

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<version>6</version>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<modules>
<webModule>
<groupId>com.acme</groupId>
<artifactId>MyEnterpriseApp-web</artifactId>
<unpack>true</unpack>
<!--<contextRoot></contextRoot>-->
</webModule>
<ejbModule>
<groupId>com.acme</groupId>
<artifactId>MyEnterpriseApp-ejb</artifactId>
<unpack>true</unpack>
</ejbModule>
</modules>
</configuration>
</plugin>
</plugins>
<finalName>MyEnterpriseApp-ear</finalName>
</build>


Whats happened here is that GlassFish is expecting the application to be in the exploded format, but it isn't.
By added the <webModule> and <ejbModule> and specifically the <unpack>true</unpack> line we are telling the plugin to explode the modules.

Hope this stops somebody else from wasting a good part of their evening.