Domanda

I am trying to use both plugin hibernate3-maven-plugin and sql-maven-plugin. My goals is that I can run "maven generate-sources" and it should do :

1) hibernate3-maven-plugin generate the init.sql

2) sql-maven-plugin execute it (an some other script)

The problem with my configuration is : If I run generate-sources with only hibernate3-maven-plugin it works and generate th init.sql but I try to run both plugin it will run sql-maven-plugin first

and end with an error :

Caused by: org.apache.maven.plugin.MojoExecutionException: /my/path/src/main/resources/sql/init.sql not found.

this is my plugins configuration :

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>hibernate3-maven-plugin</artifactId>
        <version>3.0</version>
        <dependencies>                  
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.13</version>
            </dependency>
        </dependencies>
        <executions>
            <execution>
                <id>create-script</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>hbm2ddl</goal>
                </goals>
                <configuration>
                    <hibernatetool destdir="${project.basedir}/src/main/resources/sql/">
                        <classpath>
                            <path location="${project.basedir}/src/main/java" />
                        </classpath>
                        <configuration
                            configurationfile="${project.basedir}/src/main/resources/hibernate/hibernate-mysql.cfg.xml" />
                        <hbm2ddl create="true" drop="true" export="false"
                            outputfilename="init.sql" format="true" console="true" />
                    </hibernatetool>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sql-maven-plugin</artifactId>
        <version>1.5</version>
        <dependencies>                  
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.13</version>
            </dependency>
        </dependencies>
        <configuration>
            <driver>com.mysql.jdbc.Driver</driver>
            <url>jdbc:mysql://localhost/MYDB</url>
            <username>root</username>
            <password>root</password>
        </configuration>
        <executions>
            <execution>
                <id>init-db</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>execute</goal>
                </goals>
                <configuration>
                    <autocommit>true</autocommit>
                    <srcFiles>
                        <srcFile>src/main/resources/sql/init.sql</srcFile>
                        <srcFile>src/main/resources/sql/insertMessages.sql</srcFile>
                    </srcFiles>
                </configuration>
            </execution>
        </executions>
    </plugin>            
</plugins>

UPDATE

In fact I don't want to run sql during my build. I want just to set up an easy way for other developper to reset their db to the latest DB schema and populate with testData. In the end I should run mvn like this : mvn hibernate3:hbm2ddl sql:execute

to execute both plugin

I tryed to remove the <phase/> in both executions but there is also an error :

ERROR] Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:3.0:hbm2ddl (default-cli) on project DideuroDb: There was an error creating the AntRun task. NullPointerException -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:3.0:hbm2ddl (default-cli) on project myProject: There was an error creating the AntRun task.

update 2

Configuration like this works a little better :

it generate the init.sql but doen't execute anything :

[INFO] 0 of 0 SQL statements executed successfully
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

configuration updated :

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>hibernate3-maven-plugin</artifactId>
        <version>3.0</version>
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.13</version>
            </dependency>
        </dependencies>     
        <configuration>
            <hibernatetool destdir="${project.basedir}/src/main/resources/sql/">
                <classpath>
                    <path location="${project.basedir}/src/main/java" />
                </classpath>
                <configuration
                    configurationfile="${project.basedir}/src/main/resources/hibernate/hibernate-mysql.cfg.xml" />
                <hbm2ddl create="true" drop="true" export="false"
                    outputfilename="init.sql" format="true" console="true" />

            </hibernatetool>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sql-maven-plugin</artifactId>
        <version>1.5</version>
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.13</version>
            </dependency>
        </dependencies>
        <configuration>
            <driver>com.mysql.jdbc.Driver</driver>
            <url>jdbc:mysql://localhost/MYDB</url>
            <username>root</username>
            <password>root</password>
        </configuration>
        <executions>
            <execution>
                <id>init-db</id>
                <goals>
                    <goal>execute</goal>
                </goals>
                <configuration>
                    <autocommit>true</autocommit>
                    <srcFiles>
                        <srcFile>src/main/resources/sql/init.sql</srcFile>
                        <srcFile>src/main/resources/sql/insertMessages.sql</srcFile>
                    </srcFiles>
                </configuration>
            </execution>
        </executions>
    </plugin>       
</plugins>
È stato utile?

Soluzione

Since you don't want to run your executions as part of your regular build, but as a convenience to your developers, use the default-cli "magic" execution id (see https://maven.apache.org/guides/mini/guide-default-execution-ids.html). So don't include neither phase nor goal in your execution and name it "default-cli". That way, it's configuration is only used during manual call of mvn hibernate3:hbm2ddl sql:execute:

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>hibernate3-maven-plugin</artifactId>
        <version>3.0</version>
        <dependencies>
          ...
        </dependencies>
        <executions>
          <execution>
            <id>default-cli</id>
            <configuration>
              ...
            </configuration>
          <execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sql-maven-plugin</artifactId>
        <version>1.5</version>
        <dependencies>
          ...
        </dependencies>
        <configuration>
            <driver>com.mysql.jdbc.Driver</driver>
            <url>jdbc:mysql://localhost/MYDB</url>
            <username>root</username>
            <password>root</password>
        </configuration>
        <executions>
            <execution>
                <id>default-cli</id>
                <configuration>
                    <autocommit>true</autocommit>
                    <srcFiles>
                        <srcFile>src/main/resources/sql/init.sql</srcFile>
                        <srcFile>src/main/resources/sql/insertMessages.sql</srcFile>
                    </srcFiles>
                </configuration>
            </execution>
        </executions>
    </plugin>       
</plugins>

That way your "manual" goal's configuration will never interfere with some normal lifecycle configuration.

Just two more points:

  • Consider always creating the init.sql as part of your build and attaching them as attached artifact (using the classifier sql) to your project. It will make it easier to retrieve the correct sql to a revision later
  • please don't generate your sql into src/main/resources. This could lead to unmaching classes and sqls as well as "phantom changes" in your developers' workspaces. Correct place to generate to is somewhere under ${project.build.directory}, usually target/generated-resources/hibernate3.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top