X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fplugins%2Fhibernate%2FDropMojo.java;h=2f5927228c026a1872142c0a5b7e75ba8939e344;hb=505d13bdca776dd80f661a52797789b87e97b200;hp=969a21982292bcdf8826c8e9ffa58b3c4b8e96d1;hpb=8e5921c9e76b4540f1d4b75e05e338001145ff6d;p=hibernate4-maven-plugin diff --git a/src/main/java/de/juplo/plugins/hibernate/DropMojo.java b/src/main/java/de/juplo/plugins/hibernate/DropMojo.java index 969a2198..2f592722 100644 --- a/src/main/java/de/juplo/plugins/hibernate/DropMojo.java +++ b/src/main/java/de/juplo/plugins/hibernate/DropMojo.java @@ -17,15 +17,27 @@ package de.juplo.plugins.hibernate; */ 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.tool.hbm2ddl.SchemaExport; +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 drop all tables of a database-schema that represents + * the configured mappings. * * @goal drop * @phase process-classes @@ -41,7 +53,7 @@ public class DropMojo extends AbstractSchemaMojo * relative to the project build directory * (project.build.directory). * - * @parameter property="hibernate.schema.export.drop" default-value="drop.sql" + * @parameter property="hibernate.schema.drop" default-value="drop.sql" * @since 1.0 */ private String outputFile; @@ -58,43 +70,56 @@ public class DropMojo extends AbstractSchemaMojo @Override - void build(MetadataImplementor metadata) + ExceptionHandlerCollectingImpl build( + final MetadataImplementor metadata, + final File output + ) throws MojoExecutionException, MojoFailureException { - SchemaExport schemaExport = new SchemaExport(metadata, createNamespaces); - schemaExport.setDelimiter(delimiter); - schemaExport.setFormat(format); + final ServiceRegistry registry = + metadata.getMetadataBuildingOptions().getServiceRegistry(); + final Map settings = + registry.getService(ConfigurationService.class).getSettings(); + SchemaManagementTool tool = registry.getService(SchemaManagementTool.class); - File output = new File(outputFile); + final EnumSet targetTypes = EnumSet.of(TargetType.SCRIPT); + if ((Boolean)settings.get(EXECUTE)) + targetTypes.add(TargetType.DATABASE); - if (!output.isAbsolute()) + TargetDescriptor target = new TargetDescriptor() { - // Interpret relative file path relative to build directory - output = new File(buildDirectory, outputFile); - getLog().debug("Adjusted relative path, resulting path is " + output.getPath()); - } - - // Ensure that directory path for specified file exists - File outFileParentDir = output.getParentFile(); - if (null != outFileParentDir && !outFileParentDir.exists()) - { - try + @Override + public EnumSet getTargetTypes() { - getLog().info("Creating directory path for output file:" + outFileParentDir.getPath()); - outFileParentDir.mkdirs(); + return targetTypes; } - catch (Exception e) + + @Override + public ScriptTargetOutput getScriptTargetOutput() { - getLog().error("Error creating directory path for output file: " + e.getLocalizedMessage()); + String charset = + (String)settings.get(AvailableSettings.HBM2DDL_CHARSET_NAME); + return new ScriptTargetOutputToFile(output, charset); } - } + }; + + ExceptionHandlerCollectingImpl handler = + new ExceptionHandlerCollectingImpl(); + + ExecutionOptions options = + SchemaManagementToolCoordinator.buildExecutionOptions( + registry + .getService(ConfigurationService.class) + .getSettings(), + handler + ); + + Map config = options.getConfigurationValues(); - schemaExport.setOutputFile(output.getPath()); - schemaExport.execute(false, this.export, true, false); + tool.getSchemaMigrator(config).doMigration(metadata, options, target); - for (Object exception : schemaExport.getExceptions()) - getLog().error(exception.toString()); + return handler; } }