Skip to content

Commit 33da5d6

Browse files
committed
HBX-2850: Add support for generation of 'orm.xml' files (as 'hbm.xml' will disappear at some point)
- Initial contribution of a Maven Mojo transoforming 'hbm.xml' files into 'mapping.xml' files Signed-off-by: Koen Aers <[email protected]>
1 parent 499fb95 commit 33da5d6

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

maven/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@
8686
<groupId>org.hibernate.tool</groupId>
8787
<artifactId>hibernate-tools-orm</artifactId>
8888
</dependency>
89+
<dependency>
90+
<groupId>jakarta.xml.bind</groupId>
91+
<artifactId>jakarta.xml.bind-api</artifactId>
92+
</dependency>
8993
<!-- Maven Plugins -->
9094
<dependency>
9195
<groupId>org.apache.maven</groupId>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package org.hibernate.tool.maven;
2+
3+
import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_RESOURCES;
4+
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.IOException;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import org.apache.maven.plugin.AbstractMojo;
12+
import org.apache.maven.plugin.MojoExecutionException;
13+
import org.apache.maven.plugin.MojoFailureException;
14+
import org.apache.maven.plugins.annotations.Mojo;
15+
import org.apache.maven.plugins.annotations.Parameter;
16+
import org.hibernate.boot.jaxb.Origin;
17+
import org.hibernate.boot.jaxb.SourceType;
18+
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping;
19+
import org.hibernate.boot.jaxb.hbm.transform.HbmXmlTransformer;
20+
import org.hibernate.boot.jaxb.hbm.transform.UnsupportedFeatureHandling;
21+
import org.hibernate.boot.jaxb.internal.MappingBinder;
22+
import org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappingsImpl;
23+
import org.hibernate.boot.jaxb.spi.Binding;
24+
25+
import jakarta.xml.bind.JAXBException;
26+
import jakarta.xml.bind.Marshaller;
27+
28+
@Mojo(name = "hbm2orm", defaultPhase = GENERATE_RESOURCES)
29+
public class TransformHbmMojo extends AbstractMojo {
30+
31+
@Parameter(defaultValue = "${project.basedir}/src/main/resources")
32+
private File inputFolder;
33+
34+
@Override
35+
public void execute() throws MojoExecutionException, MojoFailureException {
36+
MappingBinder mappingBinder = new MappingBinder(
37+
MappingBinder.class.getClassLoader()::getResourceAsStream,
38+
UnsupportedFeatureHandling.ERROR);
39+
Marshaller marshaller = createMarshaller(mappingBinder);
40+
List<File> hbmFiles = getHbmFiles(inputFolder);
41+
List<Binding<JaxbHbmHibernateMapping>> hbmMappings = getHbmMappings(hbmFiles, mappingBinder);
42+
List<Binding<JaxbEntityMappingsImpl>> transformed =
43+
HbmXmlTransformer.transform(hbmMappings, UnsupportedFeatureHandling.ERROR);
44+
for (int i = 0; i < hbmFiles.size(); i++) {
45+
marshall(marshaller, transformed.get(i).getRoot(), hbmFiles.get(i));
46+
}
47+
}
48+
49+
private List<Binding<JaxbHbmHibernateMapping>> getHbmMappings(List<File> hbmXmlFiles, MappingBinder mappingBinder) {
50+
List<Binding<JaxbHbmHibernateMapping>> result = new ArrayList<Binding<JaxbHbmHibernateMapping>>();
51+
hbmXmlFiles.forEach((hbmXmlFile) -> {
52+
final String fullPath = hbmXmlFile.getAbsolutePath();
53+
getLog().info("Adding file: '" + fullPath + "' to the list to be transformed.");
54+
Origin origin = new Origin(SourceType.FILE, hbmXmlFile.getAbsolutePath());
55+
Binding<JaxbHbmHibernateMapping> binding = bindMapping( mappingBinder, hbmXmlFile, origin );
56+
result.add(binding);
57+
});
58+
return result;
59+
}
60+
61+
private void marshall(Marshaller marshaller, JaxbEntityMappingsImpl mappings, File hbmXmlFile) {
62+
File mappingXmlFile = new File(
63+
hbmXmlFile.getParentFile(),
64+
hbmXmlFile.getName().replace(".hbm.xml", ".mapping.xml"));
65+
getLog().info("Marshalling file: " + hbmXmlFile.getAbsolutePath() + " into " + mappingXmlFile.getAbsolutePath());
66+
try {
67+
marshaller.marshal( mappings, mappingXmlFile );
68+
}
69+
catch (JAXBException e) {
70+
throw new RuntimeException(
71+
"Unable to marshall mapping JAXB representation to file `" + mappingXmlFile.getAbsolutePath() + "`",
72+
e
73+
);
74+
}
75+
}
76+
77+
private Binding<JaxbHbmHibernateMapping> bindMapping(
78+
MappingBinder mappingBinder, File hbmXmlFile, Origin origin) {
79+
try ( final FileInputStream fileStream = new FileInputStream(hbmXmlFile) ) {
80+
return mappingBinder.bind( fileStream, origin );
81+
}
82+
catch (IOException e) {
83+
getLog().warn( "Unable to open hbm.xml file `" + hbmXmlFile.getAbsolutePath() + "` for transformation", e );
84+
return null;
85+
}
86+
}
87+
88+
private Marshaller createMarshaller(MappingBinder mappingBinder) {
89+
try {
90+
return mappingBinder.mappingJaxbContext().createMarshaller();
91+
} catch (JAXBException e) {
92+
throw new RuntimeException("Unable to create JAXB Marshaller", e);
93+
}
94+
}
95+
96+
private List<File> getHbmFiles(File f) {
97+
List<File> result = new ArrayList<File>();
98+
if (f.isFile()) {
99+
if (f.getName().endsWith("hbm.xml")) {
100+
result.add(f);
101+
}
102+
} else {
103+
for (File child : f.listFiles()) {
104+
result.addAll(getHbmFiles(child));
105+
}
106+
}
107+
return result;
108+
}
109+
110+
}

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<mysql.version>8.0.22</mysql.version>
102102
<oracle.version>19.3.0.0</oracle.version>
103103
<sqlserver.version>9.2.1.jre8</sqlserver.version>
104+
<jakarta.xml.bind-api.version>4.0.0</jakarta.xml.bind-api.version>
104105

105106
<maven.compiler.target>17</maven.compiler.target>
106107
<maven.compiler.source>17</maven.compiler.source>
@@ -132,6 +133,11 @@
132133
<version>${oracle.version}</version>
133134
<scope>test</scope>
134135
</dependency>
136+
<dependency>
137+
<groupId>jakarta.xml.bind</groupId>
138+
<artifactId>jakarta.xml.bind-api</artifactId>
139+
<version>${jakarta.xml.bind-api.version}</version>
140+
</dependency>
135141
<dependency>
136142
<groupId>javax</groupId>
137143
<artifactId>javaee-api</artifactId>

0 commit comments

Comments
 (0)