How to repeat a certain execution several times

The following snippet generates sql create / drop for a particular database, whenever there is a modification of JPA entity classes.

How to do something equivalent to the for operation, somewhere in the following code you can use to generate sql for all supported databases (for example, H2, MySQL, Postgres)

Currently, I have to change db.groupId, db.artifactId, db.driver.version every time to generate sql files

            <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>
+3
source share
3 answers

, . , , .

, , .

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

, , h2, ACTIVE_DB "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

:

1) Ant, ant -tools ( XML)

2) Maven (Mojo), Hibernate .

" Maven" .

0

ant, maven-antrun-plugin, java, Maven Exec Plugin : 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( - ) , . java ant ant ( , , , exec mojo java mojo)

0

Source: https://habr.com/ru/post/1717041/


All Articles