Skip to content

Commit b1c8e11

Browse files
committed
HBX-2964: Resolve the project class path and execute the exporters using that class path in the reverse engineering Maven mojos
Signed-off-by: Koen Aers <[email protected]>
1 parent f515a5a commit b1c8e11

File tree

6 files changed

+65
-12
lines changed

6 files changed

+65
-12
lines changed

maven/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373

7474
<maven-plugin-annotations.version>3.5</maven-plugin-annotations.version>
7575
<maven-plugin-api.version>3.5.2</maven-plugin-api.version>
76+
<maven-core.version>3.9.9</maven-core.version>
7677

7778
<!-- Plugin versions -->
7879
<maven-project-info-reports-plugin.version>2.9</maven-project-info-reports-plugin.version>
@@ -92,6 +93,11 @@
9293
<artifactId>maven-plugin-api</artifactId>
9394
<version>${maven-plugin-api.version}</version>
9495
</dependency>
96+
<dependency>
97+
<groupId>org.apache.maven</groupId>
98+
<artifactId>maven-core</artifactId>
99+
<version>${maven-core.version}</version>
100+
</dependency>
95101
<dependency>
96102
<groupId>org.apache.maven.plugin-tools</groupId>
97103
<artifactId>maven-plugin-annotations</artifactId>

maven/src/main/java/org/hibernate/tool/maven/AbstractGenerationMojo.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@
2323
import java.io.FileInputStream;
2424
import java.io.FileNotFoundException;
2525
import java.io.IOException;
26+
import java.net.MalformedURLException;
27+
import java.net.URL;
28+
import java.net.URLClassLoader;
29+
import java.util.ArrayList;
30+
import java.util.Arrays;
31+
import java.util.HashSet;
2632
import java.util.Properties;
2733

34+
import org.apache.maven.artifact.DependencyResolutionRequiredException;
2835
import org.apache.maven.plugin.AbstractMojo;
2936
import org.apache.maven.plugins.annotations.Parameter;
37+
import org.apache.maven.project.MavenProject;
3038
import org.apache.tools.ant.BuildException;
3139
import org.hibernate.tool.api.metadata.MetadataDescriptor;
3240
import org.hibernate.tool.api.metadata.MetadataDescriptorFactory;
@@ -83,15 +91,24 @@ public abstract class AbstractGenerationMojo extends AbstractMojo {
8391
// Not exposed for now
8492
private boolean preferBasicCompositeIds = true;
8593

94+
@Parameter(defaultValue = "${project}", readonly = true, required = true)
95+
private MavenProject project;
96+
8697
public void execute() {
87-
getLog().info("Starting " + this.getClass().getSimpleName() + "...");
88-
RevengStrategy strategy = setupReverseEngineeringStrategy();
89-
if (propertyFile.exists()) {
90-
executeExporter(createJdbcDescriptor(strategy, loadPropertiesFile()));
91-
} else {
92-
getLog().info("Property file '" + propertyFile + "' cannot be found, aborting...");
93-
}
94-
getLog().info("Finished " + this.getClass().getSimpleName() + "!");
98+
ClassLoader original = Thread.currentThread().getContextClassLoader();
99+
try {
100+
Thread.currentThread().setContextClassLoader(createExporterClassLoader(original));
101+
getLog().info("Starting " + this.getClass().getSimpleName() + "...");
102+
RevengStrategy strategy = setupReverseEngineeringStrategy();
103+
if (propertyFile.exists()) {
104+
executeExporter(createJdbcDescriptor(strategy, loadPropertiesFile()));
105+
} else {
106+
getLog().info("Property file '" + propertyFile + "' cannot be found, aborting...");
107+
}
108+
getLog().info("Finished " + this.getClass().getSimpleName() + "!");
109+
} finally {
110+
Thread.currentThread().setContextClassLoader(original);
111+
}
95112
}
96113

97114
private RevengStrategy setupReverseEngineeringStrategy() {
@@ -134,6 +151,18 @@ private MetadataDescriptor createJdbcDescriptor(RevengStrategy strategy, Propert
134151
strategy,
135152
properties);
136153
}
154+
155+
private ClassLoader createExporterClassLoader(ClassLoader parent) {
156+
ArrayList<URL> urls = new ArrayList<URL>();
157+
try {
158+
for (String cpe : project.getRuntimeClasspathElements()) {
159+
urls.add(new File(cpe).toURI().toURL());
160+
}
161+
} catch (DependencyResolutionRequiredException | MalformedURLException e) {
162+
throw new RuntimeException("Problem while constructing project classloader", e);
163+
}
164+
return new URLClassLoader(urls.toArray(new URL[0]), parent);
165+
}
137166

138167
protected abstract void executeExporter(MetadataDescriptor metadataDescriptor);
139168
}

maven/src/main/java/org/hibernate/tool/maven/GenerateDaoMojo.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.maven.plugins.annotations.Mojo;
2323
import org.apache.maven.plugins.annotations.Parameter;
24+
import org.apache.maven.plugins.annotations.ResolutionScope;
2425
import org.hibernate.tool.api.export.Exporter;
2526
import org.hibernate.tool.api.export.ExporterConstants;
2627
import org.hibernate.tool.api.export.ExporterFactory;
@@ -36,7 +37,10 @@
3637
* <p>
3738
* See: https://docs.jboss.org/tools/latest/en/hibernatetools/html_single/#d0e4821
3839
*/
39-
@Mojo(name = "hbm2dao", defaultPhase = GENERATE_SOURCES)
40+
@Mojo(
41+
name = "hbm2dao",
42+
defaultPhase = GENERATE_SOURCES,
43+
requiresDependencyResolution = ResolutionScope.RUNTIME)
4044
public class GenerateDaoMojo extends AbstractGenerationMojo {
4145

4246
/** The directory into which the DAOs will be generated. */

maven/src/main/java/org/hibernate/tool/maven/GenerateDdlMojo.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.maven.plugins.annotations.Mojo;
2323
import org.apache.maven.plugins.annotations.Parameter;
24+
import org.apache.maven.plugins.annotations.ResolutionScope;
2425
import org.hibernate.boot.Metadata;
2526
import org.hibernate.tool.api.metadata.MetadataDescriptor;
2627
import org.hibernate.tool.hbm2ddl.SchemaExport;
@@ -37,7 +38,10 @@
3738
* <p>
3839
* See https://docs.jboss.org/tools/latest/en/hibernatetools/html_single/#d0e4651
3940
*/
40-
@Mojo(name = "hbm2ddl", defaultPhase = GENERATE_RESOURCES)
41+
@Mojo(
42+
name = "hbm2ddl",
43+
defaultPhase = GENERATE_RESOURCES,
44+
requiresDependencyResolution = ResolutionScope.RUNTIME)
4145
public class GenerateDdlMojo extends AbstractGenerationMojo {
4246

4347
/** The directory into which the DDLs will be generated. */

maven/src/main/java/org/hibernate/tool/maven/GenerateHbmMojo.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.maven.plugins.annotations.Mojo;
2323
import org.apache.maven.plugins.annotations.Parameter;
24+
import org.apache.maven.plugins.annotations.ResolutionScope;
2425
import org.hibernate.tool.api.export.Exporter;
2526
import org.hibernate.tool.api.export.ExporterConstants;
2627
import org.hibernate.tool.api.export.ExporterFactory;
@@ -36,7 +37,10 @@
3637
* <p>
3738
* See: https://docs.jboss.org/tools/latest/en/hibernatetools/html_single/#d0e4821
3839
*/
39-
@Mojo(name = "generateHbm", defaultPhase = GENERATE_SOURCES)
40+
@Mojo(
41+
name = "generateHbm",
42+
defaultPhase = GENERATE_SOURCES,
43+
requiresDependencyResolution = ResolutionScope.RUNTIME)
4044
public class GenerateHbmMojo extends AbstractGenerationMojo {
4145

4246
/** The directory into which the DAOs will be generated. */

maven/src/main/java/org/hibernate/tool/maven/GenerateJavaMojo.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.apache.maven.plugins.annotations.Mojo;
2727
import org.apache.maven.plugins.annotations.Parameter;
28+
import org.apache.maven.plugins.annotations.ResolutionScope;
2829
import org.hibernate.tool.api.export.Exporter;
2930
import org.hibernate.tool.api.export.ExporterConstants;
3031
import org.hibernate.tool.api.export.ExporterFactory;
@@ -36,7 +37,10 @@
3637
* <p>
3738
* See: https://docs.jboss.org/tools/latest/en/hibernatetools/html_single/#d0e4821
3839
*/
39-
@Mojo(name = "hbm2java", defaultPhase = GENERATE_SOURCES)
40+
@Mojo(
41+
name = "hbm2java",
42+
defaultPhase = GENERATE_SOURCES,
43+
requiresDependencyResolution = ResolutionScope.RUNTIME)
4044
public class GenerateJavaMojo extends AbstractGenerationMojo {
4145

4246
/** The directory into which the JPA entities will be generated. */
@@ -69,6 +73,8 @@ protected void executeExporter(MetadataDescriptor metadataDescriptor) {
6973
getLog().info("Starting POJO export to directory: " + outputDirectory + "...");
7074
pojoExporter.start();
7175
}
76+
77+
7278

7379

7480
}

0 commit comments

Comments
 (0)