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.
20 import java.util.EnumSet;
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(
74 final MetadataImplementor metadata,
78 MojoExecutionException,
81 final ServiceRegistry registry =
82 metadata.getMetadataBuildingOptions().getServiceRegistry();
84 registry.getService(ConfigurationService.class).getSettings();
85 SchemaManagementTool tool = registry.getService(SchemaManagementTool.class);
87 final EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
88 if ((Boolean)settings.get(EXECUTE))
89 targetTypes.add(TargetType.DATABASE);
91 TargetDescriptor target = new TargetDescriptor()
94 public EnumSet<TargetType> getTargetTypes()
100 public ScriptTargetOutput getScriptTargetOutput()
103 (String)settings.get(AvailableSettings.HBM2DDL_CHARSET_NAME);
104 return new ScriptTargetOutputToFile(output, charset);
108 ExceptionHandlerCollectingImpl handler =
109 new ExceptionHandlerCollectingImpl();
111 ExecutionOptions options =
112 SchemaManagementToolCoordinator.buildExecutionOptions(
114 .getService(ConfigurationService.class)
119 Map config = options.getConfigurationValues();
121 tool.getSchemaMigrator(config).doMigration(metadata, options, target);