Tuesday, August 15, 2017

Create Java EE Maven Project

Project structures

Illustrate how to create Java EE application project with maven. The whole application has one parent pom, 1 web module, 1 ejb module, 1 normal java module and 1 ear assembly project to package former those modules together.
Final structures should be like below,

+helloworld
 |----helloworld-ear
 |----helloworld-war
 |----helloworld-ejb-local
 |----helloworld-ejb-api
 |----pom.xml

maven commands

Run following maven commands,
cd <baseDir>
mvn archetype:generate -B -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=pom-root -DarchetypeVersion=1.1 -DgroupId=com.nwb.jpractice.jee7 -DartifactId=helloworld -Dversion=1.0-SNAPSHOT

cd <baseDir>/helloworld
mvn archetype:generate -B -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.1 -DgroupId=com.nwb.jpractice.jee7 -DartifactId=helloworld-ejb-api -Dversion=1.0-SNAPSHOT

mvn archetype:generate -B -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=ejb-javaee7 -DgroupId=com.nwb.jpractice.jee7 -DartifactId=helloworld-ejb-local -Dversion=1.0-SNAPSHOT

mvn archetype:generate -B -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=webapp-javaee7 -DgroupId=com.nwb.jpractice.jee7 -DartifactId=helloworld-war -Dversion=1.0-SNAPSHOT

mvn archetype:generate -B -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=ear-javaee7 -DgroupId=com.nwb.jpractice.jee7 -DartifactId=helloworld-ear -Dversion=1.0-SNAPSHOT

Optimise pom and deployment descriptor

After that manually optimise some the pom and jee deployment descriptors.

Parent pom

  • Specify encoding UTF-8
  • Specify source code java version
  • Specify which jvm version the compiled class compatible
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties> 

EAR Pom

Configure maven-ear-plugin
  • Specify assembled modules [1]
  • Customize module filename [2]. You may want to simply helloworld-ejb-local-1.0-SNAPSHOT.jar to helloworld-ejb-local.jar.
  • Adopt skinny wars [3]. Libraries should have been in WEB-INF/lib are copied to EAR's lib and shared within whole application to shrink the size of war.
  • Specify context-root for web module [4]. The context-root will be written into META-INF/application.xml. You don't have to create application.xml manually maven ear plugin is able to generate it when building project.
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <defaultLibBundleDir>lib</defaultLibBundleDir><!-- libraries of this application, this path is written into each META-INF/MANIFEST.MF during build-->
                    <skinnyWars>true</skinnyWars><!-- [3]-->
                    <modules>
                        <ejbModule><!-- [1] ejb module-->
                            <groupId>com.nwb.jpractice.jee7</groupId>
                            <artifactId>helloworld-ejb-local</artifactId>
                            <bundleFileName>helloworld-ejb-local.jar</bundleFileName><!-- [2]-->
                        </ejbModule>
                        <webModule><!-- [1] war module-->
                            <groupId>com.nwb.jpractice.jee7</groupId>
                            <artifactId>helloworld-war</artifactId>
                            <bundleFileName>helloworld-web.war</bundleFileName><!-- [2]-->
                        </webModule>
                    </modules>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
    <dependencies>
        <dependency>
            <groupId>com.nwb.jpractice.jee7</groupId>
            <artifactId>helloworld-war</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>war</type>
        </dependency>
        <dependency>
            <groupId>org.nwb.jpractice.jee7</groupId>
            <artifactId>helloworld-ejb-local</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>ejb</type>
        </dependency>
        <!-- helloworld-ejb-api and other dependencies will be in ear's lib folder -->
        <dependency>
            <groupId>com.nwb.jpractice.jee7</groupId>
            <artifactId>helloworld-ejb-api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>jar</type>
        </dependency>
    </dependencies>

Now this application can work, you can start building it and deploy it to weblogic.







No comments:

Post a Comment