X-Git-Url: http://juplo.de/gitweb/?p=hibernate4-maven-plugin;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fplugins%2Fhibernate%2FUpdateMojo.java;h=09b74a3ea626a9e904e79b92e217ba8eeb32cf24;hp=91d978cf8b8a40e5102e9c8d8482def8f40e6743;hb=4ab00d5dbf8f4263d867726607ce84d4107ef1c6;hpb=9ef60a3f37e89f8a2417e340ef6c6b4a11e6013e diff --git a/src/main/java/de/juplo/plugins/hibernate/UpdateMojo.java b/src/main/java/de/juplo/plugins/hibernate/UpdateMojo.java index 91d978cf..09b74a3e 100644 --- a/src/main/java/de/juplo/plugins/hibernate/UpdateMojo.java +++ b/src/main/java/de/juplo/plugins/hibernate/UpdateMojo.java @@ -16,19 +16,28 @@ package de.juplo.plugins.hibernate; * limitations under the License. */ +import java.util.EnumSet; +import java.util.HashMap; 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.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.SchemaManagementToolCoordinator; +import org.hibernate.tool.schema.spi.ScriptTargetOutput; import org.hibernate.tool.schema.spi.TargetDescriptor; /** - * Goal which extracts the hibernate-mapping-configuration and - * exports an according SQL-database-schema. + * Generate/Execute SQL to update the database-schema according to the + * configured mappings. * * @goal update * @phase process-classes @@ -61,19 +70,47 @@ public class UpdateMojo extends AbstractSchemaMojo @Override - void build( - MetadataImplementor metadata, - ExecutionOptions options, - TargetDescriptor target - ) + ExceptionHandlerCollectingImpl build(MetadataImplementor metadata) throws MojoExecutionException, MojoFailureException { - ServiceRegistry service = + final ServiceRegistry service = metadata.getMetadataBuildingOptions().getServiceRegistry(); SchemaManagementTool tool = service.getService(SchemaManagementTool.class); + final EnumSet targetTypes = EnumSet.of(TargetType.SCRIPT); + if (execute) + targetTypes.add(TargetType.DATABASE); + + TargetDescriptor target = new TargetDescriptor() + { + @Override + public EnumSet getTargetTypes() + { + return targetTypes; + } + + @Override + public ScriptTargetOutput getScriptTargetOutput() + { + String charset + = (String) service + .getService(ConfigurationService.class) + .getSettings() + .get(AvailableSettings.HBM2DDL_CHARSET_NAME); + return new ScriptTargetOutputToFile(output, charset); + } + }; + + ExecutionOptions options = + SchemaManagementToolCoordinator.buildExecutionOptions( + service + .getService(ConfigurationService.class) + .getSettings(), + new ExceptionHandlerCollectingImpl() + ); + Map config = options.getConfigurationValues(); tool.getSchemaMigrator(config).doMigration(metadata, options, target);