Seiten

Mittwoch, 21. Mai 2014

Simple Forge for Rapid Development

This article covers the simple usage of Forge to build up an application (or at least an almost complete stack for a prototype).
So what is Forge? It is a command line tool to create and configure Java projects. You can setup different modules (for example cdi or jpa) and Forge generates all resources needed. This can be anything from java classes to deployment descriptors right up to java test classes or JSF files. On top of that this behavior is incremental, so that you can use it at any time you want.

The setup of the modules is done by Convention over Configuration so that everything is quite simple and comprehensible. The Command line also supports tab-completion and makes the usage very fluent.

You may ask why not Forge 2 (it is already in version 2.5.0). It is faster and a little bit easier but it lacks in one point: the Arquillian module is currently not migrated but it is planed.

Example
At first we have to tell Forge to use defaults. Then we create a project with name forgetest and the topLevelPackage de.bischinger. With this a Maven War project is created.
[no project] Development $ set ACCEPT_DEFAULTS true;
[no project] Development $ new-project --named forgetest --topLevelPackage de.bischinger;
After this you can see that the shell changes from "no project" to your project name "forge test".
Now we want to add persistence namely Hibernate and WildFly (creates a persistence.xml and adds dependency to pom.xml)
[forgetest] forgetest $ persistence setup --provider HIBERNATE --container WILDFLY;
Create a JPA entity customer with a required field name. For this we need also to setup JPA validation.
[forgetest] forgetest $ validation setup --provider HIBERNATE_VALIDATOR;
[forgetest] forgetest $ entity --named Customer --package de.bischinger.model;
[forgetest] Customer.java $ field string --named name;
[forgetest] Customer.java $ constraint NotNull --onProperty name;
The interesting part is once again that after the creation the shell switches to the created context so that you can easily create the fields. You can also list the context with ls.

Now we want to build a simple JSF UI for the customer entity. At first we need to setup Scaffold which generates our UI.
[forgetest] Customer.java $ scaffold setup;
[forgetest] Customer.java $ scaffold from-entity de.bischinger.model.Customer.java;
After this we can build our application with build and after deploying (which can also be done with Forge but this would be another topic) we can see the following JSF-Page. On the left side is our link to the customers where we can already create, search and delete customers. It also has a paging feature.
Ok - the GUI still must be customized but this is again another topic. The point is that i have spent so much time in the past to reach a state like this - now it is possible within just 2 minutes.

But we are not finished yet - what about tests? For this we can install the Arquillian plugin into our project. We setup Arquillian to use a WildFly-Managed-Container with JUnit after which we can create our tests.
[forgetest] Customer.java $ forge install-plugin arquillian;
[forgetest] Customer.java $ arquillian setup --containerName WILDFLY_MANAGED --testFramework junit;
[forgetest] Customer.java $ arquillian create-test --class de.bischinger.model.Customer.java;
Done.

Conclusion

From my point of view this tool is worth a lot. I can integrate and configure almost any standard technology (JPA, Validation...) i want in no time. This way i can really concentrate on the business. Forge is nicely integrated in the JBoss Developer Studio, it can also be used on any shell and it is easy to understand. The generated sources can also be studied to familiarize with unknown technologies.