44 */
55package org .hibernate .tool .maven ;
66
7- import jakarta .xml .bind .JAXBException ;
8- import jakarta .xml .bind .Marshaller ;
9- import org .apache .maven .plugin .AbstractMojo ;
10- import org .apache .maven .plugins .annotations .Mojo ;
11- import org .apache .maven .plugins .annotations .Parameter ;
12- import org .apache .maven .plugins .annotations .ResolutionScope ;
13- import org .hibernate .boot .MetadataSources ;
14- import org .hibernate .boot .jaxb .Origin ;
15- import org .hibernate .boot .jaxb .SourceType ;
16- import org .hibernate .boot .jaxb .hbm .spi .JaxbHbmHibernateMapping ;
17- import org .hibernate .boot .jaxb .hbm .transform .HbmXmlTransformer ;
18- import org .hibernate .boot .jaxb .hbm .transform .UnsupportedFeatureHandling ;
19- import org .hibernate .boot .jaxb .internal .MappingBinder ;
20- import org .hibernate .boot .jaxb .mapping .spi .JaxbEntityMappingsImpl ;
21- import org .hibernate .boot .jaxb .spi .Binding ;
22- import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
23- import org .hibernate .boot .spi .MetadataImplementor ;
24- import org .hibernate .cfg .JdbcSettings ;
25- import org .hibernate .dialect .H2Dialect ;
26- import org .hibernate .service .ServiceRegistry ;
7+ import static org .apache .maven .plugins .annotations .LifecyclePhase .GENERATE_RESOURCES ;
278
289import java .io .File ;
29- import java .io . FileInputStream ;
30- import java .io . IOException ;
31- import java .io . Serial ;
10+ import java .net . MalformedURLException ;
11+ import java .net . URL ;
12+ import java .net . URLClassLoader ;
3213import java .util .ArrayList ;
3314import java .util .List ;
3415import java .util .Objects ;
3516
36- import static org .apache .maven .plugins .annotations .LifecyclePhase .GENERATE_RESOURCES ;
17+ import org .apache .maven .artifact .DependencyResolutionRequiredException ;
18+ import org .apache .maven .plugin .AbstractMojo ;
19+ import org .apache .maven .plugins .annotations .Mojo ;
20+ import org .apache .maven .plugins .annotations .Parameter ;
21+ import org .apache .maven .plugins .annotations .ResolutionScope ;
22+ import org .apache .maven .project .MavenProject ;
23+
24+ import org .hibernate .tool .reveng .internal .export .mapping .MappingExporter ;
3725
3826@ Mojo (
39- name = "hbm2orm" ,
40- defaultPhase = GENERATE_RESOURCES ,
41- requiresDependencyResolution = ResolutionScope .RUNTIME )
27+ name = "hbm2orm" ,
28+ defaultPhase = GENERATE_RESOURCES ,
29+ requiresDependencyResolution = ResolutionScope .RUNTIME )
4230public class TransformHbmMojo extends AbstractMojo {
4331
4432 @ Parameter (defaultValue = "${project.basedir}/src/main/resources" )
4533 private File inputFolder ;
4634
47- @ Override
48- public void execute () {
49- MappingBinder mappingBinder = new MappingBinder (
50- MappingBinder .class .getClassLoader ()::getResourceAsStream ,
51- UnsupportedFeatureHandling .ERROR );
52- List <File > hbmFiles = getHbmFiles (inputFolder );
53- List <Binding <JaxbHbmHibernateMapping >> hbmMappings = getHbmMappings (hbmFiles , mappingBinder );
54- performTransformation (hbmMappings , mappingBinder , createServiceRegistry ());
55- }
56-
57- private ServiceRegistry createServiceRegistry () {
58- StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder ();
59- ssrb .clearSettings ();
60- ssrb .applySetting (JdbcSettings .ALLOW_METADATA_ON_BOOT , false );
61- // Choose the H2 dialect by default, make this configurable
62- ssrb .applySetting (JdbcSettings .DIALECT , H2Dialect .class .getName ());
63- return ssrb .build ();
64- }
35+ @ Parameter (defaultValue = "true" )
36+ private boolean format ;
6537
66- private void performTransformation (
67- List <Binding <JaxbHbmHibernateMapping >> hbmBindings ,
68- MappingBinder mappingBinder ,
69- ServiceRegistry serviceRegistry ) {
70- Marshaller marshaller = createMarshaller (mappingBinder );
71- MetadataSources metadataSources = new MetadataSources ( serviceRegistry );
72- hbmBindings .forEach ( metadataSources ::addHbmXmlBinding );
73- List <Binding <JaxbEntityMappingsImpl >> transformedBindings = HbmXmlTransformer .transform (
74- hbmBindings ,
75- (MetadataImplementor ) metadataSources .buildMetadata (),
76- UnsupportedFeatureHandling .ERROR
77- );
78- for (int i = 0 ; i < hbmBindings .size (); i ++) {
79- Binding <JaxbHbmHibernateMapping > hbmBinding = hbmBindings .get ( i );
80- Binding <JaxbEntityMappingsImpl > transformedBinding = transformedBindings .get ( i );
38+ @ Parameter (defaultValue = "${project}" , readonly = true , required = true )
39+ private MavenProject project ;
8140
82- HbmXmlOrigin origin = (HbmXmlOrigin )hbmBinding .getOrigin ();
83- File hbmXmlFile = origin .getHbmXmlFile ();
84-
85- marshall (marshaller , transformedBinding .getRoot (), hbmXmlFile );
86- }
87- }
88-
89- private List <Binding <JaxbHbmHibernateMapping >> getHbmMappings (List <File > hbmXmlFiles , MappingBinder mappingBinder ) {
90- List <Binding <JaxbHbmHibernateMapping >> result = new ArrayList <>();
91- hbmXmlFiles .forEach ((hbmXmlFile ) -> {
92- final String fullPath = hbmXmlFile .getAbsolutePath ();
93- getLog ().info ("Adding file: '" + fullPath + "' to the list to be transformed." );
94- Origin origin = new HbmXmlOrigin ( hbmXmlFile );
95- Binding <JaxbHbmHibernateMapping > binding = bindMapping ( mappingBinder , hbmXmlFile , origin );
96- result .add (binding );
97- });
98- return result ;
99- }
100-
101- private void marshall (Marshaller marshaller , JaxbEntityMappingsImpl mappings , File hbmXmlFile ) {
102- File mappingXmlFile = new File (
103- hbmXmlFile .getParentFile (),
104- hbmXmlFile .getName ().replace (".hbm.xml" , ".mapping.xml" ));
105- getLog ().info ("Marshalling file: " + hbmXmlFile .getAbsolutePath () + " into " + mappingXmlFile .getAbsolutePath ());
106- try {
107- marshaller .marshal ( mappings , mappingXmlFile );
108- }
109- catch (JAXBException e ) {
110- throw new RuntimeException (
111- "Unable to marshall mapping JAXB representation to file `" + mappingXmlFile .getAbsolutePath () + "`" ,
112- e
113- );
114- }
115- }
116-
117- private Binding <JaxbHbmHibernateMapping > bindMapping (
118- MappingBinder mappingBinder , File hbmXmlFile , Origin origin ) {
119- try ( final FileInputStream fileStream = new FileInputStream (hbmXmlFile ) ) {
120- return mappingBinder .bind ( fileStream , origin );
121- }
122- catch (IOException e ) {
123- getLog ().warn ( "Unable to open hbm.xml file `" + hbmXmlFile .getAbsolutePath () + "` for transformation" , e );
124- return null ;
125- }
126- }
127-
128- private Marshaller createMarshaller (MappingBinder mappingBinder ) {
41+ @ Override
42+ public void execute () {
43+ ClassLoader original = Thread .currentThread ().getContextClassLoader ();
12944 try {
130- return mappingBinder .mappingJaxbContext ().createMarshaller ();
45+ Thread .currentThread ().setContextClassLoader (createClassLoader (original ));
46+ getLog ().info ("Starting " + this .getClass ().getSimpleName () + "..." );
47+ MappingExporter mappingExporter = new MappingExporter ();
48+ mappingExporter .setHbmFiles (getHbmFiles (inputFolder ));
49+ mappingExporter .setFormatResult (format );
50+ mappingExporter .start ();
51+ getLog ().info ("Finished " + this .getClass ().getSimpleName () + "!" );
13152 }
132- catch ( JAXBException e ) {
133- throw new RuntimeException ( "Unable to create JAXB Marshaller" , e );
53+ finally {
54+ Thread . currentThread (). setContextClassLoader ( original );
13455 }
13556 }
13657
@@ -149,22 +70,17 @@ private List<File> getHbmFiles(File f) {
14970 return result ;
15071 }
15172
152- private static class HbmXmlOrigin extends Origin {
153-
154- @ Serial
155- private static final long serialVersionUID = 1L ;
156-
157- private final File hbmXmlFile ;
158-
159- public HbmXmlOrigin (File hbmXmlFile ) {
160- super ( SourceType .FILE , hbmXmlFile .getAbsolutePath () );
161- this .hbmXmlFile = hbmXmlFile ;
73+ private ClassLoader createClassLoader (ClassLoader parent ) {
74+ ArrayList <URL > urls = new ArrayList <>();
75+ try {
76+ for (String cpe : project .getRuntimeClasspathElements ()) {
77+ urls .add (new File (cpe ).toURI ().toURL ());
78+ }
16279 }
163-
164- public File getHbmXmlFile () {
165- return hbmXmlFile ;
80+ catch (DependencyResolutionRequiredException | MalformedURLException e ) {
81+ throw new RuntimeException ("Problem while constructing project classloader" , e );
16682 }
167-
83+ return new URLClassLoader ( urls . toArray ( new URL [ 0 ]), parent );
16884 }
16985
17086}
0 commit comments