09b74a3ea626a9e904e79b92e217ba8eeb32cf24
[hibernate4-maven-plugin] / src / main / java / de / juplo / plugins / hibernate / UpdateMojo.java
1 package de.juplo.plugins.hibernate;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 import java.util.EnumSet;
20 import java.util.HashMap;
21 import java.util.Map;
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;
36
37
38 /**
39  * Generate/Execute SQL to update the database-schema according to the
40  * configured mappings.
41  *
42  * @goal update
43  * @phase process-classes
44  * @threadSafe
45  * @requiresDependencyResolution runtime
46  */
47 public class UpdateMojo extends AbstractSchemaMojo
48 {
49   /**
50    * Output file.
51    * <p>
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>).
55    *
56    * @parameter property="hibernate.schema.update" default-value="update.sql"
57    * @since 1.0
58    */
59   private String outputFile;
60
61
62   @Override
63   public final void execute()
64     throws
65       MojoFailureException,
66       MojoExecutionException
67   {
68     super.execute(outputFile);
69   }
70
71
72   @Override
73   ExceptionHandlerCollectingImpl build(MetadataImplementor metadata)
74       throws
75         MojoExecutionException,
76         MojoFailureException
77   {
78     final ServiceRegistry service =
79         metadata.getMetadataBuildingOptions().getServiceRegistry();
80     SchemaManagementTool tool = service.getService(SchemaManagementTool.class);
81
82     final EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
83     if (execute)
84       targetTypes.add(TargetType.DATABASE);
85
86     TargetDescriptor target = new TargetDescriptor()
87     {
88       @Override
89       public EnumSet<TargetType> getTargetTypes()
90       {
91         return targetTypes;
92       }
93
94       @Override
95       public ScriptTargetOutput getScriptTargetOutput()
96       {
97         String charset
98             = (String) service
99             .getService(ConfigurationService.class)
100             .getSettings()
101             .get(AvailableSettings.HBM2DDL_CHARSET_NAME);
102         return new ScriptTargetOutputToFile(output, charset);
103       }
104     };
105
106     ExecutionOptions options =
107         SchemaManagementToolCoordinator.buildExecutionOptions(
108             service
109                 .getService(ConfigurationService.class)
110                 .getSettings(),
111             new ExceptionHandlerCollectingImpl()
112             );
113
114     Map config = options.getConfigurationValues();
115
116     tool.getSchemaMigrator(config).doMigration(metadata, options, target);
117   }
118 }