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 java.util.EnumSet;
20 import java.util.HashMap;
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.plugin.MojoFailureException;
24 import org.hibernate.boot.spi.MetadataImplementor;
25 import org.hibernate.cfg.AvailableSettings;
26 import org.hibernate.engine.config.spi.ConfigurationService;
27 import org.hibernate.service.ServiceRegistry;
28 import org.hibernate.tool.schema.TargetType;
29 import org.hibernate.tool.schema.internal.ExceptionHandlerCollectingImpl;
30 import org.hibernate.tool.schema.internal.exec.ScriptTargetOutputToFile;
31 import org.hibernate.tool.schema.spi.ExecutionOptions;
32 import org.hibernate.tool.schema.spi.SchemaManagementTool;
33 import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator;
34 import org.hibernate.tool.schema.spi.ScriptTargetOutput;
35 import org.hibernate.tool.schema.spi.TargetDescriptor;
39 * Generate/Execute SQL to update the database-schema according to the
40 * configured mappings.
43 * @phase process-classes
45 * @requiresDependencyResolution runtime
47 public class UpdateMojo extends AbstractSchemaMojo
52 * If the specified filename is not absolut, the file will be created
53 * relative to the project build directory
54 * (<code>project.build.directory</code>).
56 * @parameter property="hibernate.schema.update" default-value="update.sql"
59 private String outputFile;
63 public final void execute()
66 MojoExecutionException
68 super.execute(outputFile);
73 ExceptionHandlerCollectingImpl build(MetadataImplementor metadata)
75 MojoExecutionException,
78 final ServiceRegistry service =
79 metadata.getMetadataBuildingOptions().getServiceRegistry();
80 SchemaManagementTool tool = service.getService(SchemaManagementTool.class);
82 final EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
84 targetTypes.add(TargetType.DATABASE);
86 TargetDescriptor target = new TargetDescriptor()
89 public EnumSet<TargetType> getTargetTypes()
95 public ScriptTargetOutput getScriptTargetOutput()
99 .getService(ConfigurationService.class)
101 .get(AvailableSettings.HBM2DDL_CHARSET_NAME);
102 return new ScriptTargetOutputToFile(output, charset);
106 ExecutionOptions options =
107 SchemaManagementToolCoordinator.buildExecutionOptions(
109 .getService(ConfigurationService.class)
111 new ExceptionHandlerCollectingImpl()
114 Map config = options.getConfigurationValues();
116 tool.getSchemaMigrator(config).doMigration(metadata, options, target);