View Javadoc

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