Enterprise Java

Java EE7 and Maven project for newbies – part 8

It’s been a long time since my last post, for this series of  tutorials. Its time to resume and add new features on our simple project. As I have mentioned in previous posts, this series of posts is targeting mostly Maven and JavaEE7 newcomers, I welcome any questions or comments (and fixes) on the contents below. I promise I will try to keep up with the updates.

Git tag for this post?

The tag for this post, is this post8, and can be found on my bitbucket repo.

What has changed from the previous post?

  • Some comments and fixes on the code from readers have already been integrated.Thank you very much all for your tome.
  • I have updated the Wildfly Application Server version from 8.1 to 8.2, so all the examples and code runs under the new server.
  • I have also updated the versions of the Arquillian BOM (s), to the latest version which is now 1.1.7.Final
  • I have also added a property under the sample-parent project that indicates the path that the various maven modules will download and use Wildfly server, automatically so that you don’t have to download it on your own. The server will be automatically downloaded and extracted to the predefined path, as soon as you try to execute one of the unit tests from the previous posts (sample-services module)
            <!--path to download wildfly-->
            <wildfly-server-home>${project.basedir}/servers/</wildfly-server-home>
    

Adding a JSF enabled war Maven Module on our ear

Eventually our project structure already featured a war (see sample-web)

CapturFiles-Feb-15-2015_19.57.36maven module. So there is no extra module introduced rather than changes on the existing pom.xml files of the parent and the module itself.

Step 1  changes on web.xml

Our  application server is already bundled with the required libraries and settings in order to support applications that make use of the JSF 2.2 specification. Wildfly bundles Mojarra 2.2.8. What we have to do is just update some configuration descriptors (eventually only one). The most important is web.xml which now looks like this.

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

Step 2 Packaging of war and the skinny war issue

Our war module, is following a packaging scheme called skinny war. Please read the following page from the Apache maven war plugin. To cut a long story short, in order to reduce the overall size of our deploy able (ear), we package all the required libraries under a predefined folder on the ear level, usually is called \lib and we don’t include libraries under the war’s WEB-INF\lib folder. The only thing you need to do, is add those dependencies of your war to the ear level. Despite the fact that the overall ‘hack’ does not feel very maven like, it works if you follow the proposed configuration, but there are cases that skinny war packaging wont work. One of these is usually for JSF based JavaEE web applications where the implementation of the JSF widget engine should be packaged within the war’s WEB-INF\lib.

For our sample project, I am using the excellent and free Primefaces library, which I highly recommend for your next JSF based project. So I need to define a dependency on my war module for the primefaces jar but by pass the skinny war mechanism only for this jar, so that it is packaged in the right place. This is how we do it.

<!-- from the war module pom.xml -->

<!-- This is the dependency --> 

 <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>${primefaces-version}</version>
  </dependency>

<!-- See the packaging exclude, we exclude all the jars apart from the one we want 
to be bundled within the WAR --> 

 <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <packagingExcludes>%regex[WEB-INF/lib/(?!primefaces).*.jar]</packagingExcludes>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>sample-services-${project.version}.jar</Class-Path>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>

Step 3 Add some jsf love, a managed bean and an xhtml page with the appropriate tags.

CapturFiles-Mar-02-2015_23.18.58

Our code is just a small table, and a couple of tags from Primefaces. If you think that you need to read more about JSF 2.X please have a look on the following links

Step 4 Package and deploy to a running server.

Start your wildfly (you are expected to have one under your project-base dir and the subfolder servers

<wildfly-server-home>${project.basedir}/servers/</wildfly-server-home>

and then under the sample-parent project type.

mvn clean install -Ph2

You should have your demo JSF 2.2 enabled demo app, on http://localhost:8080/sample-web/ and see something like the following.

CapturFiles-Mar-02-2015_23.32.01

That’s all, this will give you a simple start in order to expand on something more than a demo!

As always you will find the complete – example under tag post8 .

Reference: Java EE7 and Maven project for newbies – part 8 from our JCG partner Paris Apostolopoulos at the Papo’s log blog.

Paris Apostolopoulos

Paris is a senior software engineer focusing on J2EE development, loves Business process modelling and is keen on software quality challenges. He is passionate about Java and Java communities. He is a co-founder and administrator of the first Java User Group in greece(JHUG.gr) and occasional speaker on meet-ups and seminars and regular blogger. For his contributions and involvement on the Java community he has been awarded the title of Java Champion in 2007 by Sun Microsystems.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
javaee rulz
javaee rulz
9 years ago

wow, what can I say about Java EE. You need 8 blog posts to create a simple example application…

javapapo
9 years ago

Hi, you totally miss the point, this is a series of posts for complete newbies and focuses on project setup, different components, packaging.

Back to top button