#HOWTO: Bootstrap a JSF 2.3 Maven project in seconds

Bootstrapping a new JSF can be quite cumbersome if you are new to Java EE. Even after weeks of training, I had to look up the anatomy of the or the correct folder structure. As the JSF technology is still a valid and reasoned choice for frontend development in enterprises, I’ve created a simple Maven archetype to bootstrap a ready-to-go JSF application in seconds.

If you are completely new to JSF, make sure you read one of my blog posts about this technology first: #WHATIS? JavaServer Faces, #HOWTO: Simple CRUD table with JSF 2.3 or this great book: The Definitive Guide to JSF in Java EE 8

Using this Maven archetype you’ll get a fresh project with the following dependencies and configurations:

  • Java EE 8 API dependency
  • Microprofile 2.0.1 dependency
  • Primefaces 6.2 and all-themes dependency
  • Omnifaces 3.2 dependency
  • Mockito and JUnit dependencies for efficient testing
  • web.xml for JSF development with bootstrap as selected Primfaces theme
  • beans.xmlwith bean-discoery-mode="all"
  • persistence.xml configured for JTA persistence unit
  • microprofile-config.properties for configuration
  • Simple index.xhtmlwith a backing bean SampleBean
  • a Dockerfile for latest Payara deployment at the root path level (“/”)
  • .gitignore with configuration for Java development with the IDE of your choice
  • a script for a convenient Docker build and deployment for both Windows and Linux/Mac

To use this archetype and to be able to deploy the project you need the following tools:

  • Java 8+ installation
  • Maven CLI (mvn) or the embedded Maven of the IDE of your choice
  • a running Docker daemon (optional, you can also deploy to a local application server)

The formal Maven command for creating a new project from my archetype looks like the following:

mvn archetype:generate -DarchetypeGroupId=de.rieckpil.archetypes \
    -DarchetypeArtifactId=javaee8-jsf \
    -DarchetypeVersion=1.0.1 \
    -DgroupId=<your project Group Id> \
    -DartifactId=<your project artifact Id>

With this command, you just have to fill in your groupId and your artifactId and you’ll be prompted to enter a version number or use the default 1.0-SNAPSHOT version.

For an even faster creation, you can use the following command to bypass the interactive mode:

mvn archetype:generate -DarchetypeGroupId=de.rieckpil.archetypes -DarchetypeArtifactId=javaee8-jsf -DarchetypeVersion=1.0.1 -DgroupId=de.rieckpil.blog -DartifactId=jsf-app -DinteractiveMode=false

With this, you have a ready-to-go Maven project setup for your Java EE 8 application. To deploy the application you can execute the buildAndRun.bat (for Windows) or buildAndRun.sh (for Linux/Mac) file to create the .war, build the Docker image and start a container afterward. If you have issues with executing the buildAndRun.sh with ./buildAndRun.sh just make it executable with chmod +x buildAndRun.sh.

Right now the .war is about 5 MB big, as the additional JSF dependencies (Primefaces & Omnifaces) are packaged within the .war. To make this more leaner you can add the dependencies to the application server of your choice and mark the dependencies within the pom.xml as <scope>provided</scope>. I’m also planning to do this with a custom Payara docker image in a future release of this archetype.

After the Payara server successfully started, you can visit http://localhost:8080/ and see the sample JSF page which displays an injected MicroProfile config property.

If you have any problems or improvements for this archetype, feel free to open an issue in the GitHub repository. For bootstrapping a simple Java EE 8 with Microprofile 2.0.1 application, have a look at my other Maven archetype.

Have fun bootstrapping new JSF applications with this archetype,

Phil

Leave a comment

Your email address will not be published. Required fields are marked *