View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.torque.mojo;
17  
18  import org.apache.maven.plugin.MojoExecutionException;
19  import org.apache.torque.util.ChangeDetector;
20  
21  /**
22   * Generates platform specific SQL from database agnostic XML files.<br>
23   * <br>
24   * There are two types of SQL files created by this goal:<br>
25   * <br>
26   * Type 1: DDL statements for creating tables, primary keys, indexes, and unique constraints. Does not contain DDL for enforcing relationships between tables.<br>
27   * Type 2: DDL statements for creating and enforcing relationships between tables<br>
28   * <br>
29   * This allows data to be imported into multiple tables concurrently. Running the first type of SQL file will create the empty tables without any foreign key constraints. Data can
30   * then be loaded concurrently into the tables (using optimized high speed tools if desired) without needing to worry about the order in which the tables are loaded. After data has
31   * been loaded, the second type of SQL file can be run to add the relationships between the tables. As long as the data set is consistent and correct, all the relationships will
32   * get created correctly.<br>
33   * <br>
34   * The database platform to generate SQL for is determined by ${targetDatabase}. See also <code>impex:datasql</code>
35   * 
36   * @goal schemasql
37   * @phase generate-sources
38   */
39  public class SchemaSqlMojo extends SqlMojoBase {
40  
41  	/**
42  	 * The directory in which the SQL will be generated.
43  	 * 
44  	 * @parameter expression="${outputDir}" default-value="${project.build.directory}/classes/sql"
45  	 */
46  	@SuppressWarnings("unused")
47  	private String dummy1;
48  
49  	/**
50  	 * The location where the report file will be generated.
51  	 * 
52  	 * @parameter expression="${reportFile}" default-value= "../../../reports/report.${project.artifactId}.sql.generation"
53  	 */
54  	@SuppressWarnings("unused")
55  	private String dummy2;
56  
57  	/**
58  	 * The location where the context property file for velocity will be generated.
59  	 * 
60  	 * @parameter expression="${contextPropertiesPath}" default-value="${project.build.directory}/reports/context.sql.properties"
61  	 */
62  	@SuppressWarnings("unused")
63  	private String dummy3;
64  
65  	/**
66  	 * The suffix of the generated sql files.
67  	 * 
68  	 * @parameter expression="${suffix}"
69  	 */
70  	@SuppressWarnings("unused")
71  	private String dummy4;
72  
73  	protected void showConfig() {
74  		getLog().info("Schema Dir: " + getSchemaDir());
75  		getLog().info("Includes: " + getSchemaIncludes());
76  		getLog().info("Excludes: " + getSchemaExcludes());
77  	}
78  
79  	/**
80  	 * Generate SQL from schema XML files
81  	 */
82  	@Override
83  	public void executeMojo() throws MojoExecutionException {
84  		updateConfiguration();
85  		validateConfiguration();
86  		generateContextProperties();
87  		configureTask();
88  		addTargetDatabaseToOutputDir();
89  		addTargetDatabaseToReportFile();
90  		showConfig();
91  		ChangeDetector detector = new ChangeDetector(getCanonicalReportFile(), getSchemaFiles());
92  		if (!detector.isChanged() && isRunOnlyOnSchemaChange()) {
93  			getLog().info("Schema has not changed.  Skipping generation");
94  			return;
95  		}
96  		getLog().info("------------------------------------------------------------------------");
97  		getLog().info("Generating SQL for " + getTargetDatabase() + " from schema XML files");
98  		getLog().info("------------------------------------------------------------------------");
99  		getAntTask().execute();
100 	}
101 }