首页 > 解决方案 > exec-maven-plugin 挂起执行 Java 主类

问题描述

很长一段时间以来,我在 java 中有一个小应用程序,它使用 hibernate SchemaExport 来获取文件中的所有实际数据库结构。这在 Hibernate 4.X 中运行良好。

基本上我在 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);

每次执行项目时,我都会使用以下命令启动它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>

现在,我刚刚更新到 Hibernate 5 (5.2.17.Final)。为此,我将代码更新为:

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());

数据库脚本已正确创建。但exec-maven-process挂起并没有继续其他动作。对于挂起,我指的是 maven 进程永远不会结束并且不会继续下一个阶段(执行单一测试)。

我到目前为止所尝试的:

尽管如此,我还是不明白为什么将 Hibernate 4 更改为 Hibernate 5 会导致该过程无法结束。我已经检查了代码(System.out到处都是基本的),并且所有行都正确执行到最后,但该过程仍然有效。

有谁知道 Hibernate 5 的行为变化是否会导致这种不良行为?

标签: javahibernatemaven

解决方案


如果我使用maven-antrun-plugin似乎执行相同的类,maven 会继续执行。

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

这不完全是问题的答案,而是一个很好的解决方法。


推荐阅读