java - 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 进程永远不会结束并且不会继续下一个阶段(执行单一测试)。
我到目前为止所尝试的:
- 添加到
exec-maven-plugin
选项<async>true</async>
但没有任何变化。 - 将 a 添加
System.exit(0)
到 Main 类,但 maven 被杀死并且不会继续到下一个阶段。 - 按照此处的建议在 bash 中创建运行脚本,进程返回
Async process complete, exit value = 0
,但未生成数据库脚本。也许我可以更深入地研究脚本以找到错误,但这不是我的首选方式。
尽管如此,我还是不明白为什么将 Hibernate 4 更改为 Hibernate 5 会导致该过程无法结束。我已经检查了代码(System.out
到处都是基本的),并且所有行都正确执行到最后,但该过程仍然有效。
有谁知道 Hibernate 5 的行为变化是否会导致这种不良行为?
解决方案
如果我使用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>
这不完全是问题的答案,而是一个很好的解决方法。
推荐阅读
- windows - 如何在 Windows 上使用 Vi?
- asp.net-mvc - 重定向到视图操作过滤器属性
- c# - 循环遍历临时目录中的多个文件,并将文件插入到具有现有行/ID 的 MS SQL 数据库中
- php - 如何删除
查看快速创建默认按钮? - r - 强制绘图小提琴图不在零值上显示小提琴
- java - 无法通过 Spotify Docker 客户端将文件复制到 Docker 容器
- python - 如何仅通过张量操作在张量流图中获取掩码
- ios - 上传存储在云服务上的文件的 0 字节通过在 iOS 上
- jmeter - Jmeter Cross-Platform Path config issue
- azure-cosmosdb - 如何减少 cosmos db 中的 RU 计数成本?