19078ea89efc313a89269200066bcc3c8272b41c
[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 java.io.File;
20 import org.apache.maven.plugin.MojoExecutionException;
21 import org.apache.maven.plugin.MojoFailureException;
22 import org.hibernate.boot.spi.MetadataImplementor;
23 import org.hibernate.tool.hbm2ddl.SchemaExport;
24
25
26 /**
27  * Goal which extracts the hibernate-mapping-configuration and
28  * exports an according SQL-database-schema.
29  *
30  * @goal create
31  * @phase process-classes
32  * @threadSafe
33  * @requiresDependencyResolution runtime
34  */
35 public class CreateMojo extends AbstractSchemaMojo
36 {
37   /**
38    * Export the database-schma to the database.
39    * If set to <code>false</code>, only the SQL-script is created and the
40    * database is not touched.
41    *
42    * @parameter property="hibernate.export.export" default-value="true"
43    * @since 2.0
44    */
45   private boolean export;
46
47   /**
48    * Create the catalog
49    * If set to <code>false</code>, only the SQL-script is created and the
50    * database is not touched.
51    *
52    * @parameter property=org.hibernate.cfg.AvailableSettings.HBM2DDL_IMPORT_FILES_SQL_EXTRACTOR default-value="false"
53    * @since 2.0
54    */
55   private boolean createNamespaces; // TODO handle in configure-Method
56
57   /**
58    * Output file.
59    *
60    * @parameter property="hibernate.export.schema.filename" default-value="${project.build.directory}/schema.sql"
61    * @since 1.0
62    */
63   private String outputFile;
64
65   /**
66    * Delimiter in output-file.
67    *
68    * @parameter property="hibernate.export.schema.delimiter" default-value=";"
69    * @since 1.0
70    */
71   private String delimiter;
72
73   /**
74    * Format output-file.
75    *
76    * @parameter property="hibernate.export.schema.format" default-value="true"
77    * @since 1.0
78    */
79   private boolean format;
80
81
82   @Override
83   void build(MetadataImplementor metadata)
84       throws
85         MojoExecutionException,
86         MojoFailureException
87   {
88     SchemaExport schemaExport = new SchemaExport(metadata, createNamespaces);
89     schemaExport.setDelimiter(delimiter);
90     schemaExport.setFormat(format);
91
92     File output = new File(outputFile);
93
94     if (!output.isAbsolute())
95     {
96       // Interpret relative file path relative to build directory
97       output = new File(buildDirectory, outputFile);
98       getLog().debug("Adjusted relative path, resulting path is " + output.getPath());
99     }
100
101     // Ensure that directory path for specified file exists
102     File outFileParentDir = output.getParentFile();
103     if (null != outFileParentDir && !outFileParentDir.exists())
104     {
105       try
106       {
107         getLog().info("Creating directory path for output file:" + outFileParentDir.getPath());
108         outFileParentDir.mkdirs();
109       }
110       catch (Exception e)
111       {
112         getLog().error("Error creating directory path for output file: " + e.getLocalizedMessage());
113       }
114     }
115
116     schemaExport.setOutputFile(output.getPath());
117     schemaExport.execute(false, this.export, false, true);
118
119     for (Object exception : schemaExport.getExceptions())
120       getLog().error(exception.toString());
121   }
122 }