Skip to content

Commit 998aede

Browse files
committed
First attempt to add the Maven mojos
1 parent 762d20f commit 998aede

File tree

2 files changed

+160
-2
lines changed

2 files changed

+160
-2
lines changed

tooling/hibernate-maven-plugin/hibernate-maven-plugin.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ configurations {
2626

2727
dependencies {
2828
implementation project( ":hibernate-core" )
29+
implementation project( ":hibernate-reveng" )
2930

3031
implementation "org.apache.maven:maven-plugin-api:3.9.11"
31-
implementation "org.apache.maven:maven-project:2.2.1"
32+
implementation "org.apache.maven:maven-core:3.9.11"
3233
implementation "org.apache.maven.shared:file-management:3.1.0"
3334

3435
compileOnly "org.apache.maven.plugin-tools:maven-plugin-tools-annotations:3.15.1"
@@ -104,6 +105,7 @@ integrationTest {
104105
environment "hibernateVersion", project.version
105106
}
106107

107-
integrationTest.dependsOn rootProject.childProjects.'hibernate-core'.tasks.publishToMavenLocal
108+
integrationTest.dependsOn ':hibernate-core:publishToMavenLocal'
109+
integrationTest.dependsOn ':hibernate-reveng:publishToMavenLocal'
108110
integrationTest.dependsOn publishToMavenLocal
109111
check.dependsOn integrationTest
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.tool.maven;
6+
7+
import java.io.File;
8+
import java.io.FileInputStream;
9+
import java.io.FileNotFoundException;
10+
import java.io.IOException;
11+
import java.net.MalformedURLException;
12+
import java.net.URL;
13+
import java.net.URLClassLoader;
14+
import java.util.ArrayList;
15+
import java.util.Properties;
16+
17+
import org.apache.maven.artifact.DependencyResolutionRequiredException;
18+
import org.apache.maven.plugin.AbstractMojo;
19+
import org.apache.maven.plugin.MojoFailureException;
20+
import org.apache.maven.plugins.annotations.Parameter;
21+
import org.apache.maven.project.MavenProject;
22+
import org.hibernate.tool.reveng.api.metadata.MetadataDescriptor;
23+
import org.hibernate.tool.reveng.api.metadata.MetadataDescriptorFactory;
24+
import org.hibernate.tool.reveng.api.metadata.MetadataConstants;
25+
import org.hibernate.tool.reveng.api.core.RevengSettings;
26+
import org.hibernate.tool.reveng.api.core.RevengStrategy;
27+
import org.hibernate.tool.reveng.api.core.RevengStrategyFactory;
28+
29+
public abstract class AbstractGenerationMojo extends AbstractMojo {
30+
31+
// For reveng strategy
32+
/** The default package name to use when mappings for classes are created. */
33+
@Parameter
34+
private String packageName;
35+
36+
/** The name of a property file, e.g. hibernate.properties. */
37+
@Parameter
38+
private File revengFile;
39+
40+
/** The class name of the reverse engineering strategy to use.
41+
* Extend the DefaultReverseEngineeringStrategy and override the corresponding methods, e.g.
42+
* to adapt the generate class names or to provide custom type mappings. */
43+
@Parameter
44+
private String revengStrategy;
45+
46+
/** If true, tables which are pure many-to-many link tables will be mapped as such.
47+
* A pure many-to-many table is one which primary-key contains exactly two foreign-keys pointing
48+
* to other entity tables and has no other columns. */
49+
@Parameter(defaultValue = "true")
50+
private boolean detectManyToMany;
51+
52+
/** If true, a one-to-one association will be created for each foreignkey found. */
53+
@Parameter(defaultValue = "true")
54+
private boolean detectOneToOne;
55+
56+
/** If true, columns named VERSION or TIMESTAMP with appropriate types will be mapped with the appropriate
57+
* optimistic locking corresponding to <version> or <timestamp>. */
58+
@Parameter(defaultValue = "true")
59+
private boolean detectOptimisticLock;
60+
61+
/** If true, a collection will be mapped for each foreignkey. */
62+
@Parameter(defaultValue = "true")
63+
private boolean createCollectionForForeignKey;
64+
65+
/** If true, a many-to-one association will be created for each foreignkey found. */
66+
@Parameter(defaultValue = "true")
67+
private boolean createManyToOneForForeignKey;
68+
69+
// For configuration
70+
/** The name of a property file, e.g. hibernate.properties. */
71+
@Parameter(defaultValue = "${project.basedir}/src/main/resources/hibernate.properties")
72+
private File propertyFile;
73+
74+
// Not exposed for now
75+
private boolean preferBasicCompositeIds = true;
76+
77+
@Parameter(defaultValue = "${project}", readonly = true, required = true)
78+
private MavenProject project;
79+
80+
public void execute() throws MojoFailureException {
81+
ClassLoader original = Thread.currentThread().getContextClassLoader();
82+
try {
83+
Thread.currentThread().setContextClassLoader(createExporterClassLoader(original));
84+
getLog().info("Starting " + this.getClass().getSimpleName() + "...");
85+
RevengStrategy strategy = setupReverseEngineeringStrategy();
86+
if (propertyFile.exists()) {
87+
executeExporter(createJdbcDescriptor(strategy, loadPropertiesFile()));
88+
}
89+
else {
90+
getLog().info("Property file '" + propertyFile + "' cannot be found, aborting...");
91+
}
92+
getLog().info("Finished " + this.getClass().getSimpleName() + "!");
93+
}
94+
finally {
95+
Thread.currentThread().setContextClassLoader(original);
96+
}
97+
}
98+
99+
private RevengStrategy setupReverseEngineeringStrategy() {
100+
File[] revengFiles = null;
101+
if (revengFile != null) {
102+
revengFiles = new File[] { revengFile };
103+
}
104+
RevengStrategy strategy =
105+
RevengStrategyFactory.createReverseEngineeringStrategy(
106+
revengStrategy,
107+
revengFiles);
108+
RevengSettings settings =
109+
new RevengSettings(strategy)
110+
.setDefaultPackageName(packageName)
111+
.setDetectManyToMany(detectManyToMany)
112+
.setDetectOneToOne(detectOneToOne)
113+
.setDetectOptimisticLock(detectOptimisticLock)
114+
.setCreateCollectionForForeignKey(createCollectionForForeignKey)
115+
.setCreateManyToOneForForeignKey(createManyToOneForForeignKey);
116+
strategy.setSettings(settings);
117+
return strategy;
118+
}
119+
120+
private Properties loadPropertiesFile() throws MojoFailureException {
121+
try (FileInputStream is = new FileInputStream(propertyFile)) {
122+
Properties result = new Properties();
123+
result.load(is);
124+
return result;
125+
}
126+
catch (FileNotFoundException e) {
127+
throw new MojoFailureException(propertyFile + " not found.", e);
128+
}
129+
catch (IOException e) {
130+
throw new MojoFailureException("Problem while loading " + propertyFile, e);
131+
}
132+
}
133+
134+
private MetadataDescriptor createJdbcDescriptor(RevengStrategy strategy, Properties properties) {
135+
properties.put(MetadataConstants.PREFER_BASIC_COMPOSITE_IDS, preferBasicCompositeIds);
136+
return MetadataDescriptorFactory
137+
.createReverseEngineeringDescriptor(
138+
strategy,
139+
properties);
140+
}
141+
142+
private ClassLoader createExporterClassLoader(ClassLoader parent) {
143+
ArrayList<URL> urls = new ArrayList<URL>();
144+
try {
145+
for (String cpe : project.getRuntimeClasspathElements()) {
146+
urls.add(new File(cpe).toURI().toURL());
147+
}
148+
}
149+
catch (DependencyResolutionRequiredException | MalformedURLException e) {
150+
throw new RuntimeException("Problem while constructing project classloader", e);
151+
}
152+
return new URLClassLoader(urls.toArray(new URL[0]), parent);
153+
}
154+
155+
protected abstract void executeExporter(MetadataDescriptor metadataDescriptor);
156+
}

0 commit comments

Comments
 (0)