Skip to content

Commit 91841b8

Browse files
committed
HBX-3234: Improve the Maven hbm2orm Mojo
- Execute the mojo in a classloader including the project's classpath (to include the project classes possibly referenced in the hbm.xml) - Add an example illustrating the use of a UserClass in hbm.xml - Add an integration test to 'ExamplesTestIT' testing the above example Signed-off-by: Koen Aers <[email protected]>
1 parent 3b2ac75 commit 91841b8

File tree

6 files changed

+155
-8
lines changed

6 files changed

+155
-8
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!--
2+
~ Copyright 2004 - 2025 Red Hat, Inc.
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" basis,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
To run this example:
17+
- Have [Apache Maven](https://maven.apache.org) installed
18+
- Issue the following commands from a command-line window opened in this folder:
19+
`mvn compile org.hibernate.tool:hibernate-tools-maven:${hibernate.version}:hbm2orm`
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2018 - 2025 Red Hat, Inc.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" basis,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
22+
<groupId>org.hibernate.tool.maven.test</groupId>
23+
<artifactId>hbm2orm-simple-default</artifactId>
24+
<version>0.0.1-SNAPSHOT</version>
25+
26+
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Hibernate Tools, Tooling for your Hibernate Projects
3+
*
4+
* Copyright 2016-2025 Red Hat, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" basis,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.foo;
19+
20+
import org.hibernate.usertype.UserType;
21+
22+
public class Bar implements UserType<Integer> {
23+
24+
@Override
25+
public int getSqlType() {
26+
return 0;
27+
}
28+
29+
@Override
30+
public Class<Integer> returnedClass() {
31+
return null;
32+
}
33+
34+
@Override
35+
public Integer deepCopy(Integer integer) {
36+
return 0;
37+
}
38+
39+
@Override
40+
public boolean isMutable() {
41+
return false;
42+
}
43+
44+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2004 - 2025 Red Hat, Inc.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" basis,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<!DOCTYPE hibernate-mapping PUBLIC
18+
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
19+
"https://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
20+
<hibernate-mapping>
21+
22+
<class name="org.bar.Foo">
23+
<id name="id" type="long">
24+
<generator class="assigned"/>
25+
</id>
26+
<property name="name" type="org.foo.Bar"/>
27+
</class>
28+
29+
</hibernate-mapping>

maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ private void runTransformHbmToOrm() throws Exception {
6363
assertFalse(ormXmlFile.exists());
6464
new MavenCli().doMain(
6565
new String[] {
66-
"-Dmaven.repo.local=" + localRepo.getAbsolutePath(),
67-
"org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm"
66+
"-Dmaven.repo.local=" + localRepo.getAbsolutePath(),
67+
"compile",
68+
"org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm"
6869
},
6970
projectPath.toAbsolutePath().toString(),
7071
null,

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@
2323
import java.io.FileInputStream;
2424
import java.io.IOException;
2525
import java.io.Serial;
26+
import java.net.MalformedURLException;
27+
import java.net.URL;
28+
import java.net.URLClassLoader;
2629
import java.util.ArrayList;
2730
import java.util.List;
2831
import java.util.Objects;
2932

33+
import org.apache.maven.artifact.DependencyResolutionRequiredException;
3034
import org.apache.maven.plugin.AbstractMojo;
3135
import org.apache.maven.plugins.annotations.Mojo;
3236
import org.apache.maven.plugins.annotations.Parameter;
3337
import org.apache.maven.plugins.annotations.ResolutionScope;
38+
import org.apache.maven.project.MavenProject;
3439
import org.hibernate.boot.MetadataSources;
3540
import org.hibernate.boot.jaxb.Origin;
3641
import org.hibernate.boot.jaxb.SourceType;
@@ -62,14 +67,25 @@ public class TransformHbmMojo extends AbstractMojo {
6267
@Parameter(defaultValue = "true")
6368
private boolean format;
6469

70+
@Parameter(defaultValue = "${project}", readonly = true, required = true)
71+
private MavenProject project;
72+
6573
@Override
6674
public void execute() {
67-
MappingBinder mappingBinder = new MappingBinder(
68-
MappingBinder.class.getClassLoader()::getResourceAsStream,
69-
UnsupportedFeatureHandling.ERROR);
70-
List<File> hbmFiles = getHbmFiles(inputFolder);
71-
List<Binding<JaxbHbmHibernateMapping>> hbmMappings = getHbmMappings(hbmFiles, mappingBinder);
72-
performTransformation(hbmMappings, mappingBinder, createServiceRegistry());
75+
ClassLoader original = Thread.currentThread().getContextClassLoader();
76+
try {
77+
Thread.currentThread().setContextClassLoader(createClassLoader(original));
78+
getLog().info("Starting " + this.getClass().getSimpleName() + "...");
79+
MappingBinder mappingBinder = new MappingBinder(
80+
MappingBinder.class.getClassLoader()::getResourceAsStream,
81+
UnsupportedFeatureHandling.ERROR);
82+
List<File> hbmFiles = getHbmFiles(inputFolder);
83+
List<Binding<JaxbHbmHibernateMapping>> hbmMappings = getHbmMappings(hbmFiles, mappingBinder);
84+
performTransformation(hbmMappings, mappingBinder, createServiceRegistry());
85+
getLog().info("Finished " + this.getClass().getSimpleName() + "!");
86+
} finally {
87+
Thread.currentThread().setContextClassLoader(original);
88+
}
7389
}
7490

7591
private ServiceRegistry createServiceRegistry() {
@@ -174,6 +190,18 @@ private List<File> getHbmFiles(File f) {
174190
return result;
175191
}
176192

193+
private ClassLoader createClassLoader(ClassLoader parent) {
194+
ArrayList<URL> urls = new ArrayList<>();
195+
try {
196+
for (String cpe : project.getRuntimeClasspathElements()) {
197+
urls.add(new File(cpe).toURI().toURL());
198+
}
199+
} catch (DependencyResolutionRequiredException | MalformedURLException e) {
200+
throw new RuntimeException("Problem while constructing project classloader", e);
201+
}
202+
return new URLClassLoader(urls.toArray(new URL[0]), parent);
203+
}
204+
177205
private static class HbmXmlOrigin extends Origin {
178206

179207
@Serial

0 commit comments

Comments
 (0)