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; either
009: * version 2.1 of the License, or (at your option) any later version.
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: import java.util.StringTokenizer;
022: import java.util.Collection;
023: import java.util.Iterator;
024:
025: // Maven and Plexus dependencies
026: import org.apache.commons.lang.SystemUtils;
027: import org.apache.maven.project.MavenProject;
028: import org.apache.maven.plugin.AbstractMojo;
029: import org.apache.maven.plugin.MojoExecutionException;
030: import org.apache.maven.artifact.DependencyResolutionRequiredException;
031: import org.codehaus.plexus.util.cli.Commandline;
032: import org.codehaus.plexus.util.cli.CommandLineUtils;
033: import org.codehaus.plexus.util.cli.CommandLineException;
034: import org.codehaus.plexus.util.cli.DefaultConsumer;
035:
036: // Note: javadoc in class and fields descriptions must be XHTML.
037: /**
038: * Invokes the RMI compiler (<code>rmic</code>).
039: *
040: * @goal rmic
041: * @phase compile
042: * @description Invokes the RMI compiler (rmic).
043: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/build/maven/rmic/src/main/java/org/geotools/maven/RMICompiler.java $
044: * @version $Id: RMICompiler.java 20889 2006-08-07 15:52:32Z jgarnett $
045: * @author Martin Desruisseaux
046: */
047: public class RMICompiler extends AbstractMojo {
048: /**
049: * Directory where the output class files will be located.
050: *
051: * @parameter expression="${project.build.outputDirectory}"
052: * @required
053: */
054: private String outputDirectory;
055:
056: /**
057: * Comma-separated list of classes to compile.
058: *
059: * @parameter
060: * @required
061: */
062: private String includes;
063:
064: /**
065: * The Maven project.
066: *
067: * @parameter expression="${project}"
068: * @required
069: * @readonly
070: */
071: private MavenProject project;
072:
073: /**
074: * Invokes the RMI compiler.
075: *
076: * @throws MojoExecutionException if the plugin execution failed.
077: */
078: public void execute() throws MojoExecutionException {
079: /*
080: * Prepares the command line instruction using the Plexus utilities.
081: * First, we set the executable (full path to rmic.exe).
082: */
083: final Commandline cmd = new Commandline();
084: String command = "/../bin/rmic";
085: if (SystemUtils.IS_OS_WINDOWS) {
086: command += ".exe";
087: }
088: final File executable = new File(SystemUtils.getJavaHome(),
089: command);
090: cmd.setExecutable(executable.getAbsolutePath());
091: cmd.setWorkingDirectory(new File(outputDirectory)
092: .getAbsolutePath());
093: /*
094: * Sets the arguments: the classpath, the output directory and the list
095: * if all class files to compile.
096: */
097: final Collection pathElements;
098: try {
099: pathElements = project.getCompileClasspathElements();
100: } catch (DependencyResolutionRequiredException e) {
101: throw new MojoExecutionException(
102: "Unresolved compile dependencies", e);
103: }
104: final String pathSeparator = System
105: .getProperty("path.separator");
106: final StringBuffer classpath = new StringBuffer();
107: for (final Iterator it = pathElements.iterator(); it.hasNext();) {
108: if (classpath.length() != 0) {
109: classpath.append(pathSeparator);
110: }
111: classpath.append(it.next());
112: }
113: if (classpath.length() != 0) {
114: cmd.createArgument().setValue("-classpath");
115: cmd.createArgument().setValue(classpath.toString());
116: }
117: int n = 0;
118: if (includes != null) {
119: final StringTokenizer items = new StringTokenizer(includes,
120: ",");
121: while (items.hasMoreTokens()) {
122: cmd.createArgument().setValue(items.nextToken().trim());
123: n++;
124: }
125: }
126: /*
127: * Run the command.
128: */
129: final int exitCode;
130: CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
131: try {
132: exitCode = CommandLineUtils.executeCommandLine(cmd,
133: new DefaultConsumer(), err);
134: } catch (CommandLineException e) {
135: throw new MojoExecutionException(
136: "Unable to execute rmic command", e);
137: }
138: if (exitCode != 0) {
139: throw new MojoExecutionException("Exit code: " + exitCode
140: + " - " + err.getOutput());
141: }
142: getLog().info("Compiled " + n + " RMI classes.");
143: }
144: }
|