View Javadoc
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 drop
31   * @phase process-classes
32   * @threadSafe
33   * @requiresDependencyResolution runtime
34   */
35  public class DropMojo extends AbstractSchemaMojo
36  {
37    /**
38     * Output file.
39     * <p>
40     * If the specified filename is not absolut, the file will be created
41     * relative to the project build directory
42     * (<code>project.build.directory</code>).
43     *
44     * @parameter property="hibernate.schema.export.drop" default-value="drop.sql"
45     * @since 1.0
46     */
47    private String outputFile;
48  
49  
50    @Override
51    public final void execute()
52      throws
53        MojoFailureException,
54        MojoExecutionException
55    {
56      super.execute(outputFile);
57    }
58  
59  
60    @Override
61    void build(MetadataImplementor metadata)
62        throws
63          MojoExecutionException,
64          MojoFailureException
65    {
66      SchemaExport schemaExport = new SchemaExport(metadata, createNamespaces);
67      schemaExport.setDelimiter(delimiter);
68      schemaExport.setFormat(format);
69  
70      File output = new File(outputFile);
71  
72      if (!output.isAbsolute())
73      {
74        // Interpret relative file path relative to build directory
75        output = new File(buildDirectory, outputFile);
76        getLog().debug("Adjusted relative path, resulting path is " + output.getPath());
77      }
78  
79      // Ensure that directory path for specified file exists
80      File outFileParentDir = output.getParentFile();
81      if (null != outFileParentDir && !outFileParentDir.exists())
82      {
83        try
84        {
85          getLog().info("Creating directory path for output file:" + outFileParentDir.getPath());
86          outFileParentDir.mkdirs();
87        }
88        catch (Exception e)
89        {
90          getLog().error("Error creating directory path for output file: " + e.getLocalizedMessage());
91        }
92      }
93  
94      schemaExport.setOutputFile(output.getPath());
95      schemaExport.execute(false, this.export, true, false);
96  
97      for (Object exception : schemaExport.getExceptions())
98        getLog().error(exception.toString());
99    }
100 }