001 /**
002 * Copyright 2004-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.apache.torque.mojo;
017
018 import java.io.File;
019 import java.io.IOException;
020
021 import org.apache.commons.configuration.PropertiesConfiguration;
022 import org.apache.commons.io.FileUtils;
023 import org.apache.maven.plugin.MojoExecutionException;
024 import org.apache.torque.task.TorqueDataModelTask;
025 import org.kuali.core.db.torque.Utils;
026
027 /**
028 * Generates a DTD for the database tables from a schema XML file
029 *
030 * @goal datadtd
031 * @phase generate-sources
032 */
033 public class DataDtdMojo extends DataModelTaskMojo {
034 /** The context property for the name of the project. */
035 public static final String PROJECT_CONTEXT_PROPERTY = "project";
036
037 /**
038 * @parameter expression="${antCompatibilityMode}" antCompatibilityMode="false"
039 */
040 boolean antCompatibilityMode;
041
042 /**
043 * Only used if antCompatibilityMode is set to true. If so, the dtd that gets generated will be copied to
044 *
045 * @parameter expression="${copyToFile}" default-value="${basedir}/src/main/impex/data.dtd"
046 */
047 String copyToFile;
048
049 /**
050 * Included here as a simple property to facilitate generating DTD's for other artifacts
051 *
052 * @parameter expression="${artifactId}" default-value="${project.artifactId}"
053 */
054 String artifactId;
055
056 /**
057 * The directory in which the DTD will be generated
058 *
059 * @parameter expression="${outputDir}" default-value="${project.build.directory}/generated-impex"
060 */
061 @SuppressWarnings("unused")
062 private String dummy1;
063
064 /**
065 * The location where the report file will be generated, relative to outputDir.
066 *
067 * @parameter expression="${reportFile}" default-value="../reports/report.${project.artifactId}.datadtd.generation"
068 */
069 @SuppressWarnings("unused")
070 private String dummy2;
071
072 /**
073 * The location where the context property file for velocity will be generated.
074 *
075 * @parameter expression="${contextPropertiesPath}" default-value="${project.build.directory}/reports/context.datadtd.properties"
076 */
077 @SuppressWarnings("unused")
078 private String dummy3;
079
080 /**
081 * The name of the project
082 *
083 * @parameter expression="${projectName}" default-value="impex"
084 */
085 private String projectName;
086
087 /**
088 * The name of the schema.xml file to process
089 *
090 * @parameter expression="${schemaXMLFile}" default-value="${project.build.directory}/generated-impex/${project.artifactId}.xml"
091 * @required
092 */
093 private String schemaXMLFile;
094
095 /**
096 * Returns the context properties for the Texen task.
097 *
098 * @return The PropertiesConfiguration containing all context properties, not null.
099 */
100 @Override
101 protected PropertiesConfiguration getMojoContextProperties() {
102 PropertiesConfiguration configuration = new PropertiesConfiguration();
103 configuration.addProperty(PROJECT_CONTEXT_PROPERTY, getProjectName());
104 configuration.addProperty("version", getProject().getVersion());
105 return configuration;
106 }
107
108 protected void showConfig() {
109 getLog().info("Schema XML File: " + schemaXMLFile);
110 getLog().info("Ant Compatibility Mode: " + antCompatibilityMode);
111 getLog().info("Output Directory: " + getOutputDir());
112 }
113
114 /**
115 * Configures the Texen task wrapped by this mojo
116 */
117 @Override
118 protected void configureTask() throws MojoExecutionException {
119 TorqueDataModelTask task = new TorqueDataModelTask();
120 setAntTask(task);
121 super.configureTask();
122 boolean exists = new Utils().isFileOrResource(getSchemaXMLFile());
123 if (!exists) {
124 throw new MojoExecutionException("Unable to locate: " + getSchemaXMLFile());
125 }
126 task.setXmlFile(getSchemaXMLFile());
127
128 }
129
130 /**
131 * Returns the path to the control template.
132 *
133 * @return "data/Control.vm"
134 */
135 @Override
136 protected String getControlTemplate() {
137 return "data/Control.vm";
138 }
139
140 /**
141 * Returns the name of the project
142 *
143 * @return the name of the project.
144 */
145 public String getProjectName() {
146 return projectName;
147 }
148
149 /**
150 * Sets the name of the project
151 *
152 * @param project
153 * the name of the project.
154 */
155 public void setProjectName(final String projectName) {
156 this.projectName = projectName;
157 }
158
159 /**
160 * Returns the name of the xml file to process.
161 *
162 * @return the name of the xml file to process.
163 */
164 public String getSchemaXMLFile() {
165 return schemaXMLFile;
166 }
167
168 /**
169 * Sets the name of the xml file to process.
170 *
171 * @param project
172 * the name of the xml file to process.
173 */
174 public void setSchemaXMLFile(final String xmlFile) {
175 this.schemaXMLFile = xmlFile;
176 }
177
178 @Override
179 public void executeMojo() throws MojoExecutionException {
180 getLog().info("------------------------------------------------------------------------");
181 getLog().info("Generating database DTD");
182 getLog().info("------------------------------------------------------------------------");
183 showConfig();
184 super.executeMojo();
185 if (antCompatibilityMode) {
186 File srcFile = new File(getOutputDir() + FS + getArtifactId() + ".dtd");
187 File dstFile = new File(copyToFile);
188 getLog().info("Creating " + dstFile.getAbsolutePath() + " for Ant compatibility mode");
189 try {
190 FileUtils.copyFile(srcFile, dstFile);
191 } catch (IOException e) {
192 throw new MojoExecutionException("Error copying file", e);
193 }
194 }
195 }
196
197 public boolean isAntCompatibilityMode() {
198 return antCompatibilityMode;
199 }
200
201 public void setAntCompatibilityMode(final boolean antCompatibilityMode) {
202 this.antCompatibilityMode = antCompatibilityMode;
203 }
204
205 public String getCopyToFile() {
206 return copyToFile;
207 }
208
209 public void setCopyToFile(final String copyToFile) {
210 this.copyToFile = copyToFile;
211 }
212
213 public String getArtifactId() {
214 return artifactId;
215 }
216
217 public void setArtifactId(final String artifactId) {
218 this.artifactId = artifactId;
219 }
220 }