簡體   English   中英

exec-maven-plugin因執行Java主類而掛起

[英]exec-maven-plugin hangs with execute Java main class

長期以來,我在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>但沒有任何變化。
  • 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>

這並非完全是問題的答案,而是一個不錯的解決方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM