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 0c99673bc..8276d8b5e 100644 --- a/maven/src/main/java/org/hibernate/tool/maven/TransformHbmMojo.java +++ b/maven/src/main/java/org/hibernate/tool/maven/TransformHbmMojo.java @@ -22,12 +22,12 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.Serial; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; @@ -54,128 +54,131 @@ defaultPhase = GENERATE_RESOURCES, requiresDependencyResolution = ResolutionScope.RUNTIME) public class TransformHbmMojo extends AbstractMojo { - + @Parameter(defaultValue = "${project.basedir}/src/main/resources") - private File inputFolder; - - @Override - public void execute() throws MojoExecutionException, MojoFailureException { - MappingBinder mappingBinder = new MappingBinder( - MappingBinder.class.getClassLoader()::getResourceAsStream, - UnsupportedFeatureHandling.ERROR); - List hbmFiles = getHbmFiles(inputFolder); - List> hbmMappings = getHbmMappings(hbmFiles, mappingBinder); - performTransformation(hbmMappings, mappingBinder, createServiceRegistry()); - } - - private ServiceRegistry createServiceRegistry() { - StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(); - ssrb.clearSettings(); - ssrb.applySetting(JdbcSettings.ALLOW_METADATA_ON_BOOT, false); - // Choose the H2 dialect by default, make this configurable - ssrb.applySetting(JdbcSettings.DIALECT, H2Dialect.class.getName()); - return ssrb.build(); - } - - private void performTransformation( - List> hbmBindings, - MappingBinder mappingBinder, - ServiceRegistry serviceRegistry) { - Marshaller marshaller = createMarshaller(mappingBinder); - MetadataSources metadataSources = new MetadataSources( serviceRegistry ); - hbmBindings.forEach( metadataSources::addHbmXmlBinding ); - List> transformedBindings = HbmXmlTransformer.transform( - hbmBindings, - (MetadataImplementor) metadataSources.buildMetadata(), - UnsupportedFeatureHandling.ERROR - ); - for (int i = 0; i < hbmBindings.size(); i++) { - Binding hbmBinding = hbmBindings.get( i ); - Binding transformedBinding = transformedBindings.get( i ); - - HbmXmlOrigin origin = (HbmXmlOrigin)hbmBinding.getOrigin(); - File hbmXmlFile = origin.getHbmXmlFile(); - - marshall(marshaller, transformedBinding.getRoot(), hbmXmlFile); - } - } - - private List> getHbmMappings(List hbmXmlFiles, MappingBinder mappingBinder) { - List> result = new ArrayList>(); - hbmXmlFiles.forEach((hbmXmlFile) -> { - final String fullPath = hbmXmlFile.getAbsolutePath(); - getLog().info("Adding file: '" + fullPath + "' to the list to be transformed."); - Origin origin = new HbmXmlOrigin(hbmXmlFile); - Binding binding = bindMapping( mappingBinder, hbmXmlFile, origin ); - result.add(binding); - }); - return result; - } - - private void marshall(Marshaller marshaller, JaxbEntityMappingsImpl mappings, File hbmXmlFile) { - File mappingXmlFile = new File( - hbmXmlFile.getParentFile(), - hbmXmlFile.getName().replace(".hbm.xml", ".mapping.xml")); + private File inputFolder; + + @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()); + } + + private ServiceRegistry createServiceRegistry() { + StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(); + ssrb.clearSettings(); + ssrb.applySetting(JdbcSettings.ALLOW_METADATA_ON_BOOT, false); + // Choose the H2 dialect by default, make this configurable + ssrb.applySetting(JdbcSettings.DIALECT, H2Dialect.class.getName()); + return ssrb.build(); + } + + private void performTransformation( + List> hbmBindings, + MappingBinder mappingBinder, + ServiceRegistry serviceRegistry) { + Marshaller marshaller = createMarshaller(mappingBinder); + MetadataSources metadataSources = new MetadataSources( serviceRegistry ); + hbmBindings.forEach( metadataSources::addHbmXmlBinding ); + List> transformedBindings = HbmXmlTransformer.transform( + hbmBindings, + (MetadataImplementor) metadataSources.buildMetadata(), + UnsupportedFeatureHandling.ERROR + ); + for (int i = 0; i < hbmBindings.size(); i++) { + Binding hbmBinding = hbmBindings.get( i ); + Binding transformedBinding = transformedBindings.get( i ); + + HbmXmlOrigin origin = (HbmXmlOrigin)hbmBinding.getOrigin(); + File hbmXmlFile = origin.getHbmXmlFile(); + + marshall(marshaller, transformedBinding.getRoot(), hbmXmlFile); + } + } + + private List> getHbmMappings(List hbmXmlFiles, MappingBinder mappingBinder) { + List> result = new ArrayList<>(); + hbmXmlFiles.forEach((hbmXmlFile) -> { + final String fullPath = hbmXmlFile.getAbsolutePath(); + getLog().info("Adding file: '" + fullPath + "' to the list to be transformed."); + Origin origin = new HbmXmlOrigin( hbmXmlFile ); + Binding binding = bindMapping( mappingBinder, hbmXmlFile, origin ); + result.add(binding); + }); + return result; + } + + private void marshall(Marshaller marshaller, JaxbEntityMappingsImpl mappings, File hbmXmlFile) { + File mappingXmlFile = new File( + hbmXmlFile.getParentFile(), + hbmXmlFile.getName().replace(".hbm.xml", ".mapping.xml")); getLog().info("Marshalling file: " + hbmXmlFile.getAbsolutePath() + " into " + mappingXmlFile.getAbsolutePath()); - try { - marshaller.marshal( mappings, mappingXmlFile ); - } - catch (JAXBException e) { - throw new RuntimeException( - "Unable to marshall mapping JAXB representation to file `" + mappingXmlFile.getAbsolutePath() + "`", - e - ); - } - } - - private Binding bindMapping( - MappingBinder mappingBinder, File hbmXmlFile, Origin origin) { - try ( final FileInputStream fileStream = new FileInputStream(hbmXmlFile) ) { - return mappingBinder.bind( fileStream, origin ); - } - catch (IOException e) { - getLog().warn( "Unable to open hbm.xml file `" + hbmXmlFile.getAbsolutePath() + "` for transformation", e ); - return null; - } - } - - private Marshaller createMarshaller(MappingBinder mappingBinder) { - try { - return mappingBinder.mappingJaxbContext().createMarshaller(); - } catch (JAXBException e) { - throw new RuntimeException("Unable to create JAXB Marshaller", e); - } - } - - private List getHbmFiles(File f) { - List result = new ArrayList(); - if (f.isFile()) { - if (f.getName().endsWith("hbm.xml")) { - result.add(f); - } - } else { - for (File child : f.listFiles()) { - result.addAll(getHbmFiles(child)); - } - } - return result; - } - - private class HbmXmlOrigin extends Origin { - - private static final long serialVersionUID = 1L; - - private final File hbmXmlFile; - - public HbmXmlOrigin(File hbmXmlFile) { - super( SourceType.FILE, hbmXmlFile.getAbsolutePath() ); - this.hbmXmlFile = hbmXmlFile; - } - - public File getHbmXmlFile() { - return hbmXmlFile; - } - - } - + try { + marshaller.marshal( mappings, mappingXmlFile ); + } + catch (JAXBException e) { + throw new RuntimeException( + "Unable to marshall mapping JAXB representation to file `" + mappingXmlFile.getAbsolutePath() + "`", + e + ); + } + } + + private Binding bindMapping( + MappingBinder mappingBinder, File hbmXmlFile, Origin origin) { + try ( final FileInputStream fileStream = new FileInputStream(hbmXmlFile) ) { + return mappingBinder.bind( fileStream, origin ); + } + catch (IOException e) { + getLog().warn( "Unable to open hbm.xml file `" + hbmXmlFile.getAbsolutePath() + "` for transformation", e ); + return null; + } + } + + private Marshaller createMarshaller(MappingBinder mappingBinder) { + try { + return mappingBinder.mappingJaxbContext().createMarshaller(); + } + catch (JAXBException e) { + throw new RuntimeException("Unable to create JAXB Marshaller", e); + } + } + + private List getHbmFiles(File f) { + List result = new ArrayList<>(); + if (f.isFile()) { + if (f.getName().endsWith("hbm.xml")) { + result.add(f); + } + } + else { + for (File child : Objects.requireNonNull( f.listFiles() ) ) { + result.addAll(getHbmFiles(child)); + } + } + return result; + } + + private static class HbmXmlOrigin extends Origin { + + @Serial + private static final long serialVersionUID = 1L; + + private final File hbmXmlFile; + + public HbmXmlOrigin(File hbmXmlFile) { + super( SourceType.FILE, hbmXmlFile.getAbsolutePath() ); + this.hbmXmlFile = hbmXmlFile; + } + + public File getHbmXmlFile() { + return hbmXmlFile; + } + + } + }