19 May 2011

Deploying a Random JSF 2.0 Example on WebLogic Server using Maven and NetBeans and Maven

I had a bit of a hunt around yesterday looking for some JSF 2.0 example applications to deploy.  I quickly came across a comprehensive JSF 2.0 tutorial @ http://www.coreservlets.com/JSF-Tutorial/jsf2/ published by Marty Hall.

For each of the tutorial sections on this site, the example code and pages that are used to demonstrate the concepts and discussion are published as accessible source code.

So I thought I'd try a few of them out with WebLogic Server 10.3.5. 

The examples I looked at didn't contain a build script, so I very simply converted them into Maven projects, which I then was able to open immediately in NetBeans (this feature rocks!).  I then built, packaged and ran the application on a WebLogic Server 10.3.5 domain, all from NetBeans.

All up, I'd say it tool ~2 mins to get the example loaded and building in NetBeans and deployed to  WebLogic Server.

It takes a lot more to describe it below than it does to actually do it.

Here's what I did to get the data-tables example to build, package and deploy to WebLogic Server. 

1. Download the zip file from http://www.coreservlets.com/JSF-Tutorial/jsf2/code/data-tables.zip.

2. Extract the zip file to a local directory, which produces:

/data-tables/WebContent
/data-tables/src
/data-tables/build

3. Generate a Java EE  Web Application using the Maven Archetype mechanism.  What a great feature this is in Maven. 

$ mvn archetype:generate
...
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
...
263: remote -> webapp-j2ee13 (-)
264: remote -> webapp-j2ee14 (-)
265: remote -> webapp-javaee6 (-)
266: remote -> webapp-jee5 (-)
...
Choose a number: 107: 266
Choose version:
1: 1.0
2: 1.0.1
3: 1.1
4: 1.2
Choose a number: 4:
Define value for property 'groupId': : coreservlets
Define value for property 'artifactId': : data-tables
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  coreservlets: :
Confirm properties configuration:
groupId: coreservlets
artifactId: data-tables
version: 1.0-SNAPSHOT
package: coreservlets
 Y: : y

4. Copy the WebContent and src directories from the data-tables example into the new Maven project structure.

$ cp -rp ~/Projects/Java/data-tables/WebContent/* src/main/webapp/
$ cp -rp ~/Projects/Java//data-tables/src/ src/main/java/


5. Start NetBeans and simply open the data-tables/pom.xml file to load the project into the IDE.



Click around and explore the project to get a sense of what it contains.




6. Add the jsf-api dependency to the POM so the ManagedBean classes can be compiled.

You can either manually add this direct to the pom.xml as:
    
    <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>


Or use the "Add Dependency" option on the project to search for it and enter it via a dialog:




7. Add a weblogic.xml deployment descriptor to reference the JSF 2.0 shared-library deployed on the WLS domain.

I simply coped in an existing version of the file I had used before.  You can also use the "New --> WebLogic --> WebLogic Descriptor" option in NetBeans to add it.


Add a library-ref to the jsf-2.0.war shared-library deployed on the WLS 10.3.4 domain.

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app>
  <context-root>/data-tables-1.0-SNAPSHOT</context-root>
  <library-ref>
    <library-name>jsf</library-name>
    <specification-version>2.0</specification-version>
    <implementation-version>1.0.0.0_2-0-2</implementation-version>
  </library-ref>

</weblogic-web-app>

8. Run the project, select the WLS 10.3.5 Server type and watch it get built and packaged via Maven and deployed by NetBeans.  This will start the WLS 10.3.5 server if necessary.





9. View the example application in a browser


Summary: there it is, a random JSF 2.0 example application, converted to a Maven project, opened in NetBeans and deployed to WebLogic Server 10.3.5, all in ~ 2mins.

4 comments:

Unknown said...

Thanks for your post. I have one question: how did you deployed (and where did you get) shared JSF library?

Buttso said...

Hi -- Weblogic Server in versions 10.3.3+ provide a version of JSF 2.0 that you can deploy and use in your applications.

See $MW_HOME/wlserver/common/deployable-libraries/jsf-2.0.war.

-steve-

Sushma said...

Hi,

Great post!
I do have a question. Can I deploy the application developed using JSF 2.0, which uses servlet version 3.0 on WL server 10.3.2?

Thanks,
Sushma

Buttso said...

Hi Sushma -

In order to use Servlet 3.0 you will need WLS 12.1.1.

WLS 10.3.x supports Java EE 5 as it's core spec version with some later spec updates such as JSF 2.0 and JPA 2.0.

-steve-