|
1 | 1 | package org.hibernate.mvn;
|
2 | 2 |
|
| 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; |
3 | 14 | import org.apache.maven.plugin.AbstractMojo;
|
4 | 15 | import org.apache.maven.plugins.annotations.Parameter;
|
| 16 | +import org.apache.maven.project.MavenProject; |
5 | 17 | import org.apache.tools.ant.BuildException;
|
6 | 18 | import org.hibernate.cfg.reveng.OverrideRepository;
|
7 | 19 | import org.hibernate.cfg.reveng.ReverseEngineeringSettings;
|
8 | 20 | import org.hibernate.cfg.reveng.ReverseEngineeringStrategy;
|
9 | 21 | import org.hibernate.tool.api.metadata.MetadataDescriptor;
|
10 | 22 | import org.hibernate.tool.api.metadata.MetadataDescriptorFactory;
|
11 | 23 |
|
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 |
| - |
18 | 24 | public abstract class AbstractHbm2xMojo extends AbstractMojo {
|
19 | 25 |
|
20 | 26 | // For reveng strategy
|
@@ -63,15 +69,24 @@ public abstract class AbstractHbm2xMojo extends AbstractMojo {
|
63 | 69 | // Not exposed for now
|
64 | 70 | private boolean preferBasicCompositeIds = true;
|
65 | 71 |
|
| 72 | + @Parameter(defaultValue = "${project}", readonly = true, required = true) |
| 73 | + private MavenProject project; |
| 74 | + |
66 | 75 | 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 | + } |
75 | 90 | }
|
76 | 91 |
|
77 | 92 | private ReverseEngineeringStrategy setupReverseEngineeringStrategy() {
|
@@ -121,5 +136,17 @@ private MetadataDescriptor createJdbcDescriptor(ReverseEngineeringStrategy strat
|
121 | 136 | preferBasicCompositeIds);
|
122 | 137 | }
|
123 | 138 |
|
| 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 | + |
124 | 151 | protected abstract void executeExporter(MetadataDescriptor metadataDescriptor);
|
125 | 152 | }
|
0 commit comments