1 package de.juplo.plugins.hibernate;
4 * Copyright 2001-2005 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import static de.juplo.plugins.hibernate.AbstractSchemaMojo.EXECUTE;
21 import java.security.NoSuchAlgorithmException;
22 import java.util.EnumSet;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.MojoFailureException;
26 import org.hibernate.boot.spi.MetadataImplementor;
27 import org.hibernate.cfg.AvailableSettings;
28 import org.hibernate.engine.config.spi.ConfigurationService;
29 import org.hibernate.service.ServiceRegistry;
30 import org.hibernate.tool.schema.TargetType;
31 import org.hibernate.tool.schema.internal.ExceptionHandlerCollectingImpl;
32 import org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToFile;
33 import org.hibernate.tool.schema.spi.ExecutionOptions;
34 import org.hibernate.tool.schema.spi.SchemaManagementTool;
35 import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator;
36 import org.hibernate.tool.schema.spi.ScriptTargetOutput;
37 import org.hibernate.tool.schema.spi.TargetDescriptor;
41 * Generate/Execute SQL to create a database-schema that represents the
42 * configured mappings.
45 * @phase process-classes
47 * @requiresDependencyResolution runtime
49 public class CreateMojo extends AbstractSchemaMojo
54 * If the specified filename is not absolut, the file will be created
55 * relative to the project build directory
56 * (<code>project.build.directory</code>).
58 * @parameter property="hibernate.schema.create" default-value="create.sql"
61 private String outputFile;
65 public final void execute()
68 MojoExecutionException
72 super.execute(new MD5ModificationTracker(buildDirectory, outputFile, getLog()));
74 catch (NoSuchAlgorithmException e)
76 throw new MojoFailureException("Digest-Algorithm MD5 is missing!", e);
82 ExceptionHandlerCollectingImpl build(final MetadataImplementor metadata)
84 MojoExecutionException,
87 final ServiceRegistry registry =
88 metadata.getMetadataBuildingOptions().getServiceRegistry();
90 registry.getService(ConfigurationService.class).getSettings();
91 SchemaManagementTool tool = registry.getService(SchemaManagementTool.class);
93 final EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
94 if ((Boolean)settings.get(EXECUTE))
95 targetTypes.add(TargetType.DATABASE);
97 TargetDescriptor target = new TargetDescriptor()
100 public EnumSet<TargetType> getTargetTypes()
106 public ScriptTargetOutput getScriptTargetOutput()
109 (String)settings.get(AvailableSettings.HBM2DDL_CHARSET_NAME);
110 return new ScriptTargetOutputToFile(new File(outputFile), charset);
114 ExceptionHandlerCollectingImpl handler =
115 new ExceptionHandlerCollectingImpl();
117 ExecutionOptions options =
118 SchemaManagementToolCoordinator.buildExecutionOptions(
120 .getService(ConfigurationService.class)
125 Map config = options.getConfigurationValues();
127 tool.getSchemaMigrator(config).doMigration(metadata, options, target);