2020import static org .apache .maven .plugins .annotations .LifecyclePhase .GENERATE_RESOURCES ;
2121
2222import java .io .File ;
23- import java .io .FileInputStream ;
24- import java .io .IOException ;
25- import java .io .Serial ;
2623import java .net .MalformedURLException ;
2724import java .net .URL ;
2825import java .net .URLClassLoader ;
3633import org .apache .maven .plugins .annotations .Parameter ;
3734import org .apache .maven .plugins .annotations .ResolutionScope ;
3835import org .apache .maven .project .MavenProject ;
39- import org .hibernate .boot .MetadataSources ;
40- import org .hibernate .boot .jaxb .Origin ;
41- import org .hibernate .boot .jaxb .SourceType ;
42- import org .hibernate .boot .jaxb .hbm .spi .JaxbHbmHibernateMapping ;
43- import org .hibernate .boot .jaxb .hbm .transform .HbmXmlTransformer ;
44- import org .hibernate .boot .jaxb .hbm .transform .UnsupportedFeatureHandling ;
45- import org .hibernate .boot .jaxb .internal .MappingBinder ;
46- import org .hibernate .boot .jaxb .mapping .spi .JaxbEntityMappingsImpl ;
47- import org .hibernate .boot .jaxb .spi .Binding ;
48- import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
49- import org .hibernate .boot .spi .MetadataImplementor ;
50- import org .hibernate .cfg .JdbcSettings ;
51- import org .hibernate .dialect .H2Dialect ;
52- import org .hibernate .service .ServiceRegistry ;
5336
54- import jakarta .xml .bind .JAXBException ;
55- import jakarta .xml .bind .Marshaller ;
56- import org .hibernate .tool .api .xml .XMLPrettyPrinter ;
37+ import org .hibernate .tool .internal .export .mapping .MappingExporter ;
5738
5839@ Mojo (
5940 name = "hbm2orm" ,
@@ -76,105 +57,16 @@ public void execute() {
7657 try {
7758 Thread .currentThread ().setContextClassLoader (createClassLoader (original ));
7859 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 ());
60+ MappingExporter mappingExporter = new MappingExporter ();
61+ mappingExporter .setHbmFiles (getHbmFiles (inputFolder ));
62+ mappingExporter .setFormatResult (format );
63+ mappingExporter .start ();
8564 getLog ().info ("Finished " + this .getClass ().getSimpleName () + "!" );
8665 } finally {
8766 Thread .currentThread ().setContextClassLoader (original );
8867 }
8968 }
9069
91- private ServiceRegistry createServiceRegistry () {
92- StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder ();
93- ssrb .clearSettings ();
94- ssrb .applySetting (JdbcSettings .ALLOW_METADATA_ON_BOOT , false );
95- // Choose the H2 dialect by default, make this configurable
96- ssrb .applySetting (JdbcSettings .DIALECT , H2Dialect .class .getName ());
97- return ssrb .build ();
98- }
99-
100- private void performTransformation (
101- List <Binding <JaxbHbmHibernateMapping >> hbmBindings ,
102- MappingBinder mappingBinder ,
103- ServiceRegistry serviceRegistry ) {
104- Marshaller marshaller = createMarshaller (mappingBinder );
105- MetadataSources metadataSources = new MetadataSources ( serviceRegistry );
106- hbmBindings .forEach ( metadataSources ::addHbmXmlBinding );
107- List <Binding <JaxbEntityMappingsImpl >> transformedBindings = HbmXmlTransformer .transform (
108- hbmBindings ,
109- (MetadataImplementor ) metadataSources .buildMetadata (),
110- UnsupportedFeatureHandling .ERROR
111- );
112- for (int i = 0 ; i < hbmBindings .size (); i ++) {
113- Binding <JaxbHbmHibernateMapping > hbmBinding = hbmBindings .get ( i );
114- Binding <JaxbEntityMappingsImpl > transformedBinding = transformedBindings .get ( i );
115-
116- HbmXmlOrigin origin = (HbmXmlOrigin )hbmBinding .getOrigin ();
117- File hbmXmlFile = origin .getHbmXmlFile ();
118-
119- marshall (marshaller , transformedBinding .getRoot (), hbmXmlFile );
120- }
121- }
122-
123- private List <Binding <JaxbHbmHibernateMapping >> getHbmMappings (List <File > hbmXmlFiles , MappingBinder mappingBinder ) {
124- List <Binding <JaxbHbmHibernateMapping >> result = new ArrayList <>();
125- hbmXmlFiles .forEach ((hbmXmlFile ) -> {
126- final String fullPath = hbmXmlFile .getAbsolutePath ();
127- getLog ().info ("Adding file: '" + fullPath + "' to the list to be transformed." );
128- Origin origin = new HbmXmlOrigin ( hbmXmlFile );
129- Binding <JaxbHbmHibernateMapping > binding = bindMapping ( mappingBinder , hbmXmlFile , origin );
130- result .add (binding );
131- });
132- return result ;
133- }
134-
135- private void marshall (Marshaller marshaller , JaxbEntityMappingsImpl mappings , File hbmXmlFile ) {
136- File mappingXmlFile = new File (
137- hbmXmlFile .getParentFile (),
138- hbmXmlFile .getName ().replace (".hbm.xml" , ".mapping.xml" ));
139- getLog ().info ("Marshalling file: " + hbmXmlFile .getAbsolutePath () + " into " + mappingXmlFile .getAbsolutePath ());
140- try {
141- marshaller .marshal ( mappings , mappingXmlFile );
142- if (format ) {
143- XMLPrettyPrinter .prettyPrintFile (mappingXmlFile );
144- }
145- }
146- catch (JAXBException e ) {
147- throw new RuntimeException (
148- "Unable to marshall mapping JAXB representation to file `" + mappingXmlFile .getAbsolutePath () + "`" ,
149- e );
150- }
151- catch (IOException e ) {
152- throw new RuntimeException (
153- "Unable to format XML file `" + mappingXmlFile .getAbsolutePath () + "`" ,
154- e );
155- }
156- }
157-
158- private Binding <JaxbHbmHibernateMapping > bindMapping (
159- MappingBinder mappingBinder , File hbmXmlFile , Origin origin ) {
160- try ( final FileInputStream fileStream = new FileInputStream (hbmXmlFile ) ) {
161- return mappingBinder .bind ( fileStream , origin );
162- }
163- catch (IOException e ) {
164- getLog ().warn ( "Unable to open hbm.xml file `" + hbmXmlFile .getAbsolutePath () + "` for transformation" , e );
165- return null ;
166- }
167- }
168-
169- private Marshaller createMarshaller (MappingBinder mappingBinder ) {
170- try {
171- return mappingBinder .mappingJaxbContext ().createMarshaller ();
172- }
173- catch (JAXBException e ) {
174- throw new RuntimeException ("Unable to create JAXB Marshaller" , e );
175- }
176- }
177-
17870 private List <File > getHbmFiles (File f ) {
17971 List <File > result = new ArrayList <>();
18072 if (f .isFile ()) {
@@ -202,22 +94,4 @@ private ClassLoader createClassLoader(ClassLoader parent) {
20294 return new URLClassLoader (urls .toArray (new URL [0 ]), parent );
20395 }
20496
205- private static class HbmXmlOrigin extends Origin {
206-
207- @ Serial
208- private static final long serialVersionUID = 1L ;
209-
210- private final File hbmXmlFile ;
211-
212- public HbmXmlOrigin (File hbmXmlFile ) {
213- super ( SourceType .FILE , hbmXmlFile .getAbsolutePath () );
214- this .hbmXmlFile = hbmXmlFile ;
215- }
216-
217- public File getHbmXmlFile () {
218- return hbmXmlFile ;
219- }
220-
221- }
222-
22397}
0 commit comments