From: Kai Moritz Date: Mon, 14 Nov 2016 18:28:47 +0000 (+0100) Subject: WIP X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=505d13bdca776dd80f661a52797789b87e97b200;p=hibernate4-maven-plugin WIP --- diff --git a/src/main/java/de/juplo/plugins/hibernate/AbstractSchemaMojo.java b/src/main/java/de/juplo/plugins/hibernate/AbstractSchemaMojo.java index ca6b9355..71e1250f 100644 --- a/src/main/java/de/juplo/plugins/hibernate/AbstractSchemaMojo.java +++ b/src/main/java/de/juplo/plugins/hibernate/AbstractSchemaMojo.java @@ -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; diff --git a/src/main/java/de/juplo/plugins/hibernate/CreateMojo.java b/src/main/java/de/juplo/plugins/hibernate/CreateMojo.java index cdba7a43..a6c2ba42 100644 --- a/src/main/java/de/juplo/plugins/hibernate/CreateMojo.java +++ b/src/main/java/de/juplo/plugins/hibernate/CreateMojo.java @@ -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 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 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; } } diff --git a/src/main/java/de/juplo/plugins/hibernate/DropMojo.java b/src/main/java/de/juplo/plugins/hibernate/DropMojo.java index 4037cff3..2f592722 100644 --- a/src/main/java/de/juplo/plugins/hibernate/DropMojo.java +++ b/src/main/java/de/juplo/plugins/hibernate/DropMojo.java @@ -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 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 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; } } diff --git a/src/main/java/de/juplo/plugins/hibernate/UpdateMojo.java b/src/main/java/de/juplo/plugins/hibernate/UpdateMojo.java index 09b74a3e..6d9e62ef 100644 --- a/src/main/java/de/juplo/plugins/hibernate/UpdateMojo.java +++ b/src/main/java/de/juplo/plugins/hibernate/UpdateMojo.java @@ -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 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; } } diff --git a/src/main/java/de/juplo/plugins/hibernate/ValidateMojo.java b/src/main/java/de/juplo/plugins/hibernate/ValidateMojo.java index ed31fdd9..49a40c36 100644 --- a/src/main/java/de/juplo/plugins/hibernate/ValidateMojo.java +++ b/src/main/java/de/juplo/plugins/hibernate/ValidateMojo.java @@ -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; /**