【问题标题】:exec-maven-plugin hangs with execute Java main classexec-maven-plugin 挂起执行 Java 主类
【发布时间】:2018-05-09 09:16:52
【问题描述】:

长期以来,我在 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 进程永远不会结束并且不会继续下一个阶段(执行单一测试)。

到目前为止我所尝试的:

  • 添加到exec-maven-plugin 选项&lt;async&gt;true&lt;/async&gt; 但没有任何变化。
  • System.exit(0) 添加到 Main 类,但 maven 已被杀死并且不会继续到下一个阶段。
  • 在 bash 中创建一个运行脚本,作为suggested here,进程返回Async process complete, exit value = 0,但没有生成数据库脚本。也许我可以更深入地研究脚本以找到错误,但这不是我的首选方式。

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

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

【问题讨论】:

    标签: java hibernate maven


    【解决方案1】:

    如果我使用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>
    

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

    【讨论】:

      猜你喜欢
      • 2012-03-13
      • 2014-04-23
      • 2014-07-04
      • 2011-01-12
      • 1970-01-01
      • 2012-10-25
      • 2012-11-13
      • 1970-01-01
      • 2017-05-06
      相关资源
      最近更新 更多