Spring+HibernateEntityManager(archetype編)

前回のおさらい

前回は、component-scan を用いつつ、ドメインモデル実装中の SpringFramework に依存したアノテーションを排除しました。

今回のテーマ

今回は、Spring+HibernateEntityManager 環境のサンプルプロジェクトの生成を、mavenmaven-archetype-plugin を用いて行います。

サンプルの内容について

基本的に、前回 までの内容に沿った内容になっています。
前回から1年3ヶ月も経っているので、微妙に軌道修正しているところはありますが (^^;

archetypeからのmavenプロジェクト作成方法

settings.xmlの設定

以下の内容を追加します。

<?xml version="1.0" encoding="UTF-8"?>

<!--
  References:
   - http://maven.apache.org/maven-settings/settings.html
   - http://maven.apache.org/settings.html
-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <profiles>
    <profile>
      <id>objectfanatics</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <repositories>
        <repository>
          <id>objectfanatics</id>
          <url>http://domaingen.sourceforge.jp/maven2</url>
          <layout>default</layout>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>objectfanatics</id>
          <url>http://domaingen.sourceforge.jp/maven2</url>
          <layout>default</layout>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>

</settings>
プロジェクト生成

archetypeを指定して、プロジェクトを生成します。

mvn org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate -DarchetypeGroupId=jp.objectfanatics.archetype.ddd -DarchetypeArtifactId=ddd-archetype -DarchetypeVersion=0.0.2 -DarchetypeRepository=http://domaingen.sourceforge.jp/maven2/

いくつか質問されるので、自分の好みの情報を入力します。

Define value for groupId: : com.example
Define value for artifactId: : test-artifact
Define value for version:  1.0-SNAPSHOT: : 0.0.1-SNAPSHOT
Define value for package:  com.example: :
Confirm properties configuration:
groupId: com.example
artifactId: test-artifact
version: 0.0.1-SNAPSHOT
package: com.example
 Y: :

DB環境設定

DBに関する環境設定は、以下の2箇所で行っていますので、必要に応じて変更してください。

domain層のテスト用
domain/domain-impl/src/test/resources/beans.xml
application層用
application/src/main/resources/beans.xml

両方とも、デフォルトのDB設定は以下のようになっています。そのため MySQL5.0 系をデフォルトでインストールしてそのまま起動すれば、DB環境設定は完了です。

<!-- raw datasource -->
<bean id="rawDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://localhost:3306/test" />
  <property name="username" value="root" />
  <property name="password" value="" />
  <property name="validationQuery" value="SELECT 1" />
</bean>

テストの実行

DBアクセスを含むいくつかのテストがサンプルに含まれていますので、以下のようにしてテストを実行し、環境が正しく構築されているか確認します。

mvn clean test