2009-09-07 14 views
3

次のスニペットは、JPAエンティティクラスの変更があるたびに特定のデータベースのcreate/drop sqlを生成します。特定の実行を複数回繰り返す方法

どのように私は次のコードをどこ-での動作はサポートされているすべてのデータベースに対してSQLを生成するために使用することができます「の」は何に相当を行ってください(例えばH2、MySQLの、Postgresの)

現在、私は、DBを変更する必要があります.groupId、db.artifactId、SQLファイルに

  <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>hibernate3-maven-plugin</artifactId> 
       <version>${hibernate3-maven-plugin.version}</version> 

       <executions> 
        <execution> 
         <id>create schema</id> 
         <phase>process-test-resources</phase> 
         <goals> 
          <goal>hbm2ddl</goal> 
         </goals> 
         <configuration> 
          <componentProperties> 
           <persistenceunit>${app.module}</persistenceunit> 
           <drop>false</drop> 
           <create>true</create> 
           <outputfilename>${app.sql}-create.sql</outputfilename> 
          </componentProperties> 
         </configuration> 
        </execution> 
        <execution> 
         <id>drop schema</id> 
         <phase>process-test-resources</phase> 
         <goals> 
          <goal>hbm2ddl</goal> 
         </goals> 
         <configuration> 
          <componentProperties> 
           <persistenceunit>${app.module}</persistenceunit> 
           <drop>true</drop> 
           <create>false</create> 
           <outputfilename>${app.sql}-drop.sql</outputfilename> 
          </componentProperties> 
         </configuration> 
        </execution> 
       </executions> 

       <dependencies> 
        <dependency> 
         <groupId>org.hibernate</groupId> 
         <artifactId>hibernate-core</artifactId> 
         <version>${hibernate-core.version}</version> 
        </dependency> 

        <dependency> 
         <groupId>org.slf4j</groupId> 
         <artifactId>slf4j-api</artifactId> 
         <version>${slf4j-api.version}</version> 
        </dependency> 

        <dependency> 
         <groupId>org.slf4j</groupId> 
         <artifactId>slf4j-nop</artifactId> 
         <version>${slf4j-nop.version}</version> 
        </dependency> 

        <dependency> 
         <groupId>${db.groupId}</groupId> 
         <artifactId>${db.artifactId}</artifactId> 
         <version>${db.driver.version}</version> 
        </dependency> 
       </dependencies> 

       <configuration> 
        <components> 
         <component> 
          <name>hbm2cfgxml</name> 
          <implementation>annotationconfiguration</implementation> 
         </component> 
         <component> 
          <name>hbm2dao</name> 
          <implementation>annotationconfiguration</implementation> 
         </component> 
         <component> 
          <name>hbm2ddl</name> 
          <implementation>jpaconfiguration</implementation> 
          <outputDirectory>src/main/sql</outputDirectory> 
         </component> 
         <component> 
          <name>hbm2doc</name> 
          <implementation>annotationconfiguration</implementation> 
         </component> 
         <component> 
          <name>hbm2hbmxml</name> 
          <implementation>annotationconfiguration</implementation> 
         </component> 
         <component> 
          <name>hbm2java</name> 
          <implementation>annotationconfiguration</implementation> 
         </component> 
         <component> 
          <name>hbm2template</name> 
          <implementation>annotationconfiguration</implementation> 
         </component> 
        </components> 
       </configuration> 
      </plugin> 

答えて

0

を生成するdb.driver.version毎回私はあなたが一度にアクティブな1つのデータベースを持っていると仮定します。これが正当な仮定であれば、実際にはループを必要とせず、代わりにデータベースを切り替える必要があります。

これは、プロファイルでDB固有のプロパティを宣言し、関連するプロファイルをアクティブにすることで実現できます。

以下の設定では、言及した3つのデータベースのプロファイルを設定する方法を示し、指定がない場合はデフォルトを使用します。デフォルトを省略すると、プロファイルが宣言されていない限りビルドが失敗します。

次のようにコマンドラインから各プロファイルをアクティブにしたい:

mvn test -P h2 
mvn test -P mysql 
mvn test -P postgresql 

<profiles> 
    <profile> 
    <id>h2</id> 
    <properties> 
     <db.groupId>com.h2database</db.groupId> 
     <db.artifactId>h2</db.artifactId> 
     <db.version>1.1.117</db.version> 
    </properties> 
    </profile> 
    <profile> 
    <id>mysql</id> 
    <properties> 
     <db.groupId>mysql</db.groupId> 
     <db.artifactId>mysql-connector-java</db.artifactId> 
     <db.version>5.1.6</db.version> 
    </properties> 
    </profile> 
    <profile> 
    <id>postgresql</id> 
    <properties> 
     <db.groupId>postgresql</db.groupId> 
     <db.artifactId>postgresql</db.artifactId> 
     <db.version>8.3-603.jdbc4</db.version> 
    </properties> 
    </profile> 
</profiles> 
... 
<!--default database, say mysql--> 
<properties> 
    <db.groupId>mysql</db.groupId> 
    <db.artifactId>mysql-connector-java</db.artifactId> 
    <db.version>5.1.6</db.version> 
</properties> 

あなたはACTIVE_DB場合、たとえば、以下の設定がh2のプロファイルを活性化し、環境変数の値に基づいてプロファイルをアクティブにすることができます環境変数が "h2"に設定されています。

<profile> 
    <id>h2</id> 
    <activation> 
     <property> 
     <name>ACTIVE_DB</name> 
     <value>h2</value> 
     </property> 
    </activation> 
    <properties> 
     <db.groupId>com.h2database</db.groupId> 
     <db.artifactId>h2</db.artifactId> 
     <db.version>1.1.117</db.version> 
    </properties> 
    </profile> 
+0

リッチ(あなたがそれを行う場合、あなたはおそらくexecのモジョの代わりに、Javaのモジョを使用したいと思う)アリ埋め込んだり、外部プロセスとしてantを実行することができます一度にアクティブにすると、私はそれらの間で切り替えるためにプロファイルを使用します。 hbm2ddl.autoを使用してSQLを動的に生成するのではなく、hbm2ddlプラグインを使用して、サポートされているすべてのデータベースのスクリプトを生成し、起動前にあらかじめロードしています。したがって、サポートされているすべてのデータベースのすべての作成/削除SQLを単一実行で生成する機能が必要になります。それが明確になることを願っています。 – Joe

+0

これは問題を抱えていますが、これをテストする構成はありません。プラグインコードを調べると、これを行う組み込みのメカニズムがないことを示します –

0

取るためにいくつかの可能な方向は以下のとおりです。

1)けれども(意地の悪いとXML集中的なループを実装するためにアリのツールを使用することができAntタスク)

2を呼び出す)あなたを書きますループ内でHibernateプラグイン呼び出しをラップし、いくつかのパラメータを取る独自のMavenプラグイン(Mojo)です。

詳細はBetter Builds With Maven eBookをご覧ください。

0

私はここでのMaven Execのプラグインを使用してのmaven-antrun - プラグインやカスタムJavaクラスを使用して統合されたアリ溶液のいずれかとなるだろう:http://mojo.codehaus.org/exec-maven-plugin/java-mojo.html

<build> 
<plugins> 
    <plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>exec-maven-plugin</artifactId> 
    <executions> 
     <execution> 
     <goals> 
      <goal>java</goal> 
     </goals> 
     </execution> 
    </executions> 
    <configuration> 
     <mainClass>com.yourcompany.DocBuilder</mainClass> 
     <arguments> 
     <argument>propertyFile1.properties</argument> 
     <argument>propertyFile2.properties</argument> 
     <argument>propertyFile3.properties</argument> 
     <argument>propertyFile4.properties</argument> 
     </arguments> 
    </configuration> 
    </plugin> 
</plugins> 

次にJavaクラスCOMを書きます。 yourcompany.DocBuilder(または何でも)は、プロパティファイルの配列をパラメータとして取るmainメソッドを持ちます。私は1つのデータベースを持っている - Javaクラスは

ショーン