** Update **
I have tried to reproduce the error and tried to create a Minimal, Complete, and Verifiable example. Unfortunately I cant even seem to reproduce the error myself (o my) even after performing restart from scratch technique...
The application works when I manually execute the the hibernate3:hbm2java command.
Still, it does not seem to work programatically.
When I run the application, a huge error log is shown. Here the top few lines:
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
Ive pushed my config to this repo. It requires an (in mem) MySQL db with pw: "password" to work.
https://github.com/Weirdfishees/reverse-engineering.git
Here is my configuration:
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&usessl=false?autoreconnect=true&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"/>
And the 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);
}
}
My question kinda stays the same.
How can I run hibernate3:hbm2java programatically given above configuration?
-- Not reproducable - but additional info --
Introduction
Hi fellow stackoverflow users, I have a question with regard to the usage of Maven Embedder.
Before I boil down to the actual code parts, I'd like to share with you the context of my question.
I am building a GUI application (javaFX) that receives input from the user. Based on this input the application dynamically generates two hibernate configuration files (hibernate.cfg.xml and reveng.xml). Based on these configuration files (that provide connection details) the following desired functionality of the application is to automatically (without user interaction) reverse engineer a database and create the POJO classes of all the tables (bottom-up development).
When I manually execute the mvn hibernate3:hbm2java argument (using either command line or simply in eclipse run as -> maven build.. -> goals mvn hibernate3:hbm2java) the desired package with all the classes that I want are created.
However, the goal is to programatically execute the mvn hibernate3:hbm2java instead of doing it manually.
Research
I have tried the following options:
Running Maven from Java code in Windows?
private void executeHbmToJava() {
try {
Runtime.getRuntime().exec("cmd \\c hibernate3:hbm2java");
} catch (IOException e) {
e.printStackTrace();
}
}
This option doesnt throw any errors, however nothing seems to happen in the program (after refreshing the view in eclipse, the desired classes are not there...)
Even if this option were to work, I prefer using Maven Embedder due to context of the application (application will be used cross platform).
Furthermore, I prefer not to use the answer from user3254289 with the suggestion to use Maven Invoker.
Question
Based on the answers given by MariuszS on the question How to run maven from java?
I have configured my application as follows:
pom dependencies:
<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>
With the following (adjusted) method taken from 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);
}
The following error appears:
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
I performed some checks with regards to the MAVEN_HOME variable. As I can execute mvn commands without any trouble in both cmd and eclipse, I think these settings are correct (correct me if I am wrong).
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
$ 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)
Does anyone know how to overcome this error and achieve my desired goal of automatically fire mvn hibernate3:hbm2java from java runtime?
TL;DR
How to fix java.lang.NoSuchMethodError when using Maven Embedder?
Caused by: java.lang.NoSuchMethodError: org.codehaus.plexus.DefaultPlexusContainer.<init> (Lorg/codehaus/plexus/ContainerConfiguration;[Lcom/google/inject/Module;)V
Given:
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);
}