Build Java EE Projects with Forge 3

JBoss Forge ships now with the 3.x release which brings several enhancements to the project creator engine. Besides it, the release 3.x has changed the option names syntax a bit, switching to a dashed and lower-cased structure. Let’s see how we can quickly create a project to be deployed on WildFly 10.

Starting Forge 3

Forge downloads are available at:

Pickup the latest stable version and start the forge tool:

$ bin/forge

You can even skip the installation if you have JBoss Developer Studio which already ships with Forge Console. Just switch the view to Forge Console and you can start using Forge!

JBoss Forge 3 tutorial

Creating a simple Java EE Project with Forge

Let’s start building up a new project named “demo-forge” which uses the JAVA EE 7 stack, therefore its target will be WildFly:

project-new  --named demo-forge --type war --stack JAVA_EE_7

What is really cool if you are using JBoss Developer Studio is that the project will be automatically created in your Workspace so no need to search and import it:

JBoss Forge 3 tutorial

Now let’s create one Model object which will be eventually scaffolded:

jpa-new-entity --named Customer

jpa-new-field --named name --not-nullable
jpa-new-field --named surname --not-nullable
jpa-new-field --named email --not-nullable

As you can see, we have created the Customer Entity class and included a basic NotNull constraint on the top of it. If we want, we can create more complex constraint such as Regular Expression patterns as follows:

constraint-add --on-property email --constraint Pattern --regexp ^(.+)@(.+)$

Done with the model, we will now create the Faces layer through the scaffold-generate command, specifying the provider (Faces) and the Web context root:

scaffold-generate --targets org.demo.forge.model.Customer --web-root demo-forge --provider Faces

Please note that Forge uses lots of defaults to simplify your project creation. In our case, as we have omitted the base package name in the project creation, the root package org.[] has been usedDone with the project, it’s time to build it, which will trigger the maven install command:@SHELLbuild

Deploying the project to WildFly can be easily achieved by adding to your pom.xml the Maven’s WildFly plugin


Otherwise, you can do it directly from Forge, but you have to install a couple of adds-on first:

addon-install-from-git --url  --coordinate org.jboss.forge.addon:as
addon-install-from-git --url  --coordinate org.jboss.forge.addon:jboss-as-wf

Now you will need to set up at first the application server version to be used with:

as-setup --server wildfly --version 10.1.0.Final

And finally, you can deploy it with:


Here is your application up and running:

jboss forge tutorial

Please note that on some versions of Linux (such as Fedora 21) you can incur in issues when installing the jboss-as-addon, due to an excessive number of io watches created. In order to cope with it (if you really don’t want to install the Maven WildFly plugin :-)!) you can increase that number with:

$ sudo sysctl fs.inotify.max_user_watches=20000

How to create a CRUD REST application with Forge

In the second example, we will show how to scaffold a JAX-RS application which uses JPA to store your data.

Firstly, create a new Project named “demo-rest” which uses JAVA EE 7 stack

project-new  --named demo-rest --type war --stack JAVA_EE_7

We will then add an Entity object named Customer

jpa-new-entity --named Customer

jpa-new-field --named firstname
jpa-new-field --named surname
jpa-new-field --named email

Now we move to the top of our project and we generate the REST Endpoints to CRUD our Entities:

cd ~~
rest-generate-endpoints-from-entities --targets 

As you can see, the following EJB Rest Resource has been created:

public class CustomerEndpoint {
	@PersistenceContext(unitName = "demo-rest-persistence-unit")
	private EntityManager em;

	public Response create(Customer entity) {
		return Response.created(

	public Response deleteById(@PathParam("id") Long id) {
		Customer entity = em.find(Customer.class, id);
		if (entity == null) {
			return Response.status(Status.NOT_FOUND).build();
		return Response.noContent().build();

	public Response findById(@PathParam("id") Long id) {
		TypedQuery<Customer> findByIdQuery = em
						"SELECT DISTINCT c FROM Customer c WHERE = :entityId ORDER BY",
		findByIdQuery.setParameter("entityId", id);
		Customer entity;
		try {
			entity = findByIdQuery.getSingleResult();
		} catch (NoResultException nre) {
			entity = null;
		if (entity == null) {
			return Response.status(Status.NOT_FOUND).build();
		return Response.ok(entity).build();

	public List<Customer> listAll(@QueryParam("start") Integer startPosition,
			@QueryParam("max") Integer maxResult) {
		TypedQuery<Customer> findAllQuery = em.createQuery(
		if (startPosition != null) {
		if (maxResult != null) {
		final List<Customer> results = findAllQuery.getResultList();
		return results;

	public Response update(@PathParam("id") Long id, Customer entity) {
		if (entity == null) {
			return Response.status(Status.BAD_REQUEST).build();
		if (id == null) {
			return Response.status(Status.BAD_REQUEST).build();
		if (!id.equals(entity.getId())) {
			return Response.status(Status.CONFLICT).entity(entity).build();
		if (em.find(Customer.class, id) == null) {
			return Response.status(Status.NOT_FOUND).build();
		try {
			entity = em.merge(entity);
		} catch (OptimisticLockException e) {
			return Response.status(Response.Status.CONFLICT)

		return Response.noContent().build();

Now our Forge REST project is complete.

Scripting with JBoss Forge

Running a JBoss Forge script is an handy feature to create quickly an application starting from a script file. All you have to do is starting Forge and run the script file as follows:

run <scriptname>

As an example, pickup the following script from the quickstarts:

Run this file from the Forge console using the run command:

 $ run generate.fsh

At this point, Forge creates the new project and builds it.