2009-09-07 72 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的魔力)

肖恩