简体   繁体   中英

exec-maven-plugin hangs with execute Java main class

For a long time, I have a small application in java that uses hibernate SchemaExport to get all actual database structure in a file. This was working fine with Hibernate 4.X.

Basically I execute in a java Main.class:

hibernateConfiguration.setProperty("hibernate.hbm2ddl.auto", "create");
hibernateConfiguration.setProperty("hibernate.dialect", dialect.getDialectClass());
hibernateConfiguration.setProperty("hibernate.connection.url", "jdbc:mysql://" + host + ":" + port + "/"
SchemaExport export = new SchemaExport(hibernateConfiguration);
export.setDelimiter(";");
export.setOutputFile(outputFile);
export.setFormat(true);
export.execute(false, false, false, true);

And I launch it each time the project is executed using exec-maven-plugin :

<!-- Creates the database script BEFORE testing -->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>com.schemaexporter.main</mainClass>
        <!-- <skip>true</skip> -->
        <arguments>
            [...] <!-- Some database connection parameters -->
        </arguments>
    </configuration>
</plugin>

Now, I have just updated to Hibernate 5 (5.2.17.Final). And for this purpose, I have updated my code to:

MetadataSources metadata = new MetadataSources(new StandardServiceRegistryBuilder().applySetting("hibernate.hbm2ddl.auto", "create")
    .applySetting("hibernate.connection.driver_class", dialect.getDriver())
    .applySetting("hibernate.dialect", dialect.getDialectClass())
    .applySetting("hibernate.connection.driver_class", dialect.getDriver())
    .applySetting("hibernate.connection.url", "jdbc:mysql://" + host + ":" + port + "/" + databaseName)
    .applySetting("hibernate.connection.username", username)
    .applySetting("hibernate.connection.password", password).build());

SchemaExport export = new SchemaExport();
export.setDelimiter(";");
export.setOutputFile(directory + File.separator + outputFile);
export.setFormat(true);
export.execute(EnumSet.of(TargetType.SCRIPT), SchemaExport.Action.CREATE, metadata.buildMetadata());

The database script is created correctly. But the exec-maven-process hangs and not continue to other actions. For hanging, I refer to that the maven process never ends and not continue with next phases (executing unitary tests).

What I have tried until now:

  • Adding to exec-maven-plugin option <async>true</async> but nothing changes.
  • Adding a System.exit(0) to the Main class, but maven is killed and not continues to next phases.
  • Creating a running script in bash, as suggested here and the process returns Async process complete, exit value = 0 but the database script is not generated. Maybe I can go deeper on the script to find the error, but is not my preferred way.

Still, I do not understand why changing Hibernate 4 to Hibernate 5 causes the process not to end. I have checked the code (basic System.out everywhere), and all lines are executed correctly until the end but the process is still live.

Does anybody knows if there is a change of behaviour with Hibernate 5 that causes this undesired behaviour?

If I execute the same class using maven-antrun-plugin seems that maven continues executing.

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <phase>compile</phase>
            <configuration>
                <target>
                    <java failonerror="true" classname="com.schemaexporter.main">
                        <arg value="databaseName" />    
                        <classpath refid="maven.compile.classpath" />
                    </java>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

It is not exactly an answer for the question, but a good workaround.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM