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 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;
27 * Goal which extracts the hibernate-mapping-configuration and
28 * exports an according SQL-database-schema.
31 * @phase process-classes
33 * @requiresDependencyResolution runtime
35 public class CreateMojo extends AbstractSchemaMojo
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.
42 * @parameter property="hibernate.export.export" default-value="true"
45 private boolean export;
49 * If set to <code>false</code>, only the SQL-script is created and the
50 * database is not touched.
52 * @parameter property=org.hibernate.cfg.AvailableSettings.HBM2DDL_IMPORT_FILES_SQL_EXTRACTOR default-value="false"
55 private boolean createNamespaces; // TODO handle in configure-Method
60 * @parameter property="hibernate.export.schema.filename" default-value="${project.build.directory}/schema.sql"
63 private String outputFile;
66 * Delimiter in output-file.
68 * @parameter property="hibernate.export.schema.delimiter" default-value=";"
71 private String delimiter;
76 * @parameter property="hibernate.export.schema.format" default-value="true"
79 private boolean format;
83 void build(MetadataImplementor metadata)
85 MojoExecutionException,
88 SchemaExport schemaExport = new SchemaExport(metadata, createNamespaces);
89 schemaExport.setDelimiter(delimiter);
90 schemaExport.setFormat(format);
92 File output = new File(outputFile);
94 if (!output.isAbsolute())
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());
101 // Ensure that directory path for specified file exists
102 File outFileParentDir = output.getParentFile();
103 if (null != outFileParentDir && !outFileParentDir.exists())
107 getLog().info("Creating directory path for output file:" + outFileParentDir.getPath());
108 outFileParentDir.mkdirs();
112 getLog().error("Error creating directory path for output file: " + e.getLocalizedMessage());
116 schemaExport.setOutputFile(output.getPath());
117 schemaExport.execute(false, this.export, false, true);
119 for (Object exception : schemaExport.getExceptions())
120 getLog().error(exception.toString());