【问题标题】:How to repeat a particular execution multiple times如何多次重复特定的执行
【发布时间】:2010-11-26 03:36:00
【问题描述】:

每当对 JPA 实体类进行修改时,以下 sn -p 都会为特定数据库生成创建/删除 sql。

如何执行与“for”操作等效的操作,其中以下代码可用于为所有支持的数据库(例如 H2、MySQL、Postgres)生成 sql

目前每次生成sql文件都要修改db.groupId、db.artifactId、db.driver.version

            <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>

【问题讨论】:

    标签: hibernate maven-2


    【解决方案1】:

    我会选择使用 maven-antrun-plugin 的集成 ant 解决方案或使用 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 类 com.yourcompany.DocBuilder(或其他),其中包含一个将属性文件数组作为参数的 main 方法。您的 java 类可以嵌入 ant 或将 ant 作为外部进程运行(如果这样做,您可能希望使用 exec mojo 而不是 java mojo)

    肖恩

    【讨论】:

      【解决方案2】:

      一些可能的方向是:

      1) 调用一个 Ant 任务,该任务可以使用 ant-tools 来实现循环(虽然讨厌和 XML 密集型)

      2) 编写您自己的 Maven 插件 (Mojo),它将 Hibernate 插件调用包装在一个循环中并接受一些参数。

      查看Better Builds With Maven eBook 了解更多详情。

      【讨论】:

        【解决方案3】:

        我假设您一次只有一个数据库处于活动状态。如果这是一个有效的假设,那么您实际上并不需要循环,而是需要能够在数据库之间切换。

        您可以通过在配置文件中声明特定于数据库的属性来实现此目的,然后激活相关配置文件。

        下面的配置显示了如何为您提到的 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>
        

        【讨论】:

        • Rich - 我确实一次有一个数据库处于活动状态,我使用配置文件在它们之间切换。我不使用 hbm2ddl.auto 功能动态生成我的 SQL,但我使用 hbm2ddl 插件为所有支持的数据库生成脚本并在启动之前预加载它们。因此,我需要能够在单次运行中为所有受支持的数据库生成所有创建/删除 SQL。希望它澄清。
        • 好的,我看到了问题,我没有配置来测试这个。检查插件代码表明没有内置机制来执行此操作
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-06
        • 2021-09-19
        相关资源
        最近更新 更多