View Javadoc

1   /**
2    * Copyright 2004-2012 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.io.File;
19  import java.io.IOException;
20  
21  import org.apache.maven.plugin.MojoExecutionException;
22  import org.apache.torque.mojo.morph.FileMorphRequest;
23  import org.apache.torque.mojo.morph.MorphRequest;
24  import org.apache.torque.mojo.morph.Morpher;
25  import org.apache.torque.mojo.morph.VersionTableMorpher;
26  import org.codehaus.plexus.util.FileUtils;
27  
28  /**
29   * Morph the xml inside KS_DB_VERSION.xml so it has Maven friendly placeholders
30   * for version info
31   * 
32   * @goal morphversiontable
33   * @phase generate-sources
34   */
35  public class MorphVersionTableMojo extends AbstractMorphSingleMojo {
36  
37  	/**
38  	 * The XML file that will contain the new information
39  	 * 
40  	 * @parameter expression="${newVersionXMLFile}" default-value=
41  	 *            "${project.build.directory}/generated-impex/xml/KS_DB_VERSION.xml"
42  	 * @required
43  	 */
44  	private File newVersionXMLFile;
45  
46  	/**
47  	 * The XML file that contains the old information
48  	 * 
49  	 * @parameter expression="${oldVersionXMLFile}" default-value=
50  	 *            "${project.build.directory}/generated-impex/xml/KS_DB_VERSION.xml"
51  	 * @required
52  	 */
53  	private File oldVersionXMLFile;
54  
55  	protected void beforeExecution() {
56  		setNewFile(newVersionXMLFile);
57  		setOldFile(oldVersionXMLFile);
58  	}
59  
60  	@Override
61  	protected boolean isMorphNeeded() {
62  		// Only reason
63  		if (!getOldFile().exists()) {
64  			getLog().debug("file:" + getOldFile().getAbsolutePath() + " does not exist");
65  			return false;
66  		} else {
67  			return true;
68  		}
69  	}
70  
71  	@Override
72  	protected void executeMojo() throws MojoExecutionException {
73  		getLog().info("------------------------------------------------------------------------");
74  		getLog().info("Converting version table XML file");
75  		getLog().info("------------------------------------------------------------------------");
76  		try {
77  			FileUtils.forceMkdir(new File(FileUtils.getPath(getNewFile().getAbsolutePath())));
78  			MorphRequest request = new FileMorphRequest(getOldFile(), getNewFile());
79  			request.setName(getOldFile().getName());
80  			request.setEncoding(getEncoding());
81  			Morpher morpher = getMorpher(request, getProject().getArtifactId());
82  			morpher.executeMorph();
83  		} catch (IOException e) {
84  			throw new MojoExecutionException("Unexpected error while attempting to morph " + getOldFile().getAbsolutePath(), e);
85  		}
86  	}
87  
88  	protected Morpher getMorpher(MorphRequest request, String artifactId) {
89  		VersionTableMorpher morpher = new VersionTableMorpher(request, artifactId);
90  		morpher.setProjectVersion(getProject().getVersion());
91  		return morpher;
92  	}
93  
94  	/**
95  	 * @return the newVersionXMLFile
96  	 */
97  	public File getNewVersionXMLFile() {
98  		return newVersionXMLFile;
99  	}
100 
101 	/**
102 	 * @param newVersionXMLFile
103 	 *            the newVersionXMLFile to set
104 	 */
105 	public void setNewVersionXMLFile(final File newVersionXMLFile) {
106 		this.newVersionXMLFile = newVersionXMLFile;
107 	}
108 
109 	/**
110 	 * @return the oldVersionXMLFile
111 	 */
112 	public File getOldVersionXMLFile() {
113 		return oldVersionXMLFile;
114 	}
115 
116 	/**
117 	 * @param oldVersionXMLFile
118 	 *            the oldVersionXMLFile to set
119 	 */
120 	public void setOldVersionXMLFile(final File oldVersionXMLFile) {
121 		this.oldVersionXMLFile = oldVersionXMLFile;
122 	}
123 }