|
| 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 | +} |
0 commit comments