001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.maven;
017:
018: // J2SE dependencies
019: import java.io.File;
020: import java.io.IOException;
021:
022: // Maven and Plexus dependencies
023: import org.apache.maven.plugin.AbstractMojo;
024: import org.apache.maven.plugin.MojoExecutionException;
025: import org.codehaus.plexus.util.FileUtils;
026:
027: // Note: javadoc in class and fields descriptions must be XHTML.
028: /**
029: * Compiles Java interfaces from OpenOffice IDL files. In an ideal world, this plugin should
030: * executes <code>idlc</code> on the <code>*.idl</code> files, then <code>regmerge</code> on
031: * the generated <code>*.urd</code> files, then <code>javamaker</code> on the generated
032: * <code>*.rdb</code> files. However, since the above mentioned tools are native and would
033: * require a manual installation on all developers machine, current version just copies a
034: * pre-compiled class file. This copy must occurs after the compilation phase (in order to
035: * overwrite the files generated by <code>javac</code>), which is why the usual Maven resources
036: * mechanism doesn't fit.
037: *
038: * @goal generate
039: * @phase process-classes
040: * @description Copies .class files generated from OpenOffice IDL
041: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/build/maven/javamaker/src/main/java/org/geotools/maven/JavaMaker.java $
042: * @version $Id: JavaMaker.java 20606 2006-07-18 14:31:51Z jgarnett $
043: * @author Martin Desruisseaux
044: *
045: * @todo A future version should executes {@code idlc}, {@code regmerge} and {@code javamaker}
046: * from an OpenOffice SDK installation, and fallback on current behavior (just copy files)
047: * if no OpenOffice SDK is found.
048: */
049: public class JavaMaker extends AbstractMojo {
050: /**
051: * Directory where the .class file(s) are located.
052: *
053: * @parameter expression="${project.build.sourceDirectory}"
054: * @required
055: */
056: private String sourceDirectory;
057:
058: /**
059: * Directory where the output Java files will be located.
060: *
061: * @parameter expression="${project.build.outputDirectory}"
062: * @required
063: */
064: private String outputDirectory;
065:
066: /**
067: * Copies the {@code .class} files generated by OpenOffice.
068: *
069: * @throws MojoExecutionException if the plugin execution failed.
070: */
071: public void execute() throws MojoExecutionException {
072: final int n;
073: try {
074: n = copyClasses(new File(sourceDirectory), new File(
075: outputDirectory));
076: } catch (IOException e) {
077: throw new MojoExecutionException(
078: "Failed to copy *.class files.", e);
079: }
080: getLog().info("Copied " + n + " pre-compiled class files");
081: }
082:
083: /**
084: * Copies {@code *.class} files from source directory to output directory. The output
085: * directory structure should already exists. It should be the case if all sources files
086: * have been compiled before this method is invoked.
087: *
088: * @return The number of files copied.
089: */
090: private static int copyClasses(final File sourceDirectory,
091: final File outputDirectory) throws IOException {
092: int n = 0;
093: final String[] filenames = sourceDirectory.list();
094: for (int i = 0; i < filenames.length; i++) {
095: final String filename = filenames[i];
096: final File file = new File(sourceDirectory, filename);
097: if (file.isFile()) {
098: if (filename.endsWith(".class")
099: || filename.endsWith(".CLASS")) {
100: FileUtils
101: .copyFileToDirectory(file, outputDirectory);
102: n++;
103: }
104: } else if (file.isDirectory()) {
105: n += copyClasses(file, new File(outputDirectory,
106: filename));
107: }
108: }
109: return n;
110: }
111: }
|