Setting up a Java EE project with IntelliJ is a trivial task. Getting a multi-module setup to work properly with quick deployments and maven builds is not at all a trivial task.

My requirements:

  • A multi-module project with at least a web module, an ejb module and a common module with shared code to be used by ejb/web
  • Maven building ear file.
  • Easy deployment to local app server (Glassfish and Websphere Application Server)
  • Quick compiles and hot swapping of recompiled code into running app.
  • EJB 3.0 (not 3.1)

Download attached  example project and have a look for what it’s worth.

This is how I created my project using IntelliJ IDEA 10 Ultimate edition:

1. Create modules:

  • Create EJB module named ‘ejb’ and add EJB facet. Don’t “Fix” javaee.jar yet!
  • Create Web module named ‘web’ with Web facet. Don’t create any artifact yet. Move and rename web folder within web module! Give it name ‘webapp’ and place it under src/main.
  • Create EAR module named ‘ear’ with JavaEEApplication facet. Don’t create any artifact yet.
  • Create common module named ‘domain’ (or whatever it’s going to contain)

2. Edit root pom.xml and module pom files.

Root pom.xml should look something like:

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>dag</groupId>
    <artifactId>mavenear</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
    <modules>
        <module>domain</module>
        <module>ejb</module>
        <module>web</module>
        <module>ear</module>
    </modules>

    <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>javaee</groupId>
            <artifactId>javaee-api</artifactId>
            <version>5</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

Domain module pom.xml should look like:

<?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/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mavenear</artifactId>
        <groupId>dag</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>dag.mavenear2</groupId>
    <artifactId>domain</artifactId>
    <packaging>jar</packaging>

</project>

EJB pom.xml:

<?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/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mavenear</artifactId>
        <groupId>dag</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>dag.mavenear2</groupId>
    <artifactId>ejb</artifactId>
    <packaging>ejb</packaging>

    <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>dag.mavenear2</groupId>
            <artifactId>domain</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <ejbVersion>3.0</ejbVersion>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <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>
        </plugins>
    </build>
</project>

Web pom.xml:

<?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/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mavenear2</artifactId>
        <groupId>dag</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>dag.mavenear2</groupId>
    <artifactId>web</artifactId>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>dag.mavenear</groupId>
            <artifactId>domain</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
        </dependency>
        
        <dependency>
            <groupId>dag.mavenear2</groupId>
            <artifactId>ejb</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>web</finalName>
    </build>
</project>

EAR pom.xml:

<?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/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mavenear</artifactId>
        <groupId>dag</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>dag.mavenear2</groupId>
    <artifactId>ear</artifactId>
    <packaging>ear</packaging>
    <dependencies>
        <dependency>
            <groupId>dag.mavenear</groupId>
            <artifactId>ejb</artifactId>
            <version>1.0</version>
            <type>ejb</type>
        </dependency>
        <dependency>
            <groupId>dag.mavenear</groupId>
            <artifactId>web</artifactId>
            <version>1.0</version>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>dag.mavenear</groupId>
            <artifactId>domain</artifactId>
            <version>1.0</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <modules>
                        <webModule>
                            <groupId>dag.mavenear</groupId>
                            <artifactId>web</artifactId>
                        </webModule>
                        <ejbModule>
                            <groupId>dag.mavenear</groupId>
                            <artifactId>ejb</artifactId>
                        </ejbModule>
                        <jarModule>
                            <groupId>dag.mavenear</groupId>
                            <artifactId>domain</artifactId>
                        </jarModule>
                    </modules>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3. Now write some code:

Common module: Create a class as follows:

public class Common {
    public String sayHey() {
        return "Common says hey!";
    }
}

For EJB module: Let IntelliJ create the EJB stub by adding a stateless session bean to main src. Name it ‘Dag’ and add the following method to the bean:

@Stateless(name = "DagEJB")
public class DagBean implements Dag {
    public DagBean() {
    }

    @Override
    public String sayHey() {
        Common common = new Common();
        return "EJB says hey. Via EJB: " + common.sayHey();
    }

}

Extract an interface named ‘Dag’ with the single method sayHey().

For Web module:

Let IntelliJ IDEA create a servlet stub and name it ‘DagServlet’. IntelliJ edits web.xml for you but you need to add a servlet mapping. This can be done using a IntelliJ feature or add the following to web.xml:

<servlet-mapping>
    <servlet-name>DagServlet</servlet-name>
    <url-pattern>/servlet/*</url-pattern>
</servlet-mapping>

Add the following code to the servlet:

@EJB(name = "DagEJB")
private Dag ejb;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ServletOutputStream out = response.getOutputStream();
    out.println("<html><body>");
    out.println("<p>Servlet says hey.</p>");
    Common common = new Common();
    out.println("<p>" + common.sayHey() + "</p>");
    out.println("<p>" + ejb.sayHey() + "</p>");
    out.println("</body></html>");
    out.flush();
}

4. Build with maven

Now it should be able to build an ear file using Maven, assuming Maven is setup correctly, Run the 'package' goal in the root pom.xml and check the created ear-1.0.ear in folder .../ear/target
Deploy the ear file on Glassfish and go to http://localhost:8080/web/servlet

5. Deploy from IntelliJ

Now that we know the app works as wanted it’s time to setup deployment from within IntelliJ IDEA.

Chances are good that IntelliJ at this point has created a number of artifacts for you. Setup a Glassfish configuration and select artifact ‘ear:ear exploded’ for deployment.

Edit application.xml in ear module and set web context root to /web:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd"
             version="5">

    <module>
        <ejb>ejb-1.0.jar</ejb>
    </module>
    <module>
        <web>
            <web-uri>web.war</web-uri>
            <context-root>/web</context-root>
        </web>
    </module>
</application>

Perhaps you will need to remove artifact output folder …/ear/target/ear-1.0 if maven has built to this folder before.

Start the Glassfish server in debug mode and go to http://localhost:8080/web/servlet You will most likely end up with a ClassNotFoundException. The problem is missing MANIFEST.MF files in the web and ejb module. These can be added from the artifact view in IntelliJ:

1. Navigate to artifact “ear:ear exploded” and select ejb-1.0.jar. There should be a “Create Manifest…”-button below. Click it and select the ejb module root.

2. Insert “domain-1.0.jar” to the Class-Path field.

3. Repeat for the web:war artifact. The META-INF should be located in the webapp folder next to WEB-INF.

Potholes along the road:

  • At some point I had a web.xml without servlet API version (2.5). This lead to Glassfish not being able to detect my annotated EJB:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
...
</web-app>

Resources:

Maven EAR plugin: http://agoncal.wordpress.com/2009/10/23/because-i-always-forget-how-to-use-maven-ear-plugin/

Classloader and EAR structure: http://www.technicalfacilitation.com/get.php?link=classloading

Advertisements

I’m on a new project and have the opportunity to make things right from the beginning. I’ve been trying to include version number and build number into the packaged WAR file. As a bonus I’ve made the version available through a web page like:

Version: MyApp-1.0 (build number 7)

My project is a web service built with maven. TeamCity (5.1.1) is used for building. I use IntelliJ IDEA version 9 as my IDE. With version number I refer to the version in my pom.xml, for instance:

    <version>1.0</version>

Build number refers to the automatically incremented build number in TeamCity.

These are the steps I took:

1. In the TeamCity Build configuration, make sure Build number format includes {0}.

2. Create/edit your project file src/main/resources/application.properties:

    version=${pom.name}-${pom.version} (build number ${build.number})

pom.name and pom.version are Maven attributes defined in the beginning of your pom.xml. build.number is an environment variable set by TeamCity at build time.

3. Edit pom.xml:

<properties>
<!--
When building on TeamCity this property is set as an environment variable by TeamCity and then maven replaces the placeholder in application.properties.
When compiling locally we have to set this property here or Spring will fail to resolve the placeholder in application.properties. This works in IntelliJ (only?) with its feature to filter maven resources.
A cleaner solution is to set up an envrionment variable in the IDE with this name, but this requires new developers to configure the IDE.
-->
  <build.number>Unknown/local build</build.number>
</properties>
...
<resources>
  <resource>
    src/main/resources
    <filtering>true</filtering>
  </resource>
</resources>

This will make Maven replace the variables in application.properties with maven properties and environemnt variable available at build time.

4. Now the packaged application.properties will contain current build number from TeamCity and maven artifact name and version from your pom.xml !

As a bonus I made the version available via a jsp called status.jsp. Getting the version property from application.properties into a jsp can be made in several ways of course. Since my project don’t contain any web stuff (only web services) I created the jsp in a hacky way:

<html><body>
  <p>Version: <%= BusinessServiceImpl.instance.version %></p>
</body></html>

And BusinessServiceImpl:

@Autowired
public String version;
public static StaffDiscountServiceImpl instance;

public StaffDiscountServiceImpl(} {
  instance = this;
}

The version attribute is autowired and defined in Spring’s applicationContext.xml:

<context:property-placeholder location="classpath:/application.properties "/>
<bean id="version" >
  <constructor-arg value="${version}" />
</bean>

There’s certainly better ways of doing this jsp stuff (comments, please!) but this was simple and it works.