From 91841b869d3818eca68aaadd40f369309dc03bac Mon Sep 17 00:00:00 2001 From: Koen Aers Date: Wed, 26 Nov 2025 16:59:26 +0100 Subject: [PATCH] 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 --- .../examples/hbm2orm/with-user-type/README.md | 19 ++++++++ .../examples/hbm2orm/with-user-type/pom.xml | 26 +++++++++++ .../src/main/java/org/foo/Bar.java | 44 +++++++++++++++++++ .../src/main/resources/user-type.hbm.xml | 29 ++++++++++++ .../tool/maven/TransformHbmTestIT.java | 5 ++- .../tool/maven/TransformHbmMojo.java | 40 ++++++++++++++--- 6 files changed, 155 insertions(+), 8 deletions(-) create mode 100644 maven/docs/examples/hbm2orm/with-user-type/README.md create mode 100644 maven/docs/examples/hbm2orm/with-user-type/pom.xml create mode 100644 maven/docs/examples/hbm2orm/with-user-type/src/main/java/org/foo/Bar.java create mode 100644 maven/docs/examples/hbm2orm/with-user-type/src/main/resources/user-type.hbm.xml diff --git a/maven/docs/examples/hbm2orm/with-user-type/README.md b/maven/docs/examples/hbm2orm/with-user-type/README.md new file mode 100644 index 000000000..0b6300de1 --- /dev/null +++ b/maven/docs/examples/hbm2orm/with-user-type/README.md @@ -0,0 +1,19 @@ + +To run this example: +- Have [Apache Maven](https://maven.apache.org) installed +- Issue the following commands from a command-line window opened in this folder: + `mvn compile org.hibernate.tool:hibernate-tools-maven:${hibernate.version}:hbm2orm` \ No newline at end of file diff --git a/maven/docs/examples/hbm2orm/with-user-type/pom.xml b/maven/docs/examples/hbm2orm/with-user-type/pom.xml new file mode 100644 index 000000000..8f2461a63 --- /dev/null +++ b/maven/docs/examples/hbm2orm/with-user-type/pom.xml @@ -0,0 +1,26 @@ + + + + 4.0.0 + + org.hibernate.tool.maven.test + hbm2orm-simple-default + 0.0.1-SNAPSHOT + + \ No newline at end of file diff --git a/maven/docs/examples/hbm2orm/with-user-type/src/main/java/org/foo/Bar.java b/maven/docs/examples/hbm2orm/with-user-type/src/main/java/org/foo/Bar.java new file mode 100644 index 000000000..34baa1396 --- /dev/null +++ b/maven/docs/examples/hbm2orm/with-user-type/src/main/java/org/foo/Bar.java @@ -0,0 +1,44 @@ +/* + * Hibernate Tools, Tooling for your Hibernate Projects + * + * Copyright 2016-2025 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.foo; + +import org.hibernate.usertype.UserType; + +public class Bar implements UserType { + + @Override + public int getSqlType() { + return 0; + } + + @Override + public Class returnedClass() { + return null; + } + + @Override + public Integer deepCopy(Integer integer) { + return 0; + } + + @Override + public boolean isMutable() { + return false; + } + +} diff --git a/maven/docs/examples/hbm2orm/with-user-type/src/main/resources/user-type.hbm.xml b/maven/docs/examples/hbm2orm/with-user-type/src/main/resources/user-type.hbm.xml new file mode 100644 index 000000000..aa6eef423 --- /dev/null +++ b/maven/docs/examples/hbm2orm/with-user-type/src/main/resources/user-type.hbm.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java b/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java index 1b5e86ca6..95da8c2c3 100644 --- a/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java +++ b/maven/src/functionalTest/java/org/hibernate/tool/maven/TransformHbmTestIT.java @@ -63,8 +63,9 @@ private void runTransformHbmToOrm() throws Exception { assertFalse(ormXmlFile.exists()); new MavenCli().doMain( new String[] { - "-Dmaven.repo.local=" + localRepo.getAbsolutePath(), - "org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm" + "-Dmaven.repo.local=" + localRepo.getAbsolutePath(), + "compile", + "org.hibernate.tool:hibernate-tools-maven:" + Version.versionString() + ":hbm2orm" }, projectPath.toAbsolutePath().toString(), null, diff --git a/maven/src/main/java/org/hibernate/tool/maven/TransformHbmMojo.java b/maven/src/main/java/org/hibernate/tool/maven/TransformHbmMojo.java index 173c15f4e..e9f61d5d9 100644 --- a/maven/src/main/java/org/hibernate/tool/maven/TransformHbmMojo.java +++ b/maven/src/main/java/org/hibernate/tool/maven/TransformHbmMojo.java @@ -23,14 +23,19 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.Serial; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import org.apache.maven.artifact.DependencyResolutionRequiredException; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.project.MavenProject; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.jaxb.Origin; import org.hibernate.boot.jaxb.SourceType; @@ -62,14 +67,25 @@ public class TransformHbmMojo extends AbstractMojo { @Parameter(defaultValue = "true") private boolean format; + @Parameter(defaultValue = "${project}", readonly = true, required = true) + private MavenProject project; + @Override public void execute() { - MappingBinder mappingBinder = new MappingBinder( - MappingBinder.class.getClassLoader()::getResourceAsStream, - UnsupportedFeatureHandling.ERROR); - List hbmFiles = getHbmFiles(inputFolder); - List> hbmMappings = getHbmMappings(hbmFiles, mappingBinder); - performTransformation(hbmMappings, mappingBinder, createServiceRegistry()); + ClassLoader original = Thread.currentThread().getContextClassLoader(); + try { + Thread.currentThread().setContextClassLoader(createClassLoader(original)); + getLog().info("Starting " + this.getClass().getSimpleName() + "..."); + MappingBinder mappingBinder = new MappingBinder( + MappingBinder.class.getClassLoader()::getResourceAsStream, + UnsupportedFeatureHandling.ERROR); + List hbmFiles = getHbmFiles(inputFolder); + List> hbmMappings = getHbmMappings(hbmFiles, mappingBinder); + performTransformation(hbmMappings, mappingBinder, createServiceRegistry()); + getLog().info("Finished " + this.getClass().getSimpleName() + "!"); + } finally { + Thread.currentThread().setContextClassLoader(original); + } } private ServiceRegistry createServiceRegistry() { @@ -174,6 +190,18 @@ private List getHbmFiles(File f) { return result; } + private ClassLoader createClassLoader(ClassLoader parent) { + ArrayList urls = new ArrayList<>(); + try { + for (String cpe : project.getRuntimeClasspathElements()) { + urls.add(new File(cpe).toURI().toURL()); + } + } catch (DependencyResolutionRequiredException | MalformedURLException e) { + throw new RuntimeException("Problem while constructing project classloader", e); + } + return new URLClassLoader(urls.toArray(new URL[0]), parent); + } + private static class HbmXmlOrigin extends Origin { @Serial