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.util.EnumSet;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.plugin.MojoFailureException;
25 import org.hibernate.boot.spi.MetadataImplementor;
26 import org.hibernate.cfg.AvailableSettings;
27 import org.hibernate.engine.config.spi.ConfigurationService;
28 import org.hibernate.service.ServiceRegistry;
29 import org.hibernate.tool.schema.TargetType;
30 import org.hibernate.tool.schema.internal.ExceptionHandlerCollectingImpl;
31 import org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToFile;
32 import org.hibernate.tool.schema.spi.ExecutionOptions;
33 import org.hibernate.tool.schema.spi.SchemaManagementTool;
34 import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator;
35 import org.hibernate.tool.schema.spi.ScriptTargetOutput;
36 import org.hibernate.tool.schema.spi.TargetDescriptor;
40 * Generate/Execute SQL to create a database-schema that represents the
41 * configured mappings.
44 * @phase process-classes
46 * @requiresDependencyResolution runtime
48 public class CreateMojo extends AbstractSchemaMojo
53 * If the specified filename is not absolut, the file will be created
54 * relative to the project build directory
55 * (<code>project.build.directory</code>).
57 * @parameter property="hibernate.schema.create" default-value="create.sql"
60 private String outputFile;
64 public final void execute()
67 MojoExecutionException
69 super.execute(outputFile);
74 ExceptionHandlerCollectingImpl build(
75 final MetadataImplementor metadata,
79 MojoExecutionException,
82 final ServiceRegistry registry =
83 metadata.getMetadataBuildingOptions().getServiceRegistry();
85 registry.getService(ConfigurationService.class).getSettings();
86 SchemaManagementTool tool = registry.getService(SchemaManagementTool.class);
88 final EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
89 if ((Boolean)settings.get(EXECUTE))
90 targetTypes.add(TargetType.DATABASE);
92 TargetDescriptor target = new TargetDescriptor()
95 public EnumSet<TargetType> getTargetTypes()
101 public ScriptTargetOutput getScriptTargetOutput()
104 (String)settings.get(AvailableSettings.HBM2DDL_CHARSET_NAME);
105 return new ScriptTargetOutputToFile(output, charset);
109 ExceptionHandlerCollectingImpl handler =
110 new ExceptionHandlerCollectingImpl();
112 ExecutionOptions options =
113 SchemaManagementToolCoordinator.buildExecutionOptions(
115 .getService(ConfigurationService.class)
120 Map config = options.getConfigurationValues();
122 tool.getSchemaMigrator(config).doMigration(metadata, options, target);