WIP
authorKai Moritz <kai@juplo.de>
Mon, 14 Nov 2016 18:28:47 +0000 (19:28 +0100)
committerKai Moritz <kai@juplo.de>
Mon, 14 Nov 2016 18:28:47 +0000 (19:28 +0100)
src/main/java/de/juplo/plugins/hibernate/AbstractSchemaMojo.java
src/main/java/de/juplo/plugins/hibernate/CreateMojo.java
src/main/java/de/juplo/plugins/hibernate/DropMojo.java
src/main/java/de/juplo/plugins/hibernate/UpdateMojo.java
src/main/java/de/juplo/plugins/hibernate/ValidateMojo.java

index ca6b935..71e1250 100644 (file)
@@ -687,7 +687,7 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
       {
         thread.setContextClassLoader(classLoader);
         ExceptionHandlerCollectingImpl handler =
-            build((MetadataImplementor)metadataBuilder.build());
+            build((MetadataImplementor)metadataBuilder.build(), output);
         if (handler.getExceptions().size() > 0)
         {
           StringBuilder builder = new StringBuilder();
@@ -738,7 +738,10 @@ public abstract class AbstractSchemaMojo extends AbstractMojo
   }
 
 
-  abstract ExceptionHandlerCollectingImpl build(MetadataImplementor metadata)
+  abstract ExceptionHandlerCollectingImpl build(
+      MetadataImplementor metadata,
+      File file
+      )
     throws
       MojoFailureException,
       MojoExecutionException;
index cdba7a4..a6c2ba4 100644 (file)
@@ -16,16 +16,23 @@ package de.juplo.plugins.hibernate;
  * limitations under the License.
  */
 
+import static de.juplo.plugins.hibernate.AbstractSchemaMojo.EXECUTE;
+import java.io.File;
+import java.util.EnumSet;
 import java.util.Map;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.hibernate.boot.spi.MetadataImplementor;
+import org.hibernate.cfg.AvailableSettings;
+import org.hibernate.engine.config.spi.ConfigurationService;
 import org.hibernate.service.ServiceRegistry;
-import org.hibernate.tool.schema.SourceType;
+import org.hibernate.tool.schema.TargetType;
+import org.hibernate.tool.schema.internal.ExceptionHandlerCollectingImpl;
+import org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToFile;
 import org.hibernate.tool.schema.spi.ExecutionOptions;
 import org.hibernate.tool.schema.spi.SchemaManagementTool;
-import org.hibernate.tool.schema.spi.ScriptSourceInput;
-import org.hibernate.tool.schema.spi.SourceDescriptor;
+import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator;
+import org.hibernate.tool.schema.spi.ScriptTargetOutput;
 import org.hibernate.tool.schema.spi.TargetDescriptor;
 
 
@@ -64,35 +71,56 @@ public class CreateMojo extends AbstractSchemaMojo
 
 
   @Override
-  void build(
-      MetadataImplementor metadata,
-      ExecutionOptions options,
-      TargetDescriptor target
+  ExceptionHandlerCollectingImpl build(
+      final MetadataImplementor metadata,
+      final File output
       )
       throws
         MojoExecutionException,
         MojoFailureException
   {
-    ServiceRegistry service =
+    final ServiceRegistry registry =
         metadata.getMetadataBuildingOptions().getServiceRegistry();
-    SchemaManagementTool tool = service.getService(SchemaManagementTool.class);
+    final Map settings = 
+        registry.getService(ConfigurationService.class).getSettings();
+    SchemaManagementTool tool = registry.getService(SchemaManagementTool.class);
 
-    Map config = options.getConfigurationValues();
-    SourceDescriptor source = new SourceDescriptor()
+    final EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
+    if ((Boolean)settings.get(EXECUTE))
+      targetTypes.add(TargetType.DATABASE);
+
+    TargetDescriptor target = new TargetDescriptor()
     {
-         @Override
-      public SourceType getSourceType()
+      @Override
+      public EnumSet<TargetType> getTargetTypes()
       {
-        return SourceType.METADATA;
+        return targetTypes;
       }
 
       @Override
-      public ScriptSourceInput getScriptSourceInput()
+      public ScriptTargetOutput getScriptTargetOutput()
       {
-        return null;
+        String charset =
+            (String)settings.get(AvailableSettings.HBM2DDL_CHARSET_NAME);
+        return new ScriptTargetOutputToFile(output, charset);
       }
     };
 
-    tool.getSchemaCreator(config).doCreation(metadata, options, source, target);
+    ExceptionHandlerCollectingImpl handler =
+        new ExceptionHandlerCollectingImpl();
+
+    ExecutionOptions options =
+        SchemaManagementToolCoordinator.buildExecutionOptions(
+            registry
+                .getService(ConfigurationService.class)
+                .getSettings(),
+            handler
+            );
+
+    Map config = options.getConfigurationValues();
+
+    tool.getSchemaMigrator(config).doMigration(metadata, options, target);
+
+    return handler;
   }
 }
index 4037cff..2f59272 100644 (file)
@@ -16,16 +16,22 @@ package de.juplo.plugins.hibernate;
  * limitations under the License.
  */
 
+import java.io.File;
+import java.util.EnumSet;
 import java.util.Map;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.hibernate.boot.spi.MetadataImplementor;
+import org.hibernate.cfg.AvailableSettings;
+import org.hibernate.engine.config.spi.ConfigurationService;
 import org.hibernate.service.ServiceRegistry;
-import org.hibernate.tool.schema.SourceType;
+import org.hibernate.tool.schema.TargetType;
+import org.hibernate.tool.schema.internal.ExceptionHandlerCollectingImpl;
+import org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToFile;
 import org.hibernate.tool.schema.spi.ExecutionOptions;
 import org.hibernate.tool.schema.spi.SchemaManagementTool;
-import org.hibernate.tool.schema.spi.ScriptSourceInput;
-import org.hibernate.tool.schema.spi.SourceDescriptor;
+import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator;
+import org.hibernate.tool.schema.spi.ScriptTargetOutput;
 import org.hibernate.tool.schema.spi.TargetDescriptor;
 
 
@@ -64,35 +70,56 @@ public class DropMojo extends AbstractSchemaMojo
 
 
   @Override
-  void build(
-      MetadataImplementor metadata,
-      ExecutionOptions options,
-      TargetDescriptor target
+  ExceptionHandlerCollectingImpl build(
+      final MetadataImplementor metadata,
+      final File output
       )
       throws
         MojoExecutionException,
         MojoFailureException
   {
-    ServiceRegistry service =
+    final ServiceRegistry registry =
         metadata.getMetadataBuildingOptions().getServiceRegistry();
-    SchemaManagementTool tool = service.getService(SchemaManagementTool.class);
+    final Map settings = 
+        registry.getService(ConfigurationService.class).getSettings();
+    SchemaManagementTool tool = registry.getService(SchemaManagementTool.class);
 
-    Map config = options.getConfigurationValues();
-    SourceDescriptor source = new SourceDescriptor()
+    final EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
+    if ((Boolean)settings.get(EXECUTE))
+      targetTypes.add(TargetType.DATABASE);
+
+    TargetDescriptor target = new TargetDescriptor()
     {
-         @Override
-      public SourceType getSourceType()
+      @Override
+      public EnumSet<TargetType> getTargetTypes()
       {
-        return SourceType.METADATA;
+        return targetTypes;
       }
 
       @Override
-      public ScriptSourceInput getScriptSourceInput()
+      public ScriptTargetOutput getScriptTargetOutput()
       {
-        return null;
+        String charset =
+            (String)settings.get(AvailableSettings.HBM2DDL_CHARSET_NAME);
+        return new ScriptTargetOutputToFile(output, charset);
       }
     };
 
-    tool.getSchemaDropper(config).doDrop(metadata, options, source, target);
+    ExceptionHandlerCollectingImpl handler =
+        new ExceptionHandlerCollectingImpl();
+
+    ExecutionOptions options =
+        SchemaManagementToolCoordinator.buildExecutionOptions(
+            registry
+                .getService(ConfigurationService.class)
+                .getSettings(),
+            handler
+            );
+
+    Map config = options.getConfigurationValues();
+
+    tool.getSchemaMigrator(config).doMigration(metadata, options, target);
+
+    return handler;
   }
 }
index 09b74a3..6d9e62e 100644 (file)
@@ -16,8 +16,8 @@ package de.juplo.plugins.hibernate;
  * limitations under the License.
  */
 
+import java.io.File;
 import java.util.EnumSet;
-import java.util.HashMap;
 import java.util.Map;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
@@ -70,17 +70,22 @@ public class UpdateMojo extends AbstractSchemaMojo
 
 
   @Override
-  ExceptionHandlerCollectingImpl build(MetadataImplementor metadata)
+  ExceptionHandlerCollectingImpl build(
+      final MetadataImplementor metadata,
+      final File output
+      )
       throws
         MojoExecutionException,
         MojoFailureException
   {
-    final ServiceRegistry service =
+    final ServiceRegistry registry =
         metadata.getMetadataBuildingOptions().getServiceRegistry();
-    SchemaManagementTool tool = service.getService(SchemaManagementTool.class);
+    final Map settings = 
+        registry.getService(ConfigurationService.class).getSettings();
+    SchemaManagementTool tool = registry.getService(SchemaManagementTool.class);
 
     final EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
-    if (execute)
+    if ((Boolean)settings.get(EXECUTE))
       targetTypes.add(TargetType.DATABASE);
 
     TargetDescriptor target = new TargetDescriptor()
@@ -94,25 +99,27 @@ public class UpdateMojo extends AbstractSchemaMojo
       @Override
       public ScriptTargetOutput getScriptTargetOutput()
       {
-        String charset
-            = (String) service
-            .getService(ConfigurationService.class)
-            .getSettings()
-            .get(AvailableSettings.HBM2DDL_CHARSET_NAME);
+        String charset =
+            (String)settings.get(AvailableSettings.HBM2DDL_CHARSET_NAME);
         return new ScriptTargetOutputToFile(output, charset);
       }
     };
 
+    ExceptionHandlerCollectingImpl handler =
+        new ExceptionHandlerCollectingImpl();
+
     ExecutionOptions options =
         SchemaManagementToolCoordinator.buildExecutionOptions(
-            service
+            registry
                 .getService(ConfigurationService.class)
                 .getSettings(),
-            new ExceptionHandlerCollectingImpl()
+            handler
             );
 
     Map config = options.getConfigurationValues();
 
     tool.getSchemaMigrator(config).doMigration(metadata, options, target);
+
+    return handler;
   }
 }
index ed31fdd..49a40c3 100644 (file)
@@ -22,9 +22,7 @@ import org.apache.maven.plugin.MojoFailureException;
 import org.hibernate.boot.spi.MetadataImplementor;
 import org.hibernate.service.ServiceRegistry;
 import org.hibernate.tool.schema.internal.ExceptionHandlerCollectingImpl;
-import org.hibernate.tool.schema.spi.ExecutionOptions;
 import org.hibernate.tool.schema.spi.SchemaManagementTool;
-import org.hibernate.tool.schema.spi.TargetDescriptor;
 
 
 /**