a6c2ba42b4b9140125d685085fd6b8649cd55348
[hibernate4-maven-plugin] / src / main / java / de / juplo / plugins / hibernate / CreateMojo.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 static de.juplo.plugins.hibernate.AbstractSchemaMojo.EXECUTE;
20 import java.io.File;
21 import java.util.EnumSet;
22 import java.util.Map;
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;
37
38
39 /**
40  * Generate/Execute SQL to create a database-schema that represents the
41  * configured mappings.
42  *
43  * @goal create
44  * @phase process-classes
45  * @threadSafe
46  * @requiresDependencyResolution runtime
47  */
48 public class CreateMojo extends AbstractSchemaMojo
49 {
50   /**
51    * Output file.
52    * <p>
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>).
56    *
57    * @parameter property="hibernate.schema.create" default-value="create.sql"
58    * @since 1.0
59    */
60   private String outputFile;
61
62
63   @Override
64   public final void execute()
65     throws
66       MojoFailureException,
67       MojoExecutionException
68   {
69     super.execute(outputFile);
70   }
71
72
73   @Override
74   ExceptionHandlerCollectingImpl build(
75       final MetadataImplementor metadata,
76       final File output
77       )
78       throws
79         MojoExecutionException,
80         MojoFailureException
81   {
82     final ServiceRegistry registry =
83         metadata.getMetadataBuildingOptions().getServiceRegistry();
84     final Map settings = 
85         registry.getService(ConfigurationService.class).getSettings();
86     SchemaManagementTool tool = registry.getService(SchemaManagementTool.class);
87
88     final EnumSet<TargetType> targetTypes = EnumSet.of(TargetType.SCRIPT);
89     if ((Boolean)settings.get(EXECUTE))
90       targetTypes.add(TargetType.DATABASE);
91
92     TargetDescriptor target = new TargetDescriptor()
93     {
94       @Override
95       public EnumSet<TargetType> getTargetTypes()
96       {
97         return targetTypes;
98       }
99
100       @Override
101       public ScriptTargetOutput getScriptTargetOutput()
102       {
103         String charset =
104             (String)settings.get(AvailableSettings.HBM2DDL_CHARSET_NAME);
105         return new ScriptTargetOutputToFile(output, charset);
106       }
107     };
108
109     ExceptionHandlerCollectingImpl handler =
110         new ExceptionHandlerCollectingImpl();
111
112     ExecutionOptions options =
113         SchemaManagementToolCoordinator.buildExecutionOptions(
114             registry
115                 .getService(ConfigurationService.class)
116                 .getSettings(),
117             handler
118             );
119
120     Map config = options.getConfigurationValues();
121
122     tool.getSchemaMigrator(config).doMigration(metadata, options, target);
123
124     return handler;
125   }
126 }