16 July, 2008

Develop Enterprise Application Using Maven

Maven Integration For Eclipse IDE
- Web site address http://m2eclipse.codehaus.org/
- Eclipse new features web site address http://m2eclipse.sonatype.org/update/

Add Remote Repository For Nexus Index
Adding remote repository into the nexus index, is easy for you to search the dependency library in the remove repository.
Demo

Enterprise Application Structure

hr

|-
hr-biz
|-
hr-common
|-
hr-web
|-
hr-ear


  1. Create HR Parent Project Demo
  2. Create HR Common Module Demo
  3. Create HR Business Logic Module Demo
  4. Create HR Web Module Demo
  5. Create HR Enterprise Module Demo


Human Resource Business Logic

    HR business logic module implement using EJB3 technology, you can copy and paste the dependency settings into the pom.xml file. Below are the dependency needed to add into the project.

  1. EJB 3.0
    <dependency>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-ejb_3.0_spec</artifactId>
        <version>1.0.1</version>
        <scope>provided</scope>
     </dependency>
  2. JPA 3.0
    <dependency>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-jpa_3.0_spec</artifactId>
        <version>1.1.1</version>
        <scope>provided</scope>
    </dependency>
  3. Common Library
    <dependency>
         <groupId>biz.sdscom</groupId>
         <artifactId>hr-common</artifactId>
         <version>0.0.1-SNAPSHOT</version>
     </dependency>



Maven ejb plugin will package the project according to the ejb3 specification, maven ejb 3 plugin will needed to add into the pom.xml. see the example below:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <configuration>
                    <ejbVersion>3.0</ejbVersion>
                </configuration>
            </plugin>
        </plugins>
    </build>


Create persistence.xml file in src/main/resource/META-INF with the following setting
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="HR-PU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/hr</jta-data-source>
<properties>
  <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
  <property name="hibernate.show_sql" value="true"/>
  <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
  <!-- Required by OC4j -->
  <property name="hibernate.query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory" />
    <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.OrionTransactionManagerLookup" />
    <!-- Required by OC4j -->
</properties>
</persistence-unit>
</persistence>



If your application server require you to add deployment file, you can add your application server deployment file into src/main/resource/META-INF directory.


Full pom.xml listing

<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<parent>
  <artifactId>hr</artifactId>
  <groupId>biz.sdscom</groupId>
  <version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>biz.sdscom</groupId>
<artifactId>hr-biz</artifactId>
<name>HR Business Logic</name>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<description />
<dependencies>
  <dependency>
    <groupId>org.apache.geronimo.specs</groupId>
    <artifactId>geronimo-jpa_3.0_spec</artifactId>
    <version>1.1.1</version>
    <scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>org.apache.geronimo.specs</groupId>
    <artifactId>geronimo-ejb_3.0_spec</artifactId>
    <version>1.0.1</version>
    <scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>biz.sdscom</groupId>
    <artifactId>hr-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </dependency>
</dependencies>
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-ejb-plugin</artifactId>
      <configuration>
        <ejbVersion>3.0</ejbVersion>
      </configuration>
    </plugin>
  </plugins>
</build>
</project>


Human Resource Web Module
Web module will use MyFaces JSF implementation and RichFaces, to add dependency library into project we need to add jboss maven 2 repo into the maven indexs in Eclipse IDE. The repository url is http://repository.jboss.com/maven2/  and using jboss-maven2 as the repository id.
This is the pom.xml content for the web module

<?xml version="1.0" encoding="UTF-8"?>

<project>

  <parent>

    <artifactId>hr</artifactId>

    <groupId>biz.sdscom</groupId>

    <version>0.0.1-SNAPSHOT</version>

  </parent>

  <modelVersion>4.0.0</modelVersion>

  <groupId>biz.sdscom</groupId>

  <artifactId>hr-web</artifactId>

  <packaging>war</packaging>

  <name>HR Web Module</name>

  <version>0.0.1-SNAPSHOT</version>

  <url>http://maven.apache.org</url>

  <build>

    <finalName>hr-web</finalName>

  </build>

  <dependencies>

    <dependency>

    <groupId>junit</groupId>

    <artifactId>junit</artifactId>

    <version>3.8.1</version>

    <scope>test</scope>

  </dependency>

  <dependency>

    <groupId>biz.sdscom</groupId>

    <artifactId>hr-biz</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <scope>provided</scope>

  </dependency>

  <dependency>

    <groupId>biz.sdscom</groupId>

    <artifactId>hr-common</artifactId>

    <version>0.0.1-SNAPSHOT</version>

  </dependency>

  <dependency>

    <groupId>org.apache.geronimo.specs</groupId>

    <artifactId>geronimo-jsp_2.1_spec</artifactId>

    <version>1.0</version>

    <scope>provided</scope>

  </dependency>

  <dependency>

    <groupId>org.apache.geronimo.specs</groupId>

    <artifactId>geronimo-servlet_2.5_spec</artifactId>

    <version>1.1.2</version>

    <scope>provided</scope>

  </dependency>

  <dependency>

    <groupId>org.apache.myfaces.core</groupId>

    <artifactId>myfaces-api</artifactId>

    <version>1.2.3</version>

  </dependency>

  <dependency>

    <groupId>org.apache.myfaces.core</groupId>

    <artifactId>myfaces-impl</artifactId>

    <version>1.2.3</version>

  </dependency

  <dependency>

    <groupId>org.richfaces.framework</groupId>

    <artifactId>richfaces-api</artifactId>

    <version>3.2.1.GA</version>

  </dependency>

  <dependency> 

    <groupId>org.richfaces.framework</groupId>

    <artifactId>richfaces-impl</artifactId>

    <version>3.2.1.GA</version>

  </dependency>

<dependency>

    <groupId>org.richfaces.ui</groupId>

    <artifactId>richfaces-ui</artifactId>

    <version>3.2.1.GA</version>

  </dependency>

</dependencies> 

</project>


HR Enterprise Module
    This module is to package the project in EAR format to deploy into application server. This example was targeted OC4J 11 Preview 4. Create orion-application.xml  file and put it in the src/main/application/META-INF
File content
<orion-application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://xmlns.oracle.com/oracleas/schema/orion-application-10_0.xsd"
deployment-version="11.1.1.0.0" default-data-source="jdbc/hr"
schema-major-version="10" schema-minor-version="0">
    <ejb-module remote="false" path="hr-biz-0.0.1-SNAPSHOT.jar" />
     <web-module id="hr-web-0.0.1-SNAPSHOT" path="hr-web-0.0.1-SNAPSHOT.war" />
    <library path="./"/>
     <persistence path="persistence" />
    <jazn provider="XML" />
    <log>
        <file path="hr-application.log" />
     </log>
     <imported-shared-libraries>
        <remove-inherited name="oracle.toplink" />
     </imported-shared-libraries>
</orion-application>


Add dependency and plugin into pom.xml

pom.xml file content
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<parent>
  <artifactId>hr</artifactId>
  <groupId>biz.sdscom</groupId>
  <version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>biz.sdscom</groupId>
<artifactId>hr-ear</artifactId>
<packaging>ear</packaging>
<name>HR EAR</name>
<version>0.0.1-SNAPSHOT</version>
<description />
<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-ear-plugin</artifactId>
    <version>2.3</version>
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
        </manifest>
      </archive>
      <modules>
        <webModule>
          <groupId>biz.sdscom</groupId>
          <artifactId>hr-web</artifactId>
          <contextRoot>hr</contextRoot>
        </webModule>
        <ejbModule>
          <groupId>biz.sdscom</groupId>
          <artifactId>hr-biz</artifactId>
        </ejbModule>
        <jarModule>
          <groupId>biz.sdscom</groupId>
          <artifactId>hr-common</artifactId>
        </jarModule>
      </modules>
    </configuration>
  </plugin>
</plugins>
</build>
<dependencies>
  <dependency>
    <groupId>biz.sdscom</groupId>
    <artifactId>hr-biz</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <type>ejb</type>
  </dependency>
  <dependency>
    <groupId>biz.sdscom</groupId>
    <artifactId>hr-web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <type>war</type>
  </dependency>
  <dependency>
    <groupId>biz.sdscom</groupId>
    <artifactId>hr-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </dependency>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.2.1.ga</version>
   </dependency>
<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate</artifactId>
   <version>3.2.1.ga</version>
   </dependency>
  </dependencies>
</project>




0 comments: