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 java.util.Properties;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.apache.maven.plugin.MojoExecutionException;
22  import org.apache.maven.plugin.descriptor.PluginDescriptor;
23  import org.apache.torque.util.JdbcConfigurer;
24  import org.kuali.core.db.torque.DumpTask;
25  import org.kuali.core.db.torque.PropertyHandlingException;
26  import org.kuali.core.db.torque.StringFilter;
27  
28  /**
29   * Base Mojo for export related functionality
30   */
31  public abstract class ExportMojo extends AntTaskMojo {
32  
33      /**
34       * Additional properties for the JDBC driver
35       *
36       * @parameter
37       */
38      private Properties driverProperties;
39  
40      /**
41       * If true, files will be exported in a format that the Ant impex tasks can understand
42       *
43       * @parameter expression="${antCompatibilityMode}" default-value="false"
44       */
45      private boolean antCompatibilityMode;
46  
47      /**
48       * The Maven artifactId. Included here as a simple property of the mojo itself to facilitate usage of
49       * <code>BeanUtils.copyProperties()</code> to copy properties between Mojo's and Ant tasks
50       *
51       * @parameter expression="${artifactId}" default-value="${project.artifactId}"
52       * @required
53       */
54      private String artifactId;
55  
56      /**
57       * By default the version of the maven plugin being used during the build will be included as part of a comment in
58       * the XML. Set this to false to prevent this. Useful if you are committing files to SCM and don't want a change in
59       * the version of the plugin to show up as a change to the data.
60       *
61       * @parameter expression="${includeVersionInComment}" default-value="true"
62       * @required
63       */
64      private boolean includeVersionInComment;
65  
66      /**
67       * Additional comment that gets placed into the generated XML document. Anything included here gets appended to the
68       * default value <br>
69       * <b>Default value is:</b> Auto-generated by the maven-impex-plugin v1.0.2<br>
70       *
71       * @parameter expression="${comment}"
72       */
73      private String comment;
74  
75      /**
76       * Comma separated list of regular expressions for tables to include in the export
77       *
78       * @parameter expression="${includes}"
79       */
80      private String includes;
81  
82      /**
83       * Comma separated list of regular expressions for tables to exclude from the export
84       *
85       * @parameter expression="${excludes}"
86       */
87      private String excludes;
88  
89      /**
90       * The type of database we are targeting (eg oracle, mysql). This is optional if <code>url</code> is supplied as the
91       * database type will be automatically detected based on the <code>url</code>. If targetDatabase is explicitly
92       * supplied it will override the type selected by the automatic detection logic.
93       *
94       * @parameter expression="${targetDatabase}"
95       */
96      private String targetDatabase;
97  
98      /**
99       * The schema to export. This parameter is optional, as the schema to export is automatically derived from platform
100      * specific logic that converts the artifactId.<br>
101      * <br>
102      * For example:<br>
103      * ks-embedded-db becomes KSEMBEDDED for Oracle, and ksembedded for MySQL If <code>schema</code> is supplied, the
104      * supplied value will be used instead of the value derived from the artifactId
105      *
106      * @parameter expression="${schema}"
107      */
108     private String schema;
109 
110     /**
111      * Database driver classname. This parameter is optional, as the correct JDBC driver to use is detected from the
112      * <code>url</code> in almost all cases (works for Oracle, MySQL, Derby, PostGresSQL, DB2, H2, HSQL, SQL Server). If
113      * a driver is explicitly supplied, it will be used in place of the JDBC driver the automatic detection logic would
114      * have chosen.
115      *
116      * @parameter expression="${driver}"
117      */
118     private String driver;
119 
120     /**
121      * The connect URL of the database.
122      *
123      * @parameter expression="${url}"
124      * @required
125      */
126     private String url;
127 
128     /**
129      * The user name to connect to the database. If not specified this is automatically generated by platform specific
130      * logic for converting the artifact id.<br>
131      * <br>
132      * For example:<br>
133      * ks-embedded-db becomes KSEMBEDDED for Oracle and ksembedded for MySQL
134      *
135      * @parameter expression="${username}"
136      */
137     private String username;
138 
139     /**
140      * The password for the database user. If not specified this is automatically generated by platform specific logic
141      * for converting the artifact id.<br>
142      * <br>
143      * For example:<br>
144      * ks-embedded-db becomes KSEMBEDDED for Oracle and ksembedded for MySQL
145      *
146      * @parameter expression="${password}"
147      */
148     private String password;
149 
150     /**
151      * Returns the fully qualified class name of the database driver.
152      */
153     public String getDriver() {
154         return driver;
155     }
156 
157     /**
158      * Sets the fully qualified class name of the database driver.
159      *
160      * @param driver
161      *            the fully qualified class name of the database driver.
162      */
163     public void setDriver(final String driver) {
164         this.driver = driver;
165     }
166 
167     /**
168      * Returns the password of the database user.
169      *
170      * @return the password of the database user.
171      */
172     public String getPassword() {
173         return password;
174     }
175 
176     /**
177      * Sets the password of the database user.
178      *
179      * @param password
180      *            the password of the database user.
181      */
182     public void setPassword(final String password) {
183         this.password = password;
184     }
185 
186     /**
187      * Returns the connect URL to the database.
188      *
189      * @return the connect URL to the database.
190      */
191     public String getUrl() {
192         return url;
193     }
194 
195     /**
196      * Sets the connect URL to the database.
197      *
198      * @param url
199      *            the connect URL to the database.
200      */
201     public void setUrl(final String url) {
202         this.url = url;
203     }
204 
205     public String getUsername() {
206         return username;
207     }
208 
209     public void setUsername(final String username) {
210         this.username = username;
211     }
212 
213     public String getSchema() {
214         return schema;
215     }
216 
217     public void setSchema(final String schema) {
218         this.schema = schema;
219     }
220 
221     public String getTargetDatabase() {
222         return targetDatabase;
223     }
224 
225     public void setTargetDatabase(final String targetDatabase) {
226         this.targetDatabase = targetDatabase;
227     }
228 
229     public String getIncludes() {
230         return includes;
231     }
232 
233     public void setIncludes(final String includePatterns) {
234         this.includes = includePatterns;
235     }
236 
237     public String getExcludes() {
238         return excludes;
239     }
240 
241     public void setExcludes(final String excludePatterns) {
242         this.excludes = excludePatterns;
243     }
244 
245     public String getComment() {
246         return comment;
247     }
248 
249     public void setComment(final String comment) {
250         this.comment = comment;
251     }
252 
253     protected String getUpdatedComment() {
254         PluginDescriptor descriptor = (PluginDescriptor) this.getPluginContext().get("pluginDescriptor");
255         if (descriptor == null) {
256             // This is null for Maven 2.x
257             return " Auto-generated by the maven-impex-plugin " + getComment();
258         }
259         String name = descriptor.getName();
260         String version = descriptor.getVersion();
261         String comment = " Auto-generated by the " + name;
262         if (isIncludeVersionInComment()) {
263             comment += " v" + version + " ";
264         }
265         if (!StringUtils.isEmpty(getComment())) {
266             comment += getComment();
267         }
268         return comment;
269     }
270 
271     @Override
272     protected void configureTask() throws MojoExecutionException {
273         setComment(getUpdatedComment());
274         try {
275             JdbcConfigurer configurer = new JdbcConfigurer();
276             configurer.updateConfiguration(this);
277             configurer.validateConfiguration(this);
278         } catch (PropertyHandlingException e) {
279             throw new MojoExecutionException("Error handling properties", e);
280         }
281         super.configureTask();
282         DumpTask task = (DumpTask) super.getAntTask();
283         task.setIncludePatterns(StringFilter.getListFromCSV(getIncludes()));
284         task.setExcludePatterns(StringFilter.getListFromCSV(getExcludes()));
285         task.setDriverProperties(driverProperties);
286     }
287 
288     public String getArtifactId() {
289         return artifactId;
290     }
291 
292     public void setArtifactId(final String artifactId) {
293         this.artifactId = artifactId;
294     }
295 
296     public boolean isAntCompatibilityMode() {
297         return antCompatibilityMode;
298     }
299 
300     public void setAntCompatibilityMode(final boolean antCompatibilityMode) {
301         this.antCompatibilityMode = antCompatibilityMode;
302     }
303 
304     /**
305      * @return the includeVersionInComment
306      */
307     public boolean isIncludeVersionInComment() {
308         return includeVersionInComment;
309     }
310 
311     /**
312      * @param includeVersionInComment
313      *            the includeVersionInComment to set
314      */
315     public void setIncludeVersionInComment(final boolean includeVersionInComment) {
316         this.includeVersionInComment = includeVersionInComment;
317     }
318 
319     public Properties getDriverProperties() {
320         return driverProperties;
321     }
322 
323     public void setDriverProperties(Properties driverProperties) {
324         this.driverProperties = driverProperties;
325     }
326 }