Skip to content

Commit f454a01

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 13aed53 commit f454a01

File tree

6 files changed

+69
-18
lines changed

6 files changed

+69
-18
lines changed

maven-plugin/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
<maven-plugin-annotations.version>3.5</maven-plugin-annotations.version>
5959
<maven-plugin-api.version>3.5.2</maven-plugin-api.version>
60+
<maven-core.version>3.9.9</maven-core.version>
6061

6162
<!-- Plugin versions -->
6263
<maven-project-info-reports-plugin.version>2.9</maven-project-info-reports-plugin.version>
@@ -75,6 +76,13 @@
7576
<groupId>org.apache.maven</groupId>
7677
<artifactId>maven-plugin-api</artifactId>
7778
<version>${maven-plugin-api.version}</version>
79+
<scope>provided</scope>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.apache.maven</groupId>
83+
<artifactId>maven-core</artifactId>
84+
<version>${maven-core.version}</version>
85+
<scope>provided</scope>
7886
</dependency>
7987
<dependency>
8088
<groupId>org.apache.maven.plugin-tools</groupId>

maven-plugin/src/main/java/org/hibernate/mvn/AbstractHbm2xMojo.java

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
package org.hibernate.mvn;
22

3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.io.IOException;
7+
import java.net.MalformedURLException;
8+
import java.net.URL;
9+
import java.net.URLClassLoader;
10+
import java.util.ArrayList;
11+
import java.util.Properties;
12+
13+
import org.apache.maven.artifact.DependencyResolutionRequiredException;
314
import org.apache.maven.plugin.AbstractMojo;
415
import org.apache.maven.plugins.annotations.Parameter;
16+
import org.apache.maven.project.MavenProject;
517
import org.apache.tools.ant.BuildException;
618
import org.hibernate.cfg.reveng.OverrideRepository;
719
import org.hibernate.cfg.reveng.ReverseEngineeringSettings;
820
import org.hibernate.cfg.reveng.ReverseEngineeringStrategy;
921
import org.hibernate.tool.api.metadata.MetadataDescriptor;
1022
import org.hibernate.tool.api.metadata.MetadataDescriptorFactory;
1123

12-
import java.io.File;
13-
import java.io.FileInputStream;
14-
import java.io.FileNotFoundException;
15-
import java.io.IOException;
16-
import java.util.Properties;
17-
1824
public abstract class AbstractHbm2xMojo extends AbstractMojo {
1925

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

72+
@Parameter(defaultValue = "${project}", readonly = true, required = true)
73+
private MavenProject project;
74+
6675
public void execute() {
67-
getLog().info("Starting " + this.getClass().getSimpleName() + "...");
68-
ReverseEngineeringStrategy strategy = setupReverseEngineeringStrategy();
69-
if (propertyFile.exists()) {
70-
executeExporter(createJdbcDescriptor(strategy, loadPropertiesFile()));
71-
} else {
72-
getLog().info("Property file '" + propertyFile + "' cannot be found, aborting...");
73-
}
74-
getLog().info("Finished " + this.getClass().getSimpleName() + "!");
76+
ClassLoader original = Thread.currentThread().getContextClassLoader();
77+
try {
78+
Thread.currentThread().setContextClassLoader(createExporterClassLoader(original));
79+
getLog().info("Starting " + this.getClass().getSimpleName() + "...");
80+
ReverseEngineeringStrategy strategy = setupReverseEngineeringStrategy();
81+
if (propertyFile.exists()) {
82+
executeExporter(createJdbcDescriptor(strategy, loadPropertiesFile()));
83+
} else {
84+
getLog().info("Property file '" + propertyFile + "' cannot be found, aborting...");
85+
}
86+
getLog().info("Finished " + this.getClass().getSimpleName() + "!");
87+
} finally {
88+
Thread.currentThread().setContextClassLoader(original);
89+
}
7590
}
7691

7792
private ReverseEngineeringStrategy setupReverseEngineeringStrategy() {
@@ -121,5 +136,17 @@ private MetadataDescriptor createJdbcDescriptor(ReverseEngineeringStrategy strat
121136
preferBasicCompositeIds);
122137
}
123138

139+
private ClassLoader createExporterClassLoader(ClassLoader parent) {
140+
ArrayList<URL> urls = new ArrayList<URL>();
141+
try {
142+
for (String cpe : project.getRuntimeClasspathElements()) {
143+
urls.add(new File(cpe).toURI().toURL());
144+
}
145+
} catch (DependencyResolutionRequiredException | MalformedURLException e) {
146+
throw new RuntimeException("Problem while constructing project classloader", e);
147+
}
148+
return new URLClassLoader(urls.toArray(new URL[0]), parent);
149+
}
150+
124151
protected abstract void executeExporter(MetadataDescriptor metadataDescriptor);
125152
}

maven-plugin/src/main/java/org/hibernate/mvn/GenerateDaoMojo.java

Lines changed: 5 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.metadata.MetadataDescriptor;
2930
import org.hibernate.tool.hbm2x.DAOExporter;
3031

@@ -33,7 +34,10 @@
3334
* <p>
3435
* See: https://docs.jboss.org/tools/latest/en/hibernatetools/html_single/#d0e4821
3536
*/
36-
@Mojo(name = "hbm2dao", defaultPhase = GENERATE_SOURCES)
37+
@Mojo(
38+
name = "hbm2dao",
39+
defaultPhase = GENERATE_SOURCES,
40+
requiresDependencyResolution = ResolutionScope.RUNTIME)
3741
public class GenerateDaoMojo extends AbstractHbm2xMojo {
3842

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

maven-plugin/src/main/java/org/hibernate/mvn/GenerateHbmMojo.java

Lines changed: 5 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.metadata.MetadataDescriptor;
2930
import org.hibernate.tool.hbm2x.HibernateMappingExporter;
3031

@@ -33,7 +34,10 @@
3334
* <p>
3435
* See: https://docs.jboss.org/tools/latest/en/hibernatetools/html_single/#d0e4821
3536
*/
36-
@Mojo(name = "generateHbm", defaultPhase = GENERATE_SOURCES)
37+
@Mojo(
38+
name = "generateHbm",
39+
defaultPhase = GENERATE_SOURCES,
40+
requiresDependencyResolution = ResolutionScope.RUNTIME)
3741
public class GenerateHbmMojo extends AbstractHbm2xMojo {
3842

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

maven-plugin/src/main/java/org/hibernate/mvn/Hbm2DdlMojo.java

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

33
import org.apache.maven.plugins.annotations.Mojo;
44
import org.apache.maven.plugins.annotations.Parameter;
5+
import org.apache.maven.plugins.annotations.ResolutionScope;
56
import org.hibernate.boot.Metadata;
67
import org.hibernate.tool.api.metadata.MetadataDescriptor;
78
import org.hibernate.tool.hbm2ddl.SchemaExport;
@@ -18,7 +19,10 @@
1819
* <p>
1920
* See https://docs.jboss.org/tools/latest/en/hibernatetools/html_single/#d0e4651
2021
*/
21-
@Mojo(name = "hbm2ddl", defaultPhase = GENERATE_RESOURCES)
22+
@Mojo(
23+
name = "hbm2ddl",
24+
defaultPhase = GENERATE_RESOURCES,
25+
requiresDependencyResolution = ResolutionScope.RUNTIME)
2226
public class Hbm2DdlMojo extends AbstractHbm2xMojo {
2327

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

maven-plugin/src/main/java/org/hibernate/mvn/Hbm2JavaMojo.java

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

33
import org.apache.maven.plugins.annotations.Mojo;
44
import org.apache.maven.plugins.annotations.Parameter;
5+
import org.apache.maven.plugins.annotations.ResolutionScope;
56
import org.hibernate.tool.api.metadata.MetadataDescriptor;
67
import org.hibernate.tool.hbm2x.POJOExporter;
78

@@ -14,7 +15,10 @@
1415
* <p>
1516
* See: https://docs.jboss.org/tools/latest/en/hibernatetools/html_single/#d0e4821
1617
*/
17-
@Mojo(name = "hbm2java", defaultPhase = GENERATE_SOURCES)
18+
@Mojo(
19+
name = "hbm2java",
20+
defaultPhase = GENERATE_SOURCES,
21+
requiresDependencyResolution = ResolutionScope.RUNTIME)
1822
public class Hbm2JavaMojo extends AbstractHbm2xMojo {
1923

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

0 commit comments

Comments
 (0)