Category: ‘Roo’

Spring Roo Macへのインストール

2012年10月15日 Posted by PURGE

Mac環境へ、Spring Roo をダウンロードして解凍し、インストールした場合の設定。
/usr/local/roo/bin 配下に、roo.sh と roo.bat が存在する為、パスを通してシンボリックリンクを作成して、パスを通す。

$ ln -s /usr/local/roo/bin/roo.sh roo
$ vi .bashrc 
export PATH=$PATH:/usr/local/roo/bin

rooコマンドにて起動する。

$ roo
    ____  ____  ____  
   / __ \/ __ \/ __ \ 
  / /_/ / / / / / / / 
 / _, _/ /_/ / /_/ /  
/_/ |_|\____/\____/    1.2.2.RELEASE [rev 7d75659]


Welcome to Spring Roo. For assistance press TAB or type "hint" then hit ENTER.
@rooinaction: @SpringRoo Second blog post on testing Roo Add-ons is here - Mockito and the Argument Captor. http://t.co/S4wfLyAI 
Version is required for org.apache.maven.plugins:maven-compiler-plugin

SpringSource Tool Suite と Maven の関係

2012年10月8日 Posted by PURGE

そもそも Maven を使用すること自体経験上無く、今更インストールして使用することとなった訳だが、具体的に何をしてくれるのかが不明。

しかし、SpringSource Tool Suite で、プロジェクトを新規作成してみると、プロジェクトのビルドパスに、いつの間にやら必要なjarファイル群が設定されている。
そもそも、これらのjarファイル群はどこから来たのか?

調べてみると、WindowsXP の場合、C:\Documents and Settings\xxxxxx\.m2\repository の中に、ダウンロードされている。
これ自体がMaven の設定で、Mavenレポジトリとして、デフォルトとしてダウンロードされているようだ。

これらは、プロジェクト内にあるpom.xml に必須ライブラリとして記載されているようだ。

知らないと面倒だが、少しづつ勉強中である。

Spring Rooを触ってみる

2012年10月6日 Posted by PURGE

Blogのエントリには記載していないが、JavaのFrameworkとしては、Seasar2を使用してきた。
今回どういうわけか、SpringFrameworkに接する機会があったので、現在勉強中なのであるが、勢い余って Spring Roo に挑戦してみようと思った。
そもそも、SpringFramework に関しては約10年前程に知識不足で脱落していた。
それ以来、興味も何もなかったが、Seasar2 に慣れて、概念的な部分の理解はクリアしているつもりなので、今回は、SpringFramework 自体も、10年前よりもスンナリと頭で想像できた。つもり。

大方は、SpringRoo のサイト(英語)に記載してある通りだが。
準備としては、Maven と Roo をダウンロード&インストールして、システムのパスに含めてさぁ開始。

C:\works\hello>roo
    ____  ____  ____
   / __ \/ __ \/ __ \
  / /_/ / / / / / / /
 / _, _/ /_/ / /_/ /
/_/ |_|\____/\____/    1.2.2.RELEASE [rev 7d75659]

Welcome to Spring Roo. For assistance press TAB or type "hint" then hit ENTER.
@krimple: Just submitted my talk proposal for SpringOne/2GX on Roo add-on development. Keeping finge
rs crossed? @SpringRoo

At this time you have not authorized Spring Roo to download an index of
available add-ons. This will reduce Spring Roo features available to you.
Please type 'download status' and press ENTER for further information.

roo>

お手本通りに、hint コマンド。

roo> hint
Welcome to Roo! We hope you enjoy your stay!

Before you can use many features of Roo, you need to start a new project.

To do this, type 'project' (without the quotes) and then hit TAB.

Enter a --topLevelPackage like 'com.mycompany.projectname' (no quotes).
When you've finished completing your --topLevelPackage, press ENTER.
Your new project will then be created in the current working directory.

Note that Roo frequently allows the use of TAB, so press TAB regularly.
Once your project is created, type 'hint' and ENTER for the next suggestion.
You're also welcome to visit http://forum.springframework.org for Roo help.

トップレベルのパッケージを打ってくれとのことなので。とりあえず、sampleパッケージ。

roo> project --topLevelPackage com.whoocus.sample
Created ROOT\pom.xml
Created SRC_MAIN_RESOURCES
Created SRC_MAIN_RESOURCES\log4j.properties
Created SPRING_CONFIG_ROOT
Created SPRING_CONFIG_ROOT\applicationContext.xml

わお!!雛形が作成されている。

次は、DBの設定。正直、jpa とか hibernate に関しては無知なので、後に調べるとして、お手本通りにコマンド打ってみる。
ちなみに、DBは、MySql。”MYSQL” と大文字で打ち込まないと、エラーになった。

roo> jpa setup --provider HIBERNATE --database MYSQL --hostname localhost --databaseName roo_db -
-userName hoge --password hoge
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, commons-collections:commons-collections:3.2.1, org.hibernate:hib
ernate-validator:4.2.0.Final, javax.validation:validation-api:1.0.0.GA, cglib:cglib-nodep:2.2.2, jav
ax.transaction:jta:1.1, org.springframework:spring-jdbc:${spring.version}, org.springframework:sprin
g-orm:${spring.version}, commons-pool:commons-pool:1.5.6, commons-dbcp:commons-dbcp:1.3]

わおっ!!設定ファイルが生成された。pom.xml も更新されるようだ。正直、pom.xml は過去にも良く見かけたことがあったのだが、何者であるか知らなかった。Maven関連のファイルらしいと気付く。Mavenも、名前だけで何だか知らなかったのだけどね。
これも、これから勉強予定。

そして、今度はエンティティクラス生成かな?
Memberクラスを打ち込む。
が、しかし生成されず、Userクラスはどうか?
やはり駄目。

roo> entity jpa --class model.Member --testAutomatically
Reserved SQL keyword 'Member' is not permitted as simple type name
model.Member roo> field string --fieldName lastName --notNull
The type specified, 'model.Member'doesn't exist

model.Member roo> entity jpa --class ~.User --testAutomatically
Reserved SQL keyword 'User' is not permitted as simple type name

お手本通り、Timerクラスで打ち込むと成功。でも、そんなエンティティはいらない。
調べてみると、オプション(–permitReservedWords)を付加すると、作成されるらしい。

どうも簡単な単語は、はじかれてしまうようだ。Order とか。ちょっと腑に落ちない。

~.Member roo> entity jpa --class ~.Member --testAutomatically --permitReservedWords
Created SRC_MAIN_JAVA\com\whoocus\sample\Member.java
Created SRC_TEST_JAVA\com\whoocus\sample\MemberDataOnDemand.java
Created SRC_TEST_JAVA\com\whoocus\sample\MemberIntegrationTest.java
Created SRC_MAIN_JAVA\com\whoocus\sample\Member_Roo_Configurable.aj
Created SRC_MAIN_JAVA\com\whoocus\sample\Member_Roo_Jpa_Entity.aj
Created SRC_MAIN_JAVA\com\whoocus\sample\Member_Roo_Jpa_ActiveRecord.aj
Created SRC_MAIN_JAVA\com\whoocus\sample\Member_Roo_ToString.aj
Created SRC_TEST_JAVA\com\whoocus\sample\MemberDataOnDemand_Roo_Configurable.aj
Created SRC_TEST_JAVA\com\whoocus\sample\MemberDataOnDemand_Roo_DataOnDemand.aj
Created SRC_TEST_JAVA\com\whoocus\sample\MemberIntegrationTest_Roo_Configurable.aj
Created SRC_TEST_JAVA\com\whoocus\sample\MemberIntegrationTest_Roo_IntegrationTest.aj

成功!!
ソースファイルフォルダを見てみると、ソースファイル出来てます。感動です。
まるで、Ruby on Rails のようです。まあ、Rails っぽかったから、SpringRoo に挑戦している訳ですが…。

そして、Memberクラスのフィールド定義。

~.Member roo> field string --fieldName lastName --notNull
Updated SRC_MAIN_JAVA\com\whoocus\sample\Member.java
Updated SRC_TEST_JAVA\com\whoocus\sample\MemberDataOnDemand_Roo_DataOnDemand.aj
Created SRC_MAIN_JAVA\com\whoocus\sample\Member_Roo_JavaBean.aj
~.Member roo> field string --fieldName firstName --notNull
Updated SRC_MAIN_JAVA\com\whoocus\sample\Member.java
Updated SRC_TEST_JAVA\com\whoocus\sample\MemberDataOnDemand_Roo_DataOnDemand.aj
Updated SRC_MAIN_JAVA\com\whoocus\sample\Member_Roo_JavaBean.aj
~.Member roo> field int --fieldName age --notNull

わおっわおっ。Javaのソースコードも更新されている。ナイス。

package com.whoocus.sample;

import javax.validation.constraints.NotNull;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Member {

    @NotNull
    private String lastName;

    @NotNull
    private String firstName;
}

調子に乗って、int型のフィールドを定義しようとしたが、エラー。
Integer型ならどうだっ? エラー。

~.Member roo> field int --fieldName age --notNull
Command 'field int --fieldName age --notNull' not found (for assistance press TAB or type "hint" then hit ENTER)

At this time you have not authorized Spring Roo to download resources from
VMware domains. Some Spring Roo features are therefore unavailable. Please
type 'download status' and press ENTER for further information.

Spring Roo automatic add-on discovery service currently unavailable
~.Member roo> field integer --fieldName age --notNull
Command 'field integer --fieldName age --notNull' not found (for assistance press TAB or type "hint" then hit ENTER)

どうやら、int型の指定方法は下記のようだ。

~.Member roo> field number --fieldName age --type int

おやつの時間なので、とりあえずここまで。
また次回。