View Javadoc

1   package org.apache.torque.mojo;
2   
3   import java.io.File;
4   import java.io.IOException;
5   
6   import org.apache.commons.configuration.PropertiesConfiguration;
7   import org.apache.commons.io.FileUtils;
8   import org.apache.maven.plugin.MojoExecutionException;
9   import org.apache.torque.task.TorqueDataModelTask;
10  import org.kuali.core.db.torque.Utils;
11  
12  /**
13   * Generates a DTD for the database tables from a schema XML file
14   *
15   * @goal datadtd
16   * @phase generate-sources
17   */
18  public class DataDtdMojo extends DataModelTaskMojo {
19      /** The context property for the name of the project. */
20      public static final String PROJECT_CONTEXT_PROPERTY = "project";
21  
22      /**
23       * @parameter expression="${antCompatibilityMode}" antCompatibilityMode="false"
24       */
25      boolean antCompatibilityMode;
26  
27      /**
28       * Only used if antCompatibilityMode is set to true. If so, the dtd that gets generated will be copied to
29       *
30       * @parameter expression="${copyToFile}" default-value="${basedir}/src/main/impex/data.dtd"
31       */
32      String copyToFile;
33  
34      /**
35       * Included here as a simple property to facilitate generating DTD's for other artifacts
36       *
37       * @parameter expression="${artifactId}" default-value="${project.artifactId}"
38       */
39      String artifactId;
40  
41      /**
42       * The directory in which the DTD will be generated
43       *
44       * @parameter property="outputDir" expression="${outputDir}"
45       * default-value="${project.build.directory}/generated-impex"
46       */
47      @SuppressWarnings("unused")
48      private String dummy1;
49  
50      /**
51       * The location where the report file will be generated, relative to outputDir.
52       *
53       * @parameter property="reportFile" expression="${reportFile}"
54       * default-value="../reports/report.${project.artifactId}.datadtd.generation"
55       */
56      @SuppressWarnings("unused")
57      private String dummy2;
58  
59      /**
60       * The location where the context property file for velocity will be generated.
61       *
62       * @parameter property="contextPropertiesPath" expression="${contextPropertiesPath}"
63       * default-value="${project.build.directory}/reports/context.datadtd.properties"
64       */
65      @SuppressWarnings("unused")
66      private String dummy3;
67  
68      /**
69       * The name of the project
70       *
71       * @parameter expression="${projectName}" default-value="impex"
72       */
73      private String projectName;
74  
75      /**
76       * The name of the schema.xml file to process
77       *
78       * @parameter expression="${schemaXMLFile}"
79       * default-value="${project.build.directory}/generated-impex/${project.artifactId}.xml"
80       * @required
81       */
82      private String schemaXMLFile;
83  
84      /**
85       * Returns the context properties for the Texen task.
86       *
87       * @return The PropertiesConfiguration containing all context properties, not null.
88       */
89      @Override
90      protected PropertiesConfiguration getMojoContextProperties() {
91          PropertiesConfiguration configuration = new PropertiesConfiguration();
92          configuration.addProperty(PROJECT_CONTEXT_PROPERTY, getProjectName());
93          configuration.addProperty("version", getProject().getVersion());
94          return configuration;
95      }
96  
97      protected void showConfig() {
98          getLog().info("Schema XML File: " + schemaXMLFile);
99          getLog().info("Ant Compatibility Mode: " + antCompatibilityMode);
100         getLog().info("Output Directory: " + getOutputDir());
101     }
102 
103     /**
104      * Configures the Texen task wrapped by this mojo
105      */
106     @Override
107     protected void configureTask() throws MojoExecutionException {
108         TorqueDataModelTask task = new TorqueDataModelTask();
109         setAntTask(task);
110         super.configureTask();
111         boolean exists = new Utils().isFileOrResource(getSchemaXMLFile());
112         if (!exists) {
113             throw new MojoExecutionException("Unable to locate: " + getSchemaXMLFile());
114         }
115         task.setXmlFile(getSchemaXMLFile());
116 
117     }
118 
119     /**
120      * Returns the path to the control template.
121      *
122      * @return "data/Control.vm"
123      */
124     @Override
125     protected String getControlTemplate() {
126         return "data/Control.vm";
127     }
128 
129     /**
130      * Returns the name of the project
131      *
132      * @return the name of the project.
133      */
134     public String getProjectName() {
135         return projectName;
136     }
137 
138     /**
139      * Sets the name of the project
140      *
141      * @param project
142      * the name of the project.
143      */
144     public void setProjectName(final String projectName) {
145         this.projectName = projectName;
146     }
147 
148     /**
149      * Returns the name of the xml file to process.
150      *
151      * @return the name of the xml file to process.
152      */
153     public String getSchemaXMLFile() {
154         return schemaXMLFile;
155     }
156 
157     /**
158      * Sets the name of the xml file to process.
159      *
160      * @param project
161      * the name of the xml file to process.
162      */
163     public void setSchemaXMLFile(final String xmlFile) {
164         this.schemaXMLFile = xmlFile;
165     }
166 
167     @Override
168     public void executeMojo() throws MojoExecutionException {
169         getLog().info("------------------------------------------------------------------------");
170         getLog().info("Generating database DTD");
171         getLog().info("------------------------------------------------------------------------");
172         showConfig();
173         super.executeMojo();
174         if (antCompatibilityMode) {
175             File srcFile = new File(getOutputDir() + FS + getArtifactId() + ".dtd");
176             File dstFile = new File(copyToFile);
177             getLog().info("Creating " + dstFile.getAbsolutePath() + " for Ant compatibility mode");
178             try {
179                 FileUtils.copyFile(srcFile, dstFile);
180             } catch (IOException e) {
181                 throw new MojoExecutionException("Error copying file", e);
182             }
183         }
184     }
185 
186     public boolean isAntCompatibilityMode() {
187         return antCompatibilityMode;
188     }
189 
190     public void setAntCompatibilityMode(final boolean antCompatibilityMode) {
191         this.antCompatibilityMode = antCompatibilityMode;
192     }
193 
194     public String getCopyToFile() {
195         return copyToFile;
196     }
197 
198     public void setCopyToFile(final String copyToFile) {
199         this.copyToFile = copyToFile;
200     }
201 
202     public String getArtifactId() {
203         return artifactId;
204     }
205 
206     public void setArtifactId(final String artifactId) {
207         this.artifactId = artifactId;
208     }
209 }