Sunday, March 16, 2014

Rapid application development with Jboss Forge


Why the Forge!?

Starting a new project can be time consuming task with having to decide the project structure and getting all the configuration files in the correct locations with the correct entries.

JBoss Forge is a decent solution this that helps you jump-start a project very quickly. It helps with the initial 'setting-up' of the different layers of the application, creating required artifacts, testing and deployment. It's a shell based RAD tool that is extensible through plugins and has excellent eclipse integration. Changes on the Forge console immediately reflect on the rest of Eclipse and vice versa. 

It's comparable to using maven architypes. However with architypes, you would have to choose the 'most suitable' architype, run it once and it's done, leaving you to get rid of all the stuff you don't need. Whereas with Forge you can build and shape your project according to your technical direction in an incremental way, with only the pieces that you do need.

Preconditions

In order to use Forge on Eclipse you need to install the Forge plugin on eclipse. Search for JBoss Tools on Eclipse marketplace and install (only) Forge Tools.


After this you should be able to see the forge console (Window>Show View>Forge Console).



You will also need a JBoss runtime in your eclipse that you can deploy to, and make sure you have the maven plugin for eclipse installed (m2e)


Forging your way ahead

Now to demonstrate how we can build a basic web application and RESTful web service from scratch using Forge on Eclipse - in record time.

First, lets create a new project assuming it's going to be a car sales application.
[no project] EclipseWS $ new-project --named CarSales --topLevelPackage com.joerajeev.carsales --type war --projectFolder CarSales
Notice on the Forge console that this created the basic project structure including a pom.xml file. Apparently, JBoss developer studio would have imported the project into the IDE automatically, however since we are using eclipse we have to import the project manually. It would be best to import it as a maven project.

Now lets set up the configurations for the persistence layer. These commands may prompt you with various options. Most often the default option will suffice.
CarSales $ persistence setup --provider HIBERNATE --container JBOSS_AS7
Notice this creates the persistance.xml, updates the pom.xml and also pulls in the required dependencies.

Before creating our entities lets set up bean some validation. Hibernate validation allows us to define constraints in one single place in the code, and have that applied to all the layers of a JavaEE application.
CarSales $ validation setup --provider HIBERNATE_VALIDATOR
As before this updates and pulls in the required dependencies in addition to creating the validation.xml

Lets create an entity for a Car.
CarSales $ entity --named Car
This creates a basic Car entity class and loads it for further manipulation to it such as updating it with fields and validations. If you really want to do this using Forge, it can be done as follows;
Car.java $ field string --named make
Car.java $ constraint NotNull --onProperty make
Car.java $ field string --named model
Car.java $ constraint NotNull --onProperty model
Car.java $ field int --named year
Car.java $ constraint NotNull --onProperty year
Car.java $ field number -type java.math.BigDecimal --named price

We can get back to the 'project level' with
Car.java $ cd ~~

If the class needs to be further updated later you can always manually load the file as follows:
CarSales $ pick-up src\main\java\com\joerajeev\carsales\model\Car.java

Since this is going to be a web app, Forge can help setup the some front end scaffolding for the entities, so that we can do some CRUD operations.
CarSales $ scaffold setup
CarSales $ scaffold from-entity com.joerajeev.carsales.model.*
Note this created the web.xml, a directory structure for a web app and the view (xhtml) pages to do basic CRUD operations.

Lets build and deploy the project
CarSales $ build --notest
Builds us the war file

We can deploy the application to JBoss AS by installing and using the jboss-as-7 plugin
CarSales $ forge install-plugin jboss-as-7
CarSales $ as7 setup
CarSales $ as7 deploy
Note that you will have to provide the path to your JBoss AS installation directory during the as7 setup.

Now we have a application with some basic CRUD functionality deployed at http://localhost:8080/CarSales.


The UI here from the scaffolding we did before. We can take it from here and customize our application for according to the requirements.

Lets go ahead and create some RESTful endpoints for our entity as well
CarSales $ rest setup --activatorType APP_CLASS
CarSales $ rest endpoint-from-entity --contentType application/json ~.model.*
This generates a REST endpoint with CRUD operations for our Car entity.

Now if we redeploy, enter a few cars from the web app (http://localhost:8080/CarSales) and then try the RESTful service endpoint (http://localhost:8080/CarSales/rest/cars) we will get a list of cars in JSON format.


Caveats

  • Forge is still not mature enough to have support across a wide range of technologies. Hopefully once it catches on it will be extended through plugins to support a wider tech stack.
  • It could be more stable. For a tool that's supposed to make your life easier, it did give me some pain with some stuff (especially with setting up and testing with Arquillian), which required an advanced understanding of Maven to resolve.
  • As with any new technology, new commands need to be remembered. However, it does have tab completion which makes things fairly easier.




No comments:

Post a Comment