Roo for rapid Spring development

Whenever we try to build a new project, we used to start from scratch with our favourite IDE. Spring is used for rapid development. In such case, initial setup of the project takes time. Everybody wants a template to begin with Spring. “Can I find a blank war for Struts?” is a frequently being asked question. Hence we introduce Roo from SpringTools

If you take any generic Spring development project, it contains the following environment setup activities. This is a recursive one for every project.

We can overcome this with Roo. It helps you to create and start a Spring Web in less than 10 minutes!

How to Use Roo?

Spring has some pre-requisites. You need to ensure JDK 1.6 is installed. You should have Maven and Roo. Download them and ensure it is in PATH. Usually I will create symlinks for the frequently used applications in /usr/bin. (This tutorial is created for Ubuntu or any Unix Platforms. I hope you are smart enough to not to ask me to write the same for Windows 🙂 )

$ sudo ln -s /home/pandian/Downloads/maven/apache-maven-3.0.4/bin/mvn /usr/bin/mvn

$ sudo ln -s /home/pandian/Downloads/spring-roo-1.2.1.RELEASE/bin/roo /usr/bin/roo

Now this is the time to execute Roo

$ roo
roo>hint

You should see something like the following!

The project has been created. But we need persistence support!

roo> project --topLevelPackage org.grassfield.roo
 Created ROOT/pom.xml
 Created SRC_MAIN_RESOURCES
 Created SRC_MAIN_RESOURCES/log4j.properties
 Created SPRING_CONFIG_ROOT
 Created SPRING_CONFIG_ROOT/applicationContext.xml

I like to use MySQL with Hibernate

roo> jpa setup --provider HIBERNATE --database MYSQL
Created SPRING_CONFIG_ROOT/database.properties
Please update your database details in src/main/resources/META-INF/spring/database.properties.
Updated SPRING_CONFIG_ROOT/applicationContext.xml
Created SRC_MAIN_RESOURCES/META-INF/persistence.xml
Updated ROOT/pom.xml [added dependencies mysql:mysql-connector-java:5.1.18, org.hibernate:hibernate-core:3.6.9.Final, org.hibernate:hibernate-entitymanager:3.6.9.Final, org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final, org.hibernate:hibernate-validator:4.2.0.Final, javax.validation:validation-api:1.0.0.GA, cglib:cglib-nodep:2.2.2, javax.transaction:jta:1.1, org.springframework:spring-jdbc:${spring.version}, org.springframework:spring-orm:${spring.version}, commons-pool:commons-pool:1.5.6, commons-dbcp:commons-dbcp:1.3]

I am not updating the DB information in the database.properties located in

/home/pandian/Downloads/spring-roo-1.2.1.RELEASE/bin/src/main/resources/META-INF/spring
$ cat database.properties
 #Updated at Sat Apr 28 11:47:56 IST 2012
 #Sat Apr 28 11:47:56 IST 2012
 database.driverClassName=com.mysql.jdbc.Driver
 database.url=jdbc\:mysql\://localhost\:3306
 database.username=root
 database.password=pandian

This is the time to setup the DB. Let create an entity Timer.

roo> entity jpa --class ~.Timer --testAutomatically
 Created SRC_MAIN_JAVA/org/grassfield/roo
 Created SRC_MAIN_JAVA/org/grassfield/roo/Timer.java
 Created SRC_TEST_JAVA/org/grassfield/roo
 Created SRC_TEST_JAVA/org/grassfield/roo/TimerDataOnDemand.java
 Created SRC_TEST_JAVA/org/grassfield/roo/TimerIntegrationTest.java
 Created SRC_MAIN_JAVA/org/grassfield/roo/Timer_Roo_Configurable.aj
 Created SRC_MAIN_JAVA/org/grassfield/roo/Timer_Roo_ToString.aj
 Created SRC_MAIN_JAVA/org/grassfield/roo/Timer_Roo_Jpa_Entity.aj
 Created SRC_MAIN_JAVA/org/grassfield/roo/Timer_Roo_Jpa_ActiveRecord.aj
 Created SRC_TEST_JAVA/org/grassfield/roo/TimerIntegrationTest_Roo_Configurable.aj
 Created SRC_TEST_JAVA/org/grassfield/roo/TimerDataOnDemand_Roo_DataOnDemand.aj
 Created SRC_TEST_JAVA/org/grassfield/roo/TimerIntegrationTest_Roo_IntegrationTest.aj
 Created SRC_TEST_JAVA/org/grassfield/roo/TimerDataOnDemand_Roo_Configurable.aj
 ~.Timer

Lets create a field to the Timer entity now

roo> field string --fieldName message --notNull
 Updated SRC_MAIN_JAVA/org/grassfield/roo/Timer.java
 Updated SRC_TEST_JAVA/org/grassfield/roo/TimerDataOnDemand_Roo_DataOnDemand.aj
 Created SRC_MAIN_JAVA/org/grassfield/roo/Timer_Roo_JavaBean.aj
 ~.Timer

Lets build the web project now with spring MVC

roo> hint web mvc

It will be useful to execute the hint command after every execution.

~.Timer roo> web mvc setup
 Created ROOT/src/main/webapp/WEB-INF/spring
 Created ROOT/src/main/webapp/WEB-INF/spring/webmvc-config.xml
 Created ROOT/src/main/webapp/WEB-INF/web.xml
 Updated ROOT/src/main/webapp/WEB-INF/spring/webmvc-config.xml
 Created ROOT/src/main/webapp/images
 Created ROOT/src/main/webapp/images/add.png
 Created ROOT/src/main/webapp/images/banner-graphic.png
 Created ROOT/src/main/webapp/images/create.png
 Created ROOT/src/main/webapp/images/delete.png
 Created ROOT/src/main/webapp/images/favicon.ico
 Created ROOT/src/main/webapp/images/list.png
 Created ROOT/src/main/webapp/images/resultset_first.png
 Created ROOT/src/main/webapp/images/resultset_last.png
 Created ROOT/src/main/webapp/images/resultset_next.png
 Created ROOT/src/main/webapp/images/resultset_previous.png
 Created ROOT/src/main/webapp/images/show.png
 Created ROOT/src/main/webapp/images/springsource-logo.png
 Created ROOT/src/main/webapp/images/update.png
 Created ROOT/src/main/webapp/styles
 Created ROOT/src/main/webapp/styles/alt.css
 Created ROOT/src/main/webapp/styles/standard.css
 Created ROOT/src/main/webapp/WEB-INF/classes
 Created ROOT/src/main/webapp/WEB-INF/classes/alt.properties
 Created ROOT/src/main/webapp/WEB-INF/classes/standard.properties
 Created ROOT/src/main/webapp/WEB-INF/layouts
 Created ROOT/src/main/webapp/WEB-INF/layouts/default.jspx
 Created ROOT/src/main/webapp/WEB-INF/layouts/layouts.xml
 Created ROOT/src/main/webapp/WEB-INF/views
 Created ROOT/src/main/webapp/WEB-INF/views/header.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/menu.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/footer.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/views.xml
 Created ROOT/src/main/webapp/WEB-INF/views/dataAccessFailure.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/index-template.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/index.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/resourceNotFound.jspx
 Created ROOT/src/main/webapp/WEB-INF/views/uncaughtException.jspx
 Created ROOT/src/main/webapp/WEB-INF/tags/form
 Created ROOT/src/main/webapp/WEB-INF/tags/form/create.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/dependency.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/find.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/list.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/show.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/update.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/checkbox.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/column.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/datetime.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/display.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/editor.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/input.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/reference.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/select.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/simple.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/table.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/form/fields/textarea.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/menu
 Created ROOT/src/main/webapp/WEB-INF/tags/menu/category.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/menu/item.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/menu/menu.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util
 Created ROOT/src/main/webapp/WEB-INF/tags/util/language.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util/load-scripts.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util/pagination.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util/panel.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util/placeholder.tagx
 Created ROOT/src/main/webapp/WEB-INF/tags/util/theme.tagx
 Created ROOT/src/main/webapp/WEB-INF/i18n
 Created ROOT/src/main/webapp/WEB-INF/i18n/messages.properties
 Created ROOT/src/main/webapp/images/en.png
 Updated ROOT/src/main/webapp/WEB-INF/i18n/application.properties
 Updated ROOT/src/main/webapp/WEB-INF/web.xml
 Updated ROOT/pom.xml [added dependencies org.springframework:spring-webmvc:${spring.version}, org.springframework.webflow:spring-js-resources:2.2.1.RELEASE, commons-digester:commons-digester:2.1, commons-fileupload:commons-fileupload:1.2.2, javax.servlet.jsp.jstl:jstl-api:1.2, org.glassfish.web:jstl-impl:1.2, javax.el:el-api:1.0, joda-time:joda-time:1.6, javax.servlet.jsp:jsp-api:2.1, commons-codec:commons-codec:1.5; updated project type to war; added dependency org.apache.tiles:tiles-jsp:2.2.2]
 Updated SRC_MAIN_WEBAPP/WEB-INF/views/footer.jspx

and next….

roo> web mvc all --package ~.web
 Created SRC_MAIN_JAVA/org/grassfield/roo/web
 Created SRC_MAIN_JAVA/org/grassfield/roo/web/TimerController.java
 Updated SRC_MAIN_WEBAPP/WEB-INF/spring/webmvc-config.xml
 Created SRC_MAIN_JAVA/org/grassfield/roo/web/ApplicationConversionServiceFactoryBean.java
 Created SRC_MAIN_WEBAPP/WEB-INF/views/timers
 Created SRC_MAIN_WEBAPP/WEB-INF/views/timers/views.xml
 Updated SRC_MAIN_WEBAPP/WEB-INF/views/timers/views.xml
 Updated SRC_MAIN_WEBAPP/WEB-INF/i18n/application.properties
 Created SRC_MAIN_JAVA/org/grassfield/roo/web/TimerController_Roo_Controller.aj
 Created SRC_MAIN_WEBAPP/WEB-INF/views/timers/list.jspx
 Created SRC_MAIN_WEBAPP/WEB-INF/views/timers/show.jspx
 Created SRC_MAIN_WEBAPP/WEB-INF/views/timers/create.jspx
 Updated SRC_MAIN_WEBAPP/WEB-INF/views/menu.jspx
 Created SRC_MAIN_WEBAPP/WEB-INF/views/timers/update.jspx
 Created SRC_MAIN_JAVA/org/grassfield/roo/web/ApplicationConversionServiceFactoryBean_Roo_ConversionService.aj

Lets configure the tests!

roo> selenium test --controller ~.web.TimerController
 Updated SRC_MAIN_WEBAPP/WEB-INF/i18n/application.properties
 Created SRC_MAIN_WEBAPP/selenium
 Created SRC_MAIN_WEBAPP/selenium/test-timer.xhtml
 Created SRC_MAIN_WEBAPP/selenium/test-suite.xhtml
 Updated SRC_MAIN_WEBAPP/WEB-INF/views/menu.jspx
 Updated ROOT/pom.xml [added plugin org.codehaus.mojo:selenium-maven-plugin:2.1]
~.Timer

Lets run the tests now

~.web roo> perform tests
[INFO] ------------------------------------------------------------------------
 [INFO] BUILD FAILURE
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time: 3.818s
 [INFO] Finished at: Sat Apr 28 12:40:43 IST 2012
 [INFO] Final Memory: 7M/151M
 [INFO] ------------------------------------------------------------------------
 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.9:test (default-test) on project roo: There are test failures.
 [ERROR] o>
 [ERROR] Please refer to /home/pandian/Downloads/spring-roo-1.2.1.RELEASE/bin/newproj/target/surefire-reports for the individual test results.
 [ERROR] -> [Help 1]
 [ERROR] o>
 [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
 [ERROR] Re-run Maven using the -X switch to enable full debug logging.
 [ERROR] o>
 [ERROR] For more information about the errors and possible solutions, please read the following articles:
 [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Unfortunate, lets see the test log

2012-04-28 12:40:42,920 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - Unsuccessful: create table timer (id bigint not null auto_increment, message varchar(255) not null, version integer, primary key (id)) ENGINE=InnoDB
2012-04-28 12:40:42,920 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - No database selected
2012-04-28 12:40:43,176 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No database selected
2012-04-28 12:40:43,190 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No database selected
2012-04-28 12:40:43,201 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No database selected
2012-04-28 12:40:43,205 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No database selected
2012-04-28 12:40:43,209 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No database selected
2012-04-28 12:40:43,213 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No database selected
2012-04-28 12:40:43,216 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No database selected
2012-04-28 12:40:43,221 [main] ERROR org.hibernate.util.JDBCExceptionReporter - No database selected

We have forgot to give the database name in config files.

I am creating a new Database roodb

mysql> create database roodb;

Hence the db property file is changed as below

$ cat ./src/main/resources/META-INF/spring/database.properties
#Updated at Sat Apr 28 12:35:06 IST 2012
 #Sat Apr 28 12:35:06 IST 2012
 database.driverClassName=com.mysql.jdbc.Driver
 database.url=jdbc\:mysql\://localhost\:3306/roodb
 database.username=root
 database.password=pandian

Let’s run the test again!

~.web roo> perform test
[INFO] ------------------------------------------------------------------------
 [INFO] BUILD SUCCESS
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time: 4.520s
 [INFO] Finished at: Sat Apr 28 12:51:22 IST 2012
 [INFO] Final Memory: 7M/151M
 [INFO] ------------------------------------------------------------------------

Cheers!

lets package it now

~.web roo> perform package
[INFO] Webapp assembled in [177 msecs]
[INFO] Building war: /home/pandian/Downloads/spring-roo-1.2.1.RELEASE/bin/newproj/target/roo-0.1.0.BUILD-SNAPSHOT.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:00.264s
[INFO] Finished at: Sat Apr 28 12:56:13 IST 2012
[INFO] Final Memory: 8M/151M
[INFO] ------------------------------------------------------------------------

I prefer eclipse build

.web roo> perform eclipse
 [INFO] ------------------------------------------------------------------------
 [INFO] BUILD SUCCESS
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time: 8:21.388s
 [INFO] Finished at: Sat Apr 28 13:07:00 IST 2012
 [INFO] Final Memory: 13M/145M
 [INFO] ------------------------------------------------------------------------
~.web roo> quit
 $mvn tomcat:run

Cheers!

Lets see what happened at DB end.

mysql> show tables;
+-----------------+
| Tables_in_roodb |
+-----------------+
| timer           |
+-----------------+
1 row in set (0.00 sec)
mysql> desc timer;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| message | varchar(255) | NO   |     | NULL    |                |
| version | int(11)      | YES  |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> select * from timer;
Empty set (0.00 sec)

Advertisements

One thought on “Roo for rapid Spring development

  1. Pingback: ROO – 2: Creating a simple application: Simple User Record « JavaShine

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s