2009-09-07 66 views
3

下面的代碼片段爲特定數據庫生成創建/刪除sql,只要修改了JPA實體類即可。如何重複執行多次特定的執行

如何執行的東西相當於一個「的」操作,其中,在下面的代碼可用於生成所有支持的數據庫(如H2,MySQL和Postgres的)

目前我有修改數據庫的SQL .groupId,db.artifactId,db.driver.version每次生成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

我假設你只有一個數據庫同時啓動。如果這是一個有效的假設,那麼您並不需要循環,而是需要能夠在數據庫之間切換。

您可以通過在配置文件中聲明特定於數據庫的屬性來實現此目的,然後激活相關配置文件。

下面的配置顯示瞭如何爲您提到的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

瑞奇 - 我有一個數據庫一次激活,我使用配置文件在它們之間切換。我不使用hbm2ddl.auto功能來動態生成我的SQL,但我使用hbm2ddl插件爲所有支持的數據庫生成腳本,並在啓動之前預加載它們。因此,我需要能夠在單次運行中爲所有支持的數據庫生成所有創建/刪除SQL。希望澄清。 – Joe 2009-09-07 14:30:00

+0

好吧,我看到了問題,我還沒有配置來測試它。檢查插件代碼表明沒有內置機制來執行此操作 – 2009-09-07 21:42:19

0

一些可能的方向採取有:

1)調出一個Ant任務,可以使用ant-工具來實現循環(骯髒和XML密集雖然)

2)寫你自己的Maven插件(Mojo),該插件在循環中包裝Hibernate插件,並接受一些參數。

查看Better Builds With Maven eBook瞭解更多詳情。

0

我會去使用maven-antrun-插件或使用的Maven Exec插件這裏定製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類融爲一體。 yourcompany.DocBuilder(或其他)與一個主要方法,以屬性文件的數組作爲參數。 Java類可以嵌入螞蟻或螞蟻運行作爲一個外部進程(如果你這樣做,你可能會想使用exec的魔力,而不是Java的魔力)

肖恩