Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

<maven-plugin-annotations.version>3.5</maven-plugin-annotations.version>
<maven-plugin-api.version>3.5.2</maven-plugin-api.version>
<maven-core.version>3.9.9</maven-core.version>

<!-- Plugin versions -->
<maven-project-info-reports-plugin.version>2.9</maven-project-info-reports-plugin.version>
Expand All @@ -75,6 +76,13 @@
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven-plugin-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven-core.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
Expand Down
55 changes: 41 additions & 14 deletions maven-plugin/src/main/java/org/hibernate/mvn/AbstractHbm2xMojo.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package org.hibernate.mvn;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Properties;

import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.tools.ant.BuildException;
import org.hibernate.cfg.reveng.OverrideRepository;
import org.hibernate.cfg.reveng.ReverseEngineeringSettings;
import org.hibernate.cfg.reveng.ReverseEngineeringStrategy;
import org.hibernate.tool.api.metadata.MetadataDescriptor;
import org.hibernate.tool.api.metadata.MetadataDescriptorFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public abstract class AbstractHbm2xMojo extends AbstractMojo {

// For reveng strategy
Expand Down Expand Up @@ -63,15 +69,24 @@ public abstract class AbstractHbm2xMojo extends AbstractMojo {
// Not exposed for now
private boolean preferBasicCompositeIds = true;

@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;

public void execute() {
getLog().info("Starting " + this.getClass().getSimpleName() + "...");
ReverseEngineeringStrategy strategy = setupReverseEngineeringStrategy();
if (propertyFile.exists()) {
executeExporter(createJdbcDescriptor(strategy, loadPropertiesFile()));
} else {
getLog().info("Property file '" + propertyFile + "' cannot be found, aborting...");
}
getLog().info("Finished " + this.getClass().getSimpleName() + "!");
ClassLoader original = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(createExporterClassLoader(original));
getLog().info("Starting " + this.getClass().getSimpleName() + "...");
ReverseEngineeringStrategy strategy = setupReverseEngineeringStrategy();
if (propertyFile.exists()) {
executeExporter(createJdbcDescriptor(strategy, loadPropertiesFile()));
} else {
getLog().info("Property file '" + propertyFile + "' cannot be found, aborting...");
}
getLog().info("Finished " + this.getClass().getSimpleName() + "!");
} finally {
Thread.currentThread().setContextClassLoader(original);
}
}

private ReverseEngineeringStrategy setupReverseEngineeringStrategy() {
Expand Down Expand Up @@ -121,5 +136,17 @@ private MetadataDescriptor createJdbcDescriptor(ReverseEngineeringStrategy strat
preferBasicCompositeIds);
}

private ClassLoader createExporterClassLoader(ClassLoader parent) {
ArrayList<URL> urls = new ArrayList<URL>();
try {
for (String cpe : project.getRuntimeClasspathElements()) {
urls.add(new File(cpe).toURI().toURL());
}
} catch (DependencyResolutionRequiredException | MalformedURLException e) {
throw new RuntimeException("Problem while constructing project classloader", e);
}
return new URLClassLoader(urls.toArray(new URL[0]), parent);
}

protected abstract void executeExporter(MetadataDescriptor metadataDescriptor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.hibernate.tool.api.metadata.MetadataDescriptor;
import org.hibernate.tool.hbm2x.DAOExporter;

Expand All @@ -33,7 +34,10 @@
* <p>
* See: https://docs.jboss.org/tools/latest/en/hibernatetools/html_single/#d0e4821
*/
@Mojo(name = "hbm2dao", defaultPhase = GENERATE_SOURCES)
@Mojo(
name = "hbm2dao",
defaultPhase = GENERATE_SOURCES,
requiresDependencyResolution = ResolutionScope.RUNTIME)
public class GenerateDaoMojo extends AbstractHbm2xMojo {

/** The directory into which the DAOs will be generated. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.hibernate.tool.api.metadata.MetadataDescriptor;
import org.hibernate.tool.hbm2x.HibernateMappingExporter;

Expand All @@ -33,7 +34,10 @@
* <p>
* See: https://docs.jboss.org/tools/latest/en/hibernatetools/html_single/#d0e4821
*/
@Mojo(name = "generateHbm", defaultPhase = GENERATE_SOURCES)
@Mojo(
name = "generateHbm",
defaultPhase = GENERATE_SOURCES,
requiresDependencyResolution = ResolutionScope.RUNTIME)
public class GenerateHbmMojo extends AbstractHbm2xMojo {

/** The directory into which the DAOs will be generated. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.hibernate.boot.Metadata;
import org.hibernate.tool.api.metadata.MetadataDescriptor;
import org.hibernate.tool.hbm2ddl.SchemaExport;
Expand All @@ -18,7 +19,10 @@
* <p>
* See https://docs.jboss.org/tools/latest/en/hibernatetools/html_single/#d0e4651
*/
@Mojo(name = "hbm2ddl", defaultPhase = GENERATE_RESOURCES)
@Mojo(
name = "hbm2ddl",
defaultPhase = GENERATE_RESOURCES,
requiresDependencyResolution = ResolutionScope.RUNTIME)
public class Hbm2DdlMojo extends AbstractHbm2xMojo {

/** The directory into which the DDLs will be generated. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.hibernate.tool.api.metadata.MetadataDescriptor;
import org.hibernate.tool.hbm2x.POJOExporter;

Expand All @@ -14,7 +15,10 @@
* <p>
* See: https://docs.jboss.org/tools/latest/en/hibernatetools/html_single/#d0e4821
*/
@Mojo(name = "hbm2java", defaultPhase = GENERATE_SOURCES)
@Mojo(
name = "hbm2java",
defaultPhase = GENERATE_SOURCES,
requiresDependencyResolution = ResolutionScope.RUNTIME)
public class Hbm2JavaMojo extends AbstractHbm2xMojo {

/** The directory into which the JPA entities will be generated. */
Expand Down