X-Git-Url: http://juplo.de/gitweb/?p=hibernate4-maven-plugin;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fplugins%2Fhibernate%2FCreateMojo.java;h=9a959ec3214a7c2b3446d889261111f0fc098bcd;hp=19078ea89efc313a89269200066bcc3c8272b41c;hb=bec99c73fea6edf073dcf885ef07defee1ed8608;hpb=4940080670944a15916c68fb294e18a6bfef12d5 diff --git a/src/main/java/de/juplo/plugins/hibernate/CreateMojo.java b/src/main/java/de/juplo/plugins/hibernate/CreateMojo.java index 19078ea8..9a959ec3 100644 --- a/src/main/java/de/juplo/plugins/hibernate/CreateMojo.java +++ b/src/main/java/de/juplo/plugins/hibernate/CreateMojo.java @@ -16,16 +16,30 @@ package de.juplo.plugins.hibernate; * limitations under the License. */ +import static de.juplo.plugins.hibernate.AbstractSchemaMojo.EXECUTE; import java.io.File; +import java.security.NoSuchAlgorithmException; +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 create a database-schema that represents the + * configured mappings. * * @goal create * @phase process-classes @@ -34,89 +48,84 @@ import org.hibernate.tool.hbm2ddl.SchemaExport; */ public class CreateMojo extends AbstractSchemaMojo { - /** - * Export the database-schma to the database. - * If set to false, only the SQL-script is created and the - * database is not touched. - * - * @parameter property="hibernate.export.export" default-value="true" - * @since 2.0 - */ - private boolean export; - - /** - * Create the catalog - * If set to false, only the SQL-script is created and the - * database is not touched. - * - * @parameter property=org.hibernate.cfg.AvailableSettings.HBM2DDL_IMPORT_FILES_SQL_EXTRACTOR default-value="false" - * @since 2.0 - */ - private boolean createNamespaces; // TODO handle in configure-Method - /** * Output file. + *

+ * If the specified filename is not absolut, the file will be created + * relative to the project build directory + * (project.build.directory). * - * @parameter property="hibernate.export.schema.filename" default-value="${project.build.directory}/schema.sql" + * @parameter property="hibernate.schema.create" default-value="create.sql" * @since 1.0 */ private String outputFile; - /** - * Delimiter in output-file. - * - * @parameter property="hibernate.export.schema.delimiter" default-value=";" - * @since 1.0 - */ - private String delimiter; - /** - * Format output-file. - * - * @parameter property="hibernate.export.schema.format" default-value="true" - * @since 1.0 - */ - private boolean format; + @Override + public final void execute() + throws + MojoFailureException, + MojoExecutionException + { + try + { + super.execute(new MD5ModificationTracker(buildDirectory, outputFile, getLog())); + } + catch (NoSuchAlgorithmException e) + { + throw new MojoFailureException("Digest-Algorithm MD5 is missing!", e); + } + } @Override - void build(MetadataImplementor metadata) + ExceptionHandlerCollectingImpl build(final MetadataImplementor metadata) 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(new File(outputFile), 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, false, true); + tool.getSchemaMigrator(config).doMigration(metadata, options, target); - for (Object exception : schemaExport.getExceptions()) - getLog().error(exception.toString()); + return handler; } }