Need to start a project in lightning speed? If you want to skip those dirty works in setting up a project framework, well…, I just found out a great utility just for that. It is called Appfuse 2. Now with version 2, it is tied together with Maven 2 which makes the setup a lot easier. The prerequisites for applying Appfuse is Maven 2 and MySQL 5. You need to install these 2 in your machine.
There are several archetypes you could choose from, and for the sake of writing, I just choose appfuse-basic-jsf. So I just run below command, it will create a project with JSF,Spring & Hibernate. There are also other persistence files included, such as JPA and iBatis.
mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-jsf -DremoteRepositories=http://static.appfuse.org/releases -DarchetypeVersion=2.0.2 -DgroupId=org.dhydrated.appfuse.jsf -DartifactId=appfuseJsfWeb
Then, I need to tweak the pom according to my MySQL installation. By default, it uses root with no password as the MySQL user. So I just simply add my password in the pom file below.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.dhydrated.appfuse.jsf</groupId> <artifactId>appfuseJsfWeb</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>AppFuse JSF Application</name> <url>http://www.mycompany.com</url> ... <properties> ... <jdbc.username>root</jdbc.username> <jdbc.password>password</jdbc.password> </properties> </project>
Then, I run below command and let Maven 2 do all the boring stuffs such as dependencies checking, creating tables in database and etc. It will take a while if you doing it for the first time.
mvn
Then, I need to package it into war, so I just simply run below command.
mvn war:war
Next, I need to tweak again the pom since I’m deploying the application into Glassfish 2 in my machine. By default, the application is set to run with Jetty or Tomcat. So if your choice of application server is one of these default settings, you can skip this step. I’ll add below maven plugin to enable me to deploy my application into Glassfish 2.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
...
<build>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>deploy</id>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>C:/Program Files/glassfish-v2ur1/bin/asadmin.bat</executable>
<arguments>
<argument>deploy</argument>
<argument>--user=admin</argument>
<argument>--passwordfile="C:/passwordfile.txt"</argument>
<argument>--host=localhost</argument>
<argument>--port=14360</argument>
<argument>target/${artifactId}.${packaging}</argument>
</arguments>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>
Above, I’m using exec-maven-plugin to run an executable command to deploy my war into Glassfish 2. In order to call above plugin, I typed below command.
mvn exec:exec
After that, you should be able to see your application is being deploy from your server console. After all these steps, Presto!, now you have a full-fledged Java Enterprise application running on your server. Thanks to Spring, it is also equipped with security and transaction management. Couple of users with specific roles are already included.
If I run my application, I should get the Login page as below.
Above is the login page. One of the default user is admin.
Above is the home page once you are logged in. It uses Struts-Menu for menu layout.
It also includes CRUD pages for Users.
How’s that for speed? It only took 10 minutes to create this application (if you know how to do it, of course). There are several other UI frameworks you could choose from, besides JSF. I guess the 2 most commonly used are JSF and Struts2. If I only knew about Appfuse 2, I would have used it on my last project. It is awesome. I don’t need to do all the basic stuffs anymore and I can just straight away focus on the business requirements. Yeah!


