Maven Embedder - 以编程方式运行hibernate3:hbm2java

时间:2016-02-28 15:57:03

标签: java hibernate maven maven-embedder

**更新**

我试图重现错误并尝试创建一个Minimal,Complete和Verifiable示例。不幸的是,即使从头开始重新启动技术,我甚至无法自己重现错误(我的)...

当我手动执行hibernate3:hbm2java命令时,应用程序正常工作。

但是,它似乎没有以编程方式工作。

运行应用程序时,会显示一个巨大的错误日志。这里排在前几行:

at ClassRealm[plexus.core, parent: null] (via modules: org.eclipse.sisu.wire.WireModule -> org.eclipse.sisu.plexus.PlexusBindingModule)
  while locating org.apache.maven.project.ProjectBuilder
  while locating org.apache.maven.DefaultMaven

12 errors

我已将配置推送到此回购。它需要一个(在mem中)MySQL数据库,pw:“password”才能工作。

https://github.com/Weirdfishees/reverse-engineering.git

这是我的配置:

POM:

<build>    
<plugins>      
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>      

   <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>hibernate3-maven-plugin</artifactId>
        <version>2.2</version>            
        <configuration>
            <components>
                <component>
                    <name>hbm2java</name>
                    <outputDirectory>src/main/java</outputDirectory>
                    <implementation>jdbcconfiguration</implementation>
                </component>
            </components>
            <componentProperties>
                <revengfile>/src/main/resources/META-INF/reveng.xml</revengfile>
                <configurationfile>/src/main/resources/META-INF/hibernate.cfg.xml</configurationfile>
                <jdk5>true</jdk5>
                <ejb3>true</ejb3>
            </componentProperties>
        </configuration>
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.0.8</version>
            </dependency>
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib-nodep</artifactId>
                <version>2.1_3</version>
            </dependency>
        </dependencies>                                
    </plugin>
</plugins>

<dependencies>

<!-- ... -->

<!-- this is not needed for the example, but after executing hibernate3:hbm2java, no compile errors will show -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.1.0.Final</version>      
</dependency>

<!--  to prevent error SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.18</version>
</dependency>

<!-- ... -->

  <dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-embedder</artifactId>
    <version>3.3.3</version>
</dependency>
<dependency>
    <groupId>org.eclipse.aether</groupId>
    <artifactId>aether-connector-wagon</artifactId>
    <version>0.9.0.M2</version>
</dependency>
<dependency>
    <groupId>org.apache.maven.wagon</groupId>
    <artifactId>wagon-http-lightweight</artifactId>
    <version>2.5</version>
</dependency>

的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sakila?autoreconnect=true&amp;usessl=false?autoreconnect=true&amp;usessl=false</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>

reveng.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE hibernate-reverse-engineering SYSTEM "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<schema-selection match-schema="sakila"/>
<table-filter match-name=".*" package="nl.sander.m.localhost.sakila"/>

和application.java

package reverseengineer;

import java.io.File;

import org.apache.maven.cli.MavenCli;

public class Application {
    public static void main(String[] args) {
    System.setProperty("maven.multiModuleProjectDirectory", new     File(".").getAbsolutePath());
    MavenCli cli = new MavenCli();
    cli.doMain(new String[]{"hibernate3:hbm2java"}, ".", System.out, System.out);
}
}

我的问题保持不变。

如何以编程方式运行hibernate3:hbm2java?

- 不可重复 - 但附加信息 -

简介

熟悉stackoverflow用户,我对Maven Embedder的用法有疑问。

在我归结为实际的代码部分之前,我想与您分享我的问题的背景。

我正在构建一个接收用户输入的GUI应用程序(javaFX)。基于此输入,应用程序动态生成两个hibernate配置文件(hibernate.cfg.xml和reveng.xml)。基于这些配置文件(提供连接详细信息),应用程序的以下所需功能是自动(无需用户交互)对数据库进行反向工程并创建所有表的POJO类(自下而上)发展)。

当我手动执行mvn hibernate3:hbm2java参数时(使用命令行或者只是在eclipse中运行 - &gt; maven build .. - &gt; goals mvn hibernate3:hbm2java)所需的包含我想要的所有类被创造了。

但是,目标是以编程方式执行mvn hibernate3:hbm2java而不是手动执行。

研究

我尝试了以下选项:

Running Maven from Java code in Windows?

private void executeHbmToJava() {       
    try {
        Runtime.getRuntime().exec("cmd \\c hibernate3:hbm2java");
    } catch (IOException e) {           
        e.printStackTrace();
    }           
}

这个选项不会抛出任何错误,但程序中似乎没有发生任何事情(在eclipse中刷新视图后,所需的类不在那里......)

即使这个选项有效,我也更喜欢使用Maven Embedder,因为应用程序的上下文(应用程序将跨平台使用)。

此外,我更喜欢来使用user3254289的回答,建议使用Maven Invoker

问题

根据MariuszS在问题How to run maven from java?

上给出的答案

我按如下方式配置了我的应用程序:

pom依赖项:

<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-embedder</artifactId>
    <version>3.1.1</version>
</dependency>
<dependency>
    <groupId>org.eclipse.aether</groupId>
    <artifactId>aether-connector-wagon</artifactId>
    <version>0.9.0.M2</version>
</dependency>
<dependency>
    <groupId>org.apache.maven.wagon</groupId>
    <artifactId>wagon-http-lightweight</artifactId>
    <version>2.5</version>
</dependency>

使用以下(调整后)方法取自https://github.com/mariuszs/maven-cli-example

private void executeHbmToJava() {           
    System.out.println(new File(".").getAbsolutePath());
    MavenCli cli = new MavenCli();
    cli.doMain(new String[]{"clean", "install"}, ".", System.out, System.out);

    // desired is the following
    // cli.doMain(new String[]{"hibernate3:hbm2java"}, ".", System.out, System.out);
}

出现以下错误:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

Caused by: java.lang.NoSuchMethodError: org.codehaus.plexus.DefaultPlexusContainer.<init>(Lorg/codehaus/plexus/ContainerConfiguration;[Lcom/google/inject/Module;)V

基于Maven 3.0.4 NoSuchMethod: ... java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.copyOf(..)

我对MAVEN_HOME变量进行了一些检查。因为我可以在cmd和eclipse中毫无困难地执行mvn命令,我认为这些设置是正确的(如果我错了,请纠正我)。

Maven

$ mvn -v
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04- 22T13:57:37+02:00)
Maven home: C:\Program Files\Apache Software Foundation\apache-maven-3.3.3\bin\..
Java version: 1.8.0_45, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_45\jre
Default locale: nl_NL, platform encoding: Cp1252
OS name: "windows 8.1", version: "6.3", arch: "amd64", family: "dos"

爪哇

$ java -version
java version "1.8.0_71"
Java(TM) SE Runtime Environment (build 1.8.0_71-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.71-b15, mixed mode)

有谁知道如何克服这个错误并达到我想要的目标,从java运行时自动激活mvn hibernate3:hbm2java?

TL; DR

如何在使用Maven Embedder时修复java.lang.NoSuchMethodError?

Caused by: java.lang.NoSuchMethodError:     org.codehaus.plexus.DefaultPlexusContainer.<init>    (Lorg/codehaus/plexus/ContainerConfiguration;[Lcom/google/inject/Module;)V

假设:

private void executeHbmToJava() {           
    System.out.println(new File(".").getAbsolutePath());
    MavenCli cli = new MavenCli();
    cli.doMain(new String[]{"clean", "install"}, ".", System.out, System.out);

    // desired is the following
    // cli.doMain(new String[]{"hibernate3:hbm2java"}, ".", System.out, System.out);
}

1 个答案:

答案 0 :(得分:0)

我成功解决了这个问题。感谢Tunaki提供最小,完整和可验证示例的链接。通过运用这些技术,我设法解决了这个问题。

根据Mykel Alvis的回答https://stackoverflow.com/a/30439890 我把我的pom调整到以下:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.5</version>
</dependency>
<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-embedder</artifactId>
    <version>3.3.3</version>
</dependency>
<dependency>
    <groupId>org.eclipse.aether</groupId>
    <artifactId>aether-connector-basic</artifactId>
    <version>1.0.2.v20150114</version>
</dependency>
<dependency>
    <groupId>org.eclipse.aether</groupId>
    <artifactId>aether-transport-wagon</artifactId>
    <version>1.0.2.v20150114</version>
</dependency>
<dependency>
    <groupId>org.apache.maven.wagon</groupId>
    <artifactId>wagon-http</artifactId>
    <version>2.9</version>
</dependency>
<dependency>
    <groupId>org.apache.maven.wagon</groupId>
    <artifactId>wagon-provider-api</artifactId>
    <version>2.9</version>
</dependency>
<dependency>
    <groupId>org.apache.maven.wagon</groupId>
    <artifactId>wagon-http-lightweight</artifactId>
    <version>2.9</version>
</dependency>

此外,我禁用了用于此部分应用程序的所有其他依赖项。上述依赖项的组合和对未使用的依赖项的注释解决了这个问题。

工作提取版本位于https://github.com/Weirdfishees/reverse-engineering.git