diff --git a/maven-mboot2/src/main/java/MBoot.java b/maven-mboot2/src/main/java/MBoot.java index 92a51ed3cd..884c231914 100644 --- a/maven-mboot2/src/main/java/MBoot.java +++ b/maven-mboot2/src/main/java/MBoot.java @@ -885,6 +885,7 @@ public class MBoot CompilerConfiguration compilerConfiguration = new CompilerConfiguration(); compilerConfiguration.setOutputLocation( outputDirectory ); List classpathEntries = classpath( dependencies, extraClasspath, scope, localRepository ); + compilerConfiguration.setNoWarn( true ); compilerConfiguration.setClasspathEntries( classpathEntries ); compilerConfiguration.setSourceLocations( Arrays.asList( sourceDirectories ) ); diff --git a/maven-mboot2/src/main/java/compile/AbstractCompiler.java b/maven-mboot2/src/main/java/compile/AbstractCompiler.java index 0a51f4acd9..85744df8c1 100644 --- a/maven-mboot2/src/main/java/compile/AbstractCompiler.java +++ b/maven-mboot2/src/main/java/compile/AbstractCompiler.java @@ -4,33 +4,38 @@ import util.DirectoryScanner; import java.io.File; import java.io.IOException; -import java.util.ArrayList; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; /** - * - * - * @author Jason van Zyl - * @author Michal Maczka - * + * @author Jason van Zyl + * @author Michal Maczka * @version $Id$ */ public abstract class AbstractCompiler implements Compiler { private static String PS = System.getProperty( "path.separator" ); + + /** + * @deprecated Use getPathString(..) instead. + */ + public String getClasspathString( List pathElements ) throws Exception + { + return getPathString( pathElements ); + } - public String getClasspathString( List classpathElements ) + public String getPathString( List pathElements ) throws Exception { StringBuffer sb = new StringBuffer(); - for ( Iterator it = classpathElements.iterator(); it.hasNext(); ) + for ( Iterator it = pathElements.iterator(); it.hasNext(); ) { String element = (String) it.next(); - + sb.append( element ).append( PS ); } @@ -39,46 +44,70 @@ public abstract class AbstractCompiler protected String[] getSourceFiles( CompilerConfiguration config ) { - List sources = new ArrayList(); + Set sources = new HashSet(); - for ( Iterator it = config.getSourceLocations().iterator(); it.hasNext(); ) + Set sourceFiles = config.getSourceFiles(); + if ( sourceFiles != null && !sourceFiles.isEmpty() ) { - String sourceLocation = (String) it.next(); - - DirectoryScanner scanner = new DirectoryScanner(); - - scanner.setBasedir( sourceLocation ); - - Set includes = config.getIncludes(); - if(includes != null && !includes.isEmpty()) { - String[] inclStrs = (String[])includes.toArray(new String[includes.size()]); - scanner.setIncludes( inclStrs ); - } - else { - scanner.setIncludes(new String[] {"**/*.java"}); - } - - Set excludes = config.getIncludes(); - if(excludes != null && !excludes.isEmpty()) { - String[] exclStrs = (String[])excludes.toArray(new String[excludes.size()]); - scanner.setIncludes( exclStrs ); - } - - scanner.scan(); - - String[] sourceDirectorySources = scanner.getIncludedFiles(); - - for ( int j = 0; j < sourceDirectorySources.length; j++ ) + for ( Iterator it = sourceFiles.iterator(); it.hasNext(); ) { - File f = new File( sourceLocation, sourceDirectorySources[j] ); + File sourceFile = (File) it.next(); + sources.add( sourceFile.getAbsolutePath() ); + } + } + else + { + for ( Iterator it = config.getSourceLocations().iterator(); it.hasNext(); ) + { + String sourceLocation = (String) it.next(); - sources.add( f.getPath() ); + DirectoryScanner scanner = new DirectoryScanner(); + + scanner.setBasedir( sourceLocation ); + + Set includes = config.getIncludes(); + if ( includes != null && !includes.isEmpty() ) + { + String[] inclStrs = (String[]) includes.toArray( new String[includes.size()] ); + scanner.setIncludes( inclStrs ); + } + else + { + scanner.setIncludes( new String[] { "**/*.java" } ); + } + + Set excludes = config.getExcludes(); + if ( excludes != null && !excludes.isEmpty() ) + { + String[] exclStrs = (String[]) excludes.toArray( new String[excludes.size()] ); + scanner.setIncludes( exclStrs ); + } + + scanner.scan(); + + String[] sourceDirectorySources = scanner.getIncludedFiles(); + + for ( int j = 0; j < sourceDirectorySources.length; j++ ) + { + File f = new File( sourceLocation, sourceDirectorySources[j] ); + + sources.add( f.getPath() ); + } } } - String[] sourceArray = new String[sources.size()]; + String[] result = null; - return (String[]) sources.toArray( sourceArray ); + if ( sources.isEmpty() ) + { + result = new String[0]; + } + else + { + result = (String[]) sources.toArray( new String[sources.size()] ); + } + + return result; } protected String makeClassName( String fileName, String sourceDir ) @@ -97,8 +126,7 @@ public abstract class AbstractCompiler if ( sourceDir != null ) { - String prefix = - new File( sourceDir ).getCanonicalPath().replace( '\\', '/' ); + String prefix = new File( sourceDir ).getCanonicalPath().replace( '\\', '/' ); if ( canonical != null ) { @@ -147,4 +175,4 @@ public abstract class AbstractCompiler return args; } -} +} \ No newline at end of file diff --git a/maven-mboot2/src/main/java/compile/CompilerConfiguration.java b/maven-mboot2/src/main/java/compile/CompilerConfiguration.java index 999aa60a38..7b6cfaa45d 100644 --- a/maven-mboot2/src/main/java/compile/CompilerConfiguration.java +++ b/maven-mboot2/src/main/java/compile/CompilerConfiguration.java @@ -17,13 +17,32 @@ public class CompilerConfiguration { private String outputLocation; + private List classpathEntries = new LinkedList(); + private List sourceLocations = new LinkedList(); + private Set includes = new HashSet(); private Set excludes = new HashSet(); + private Map compilerOptions = new TreeMap(); + private boolean debug = false; + private Set sourceFiles = new HashSet(); + + private boolean noWarn; + + public void setSourceFiles(Set sourceFiles) + { + this.sourceFiles = sourceFiles; + } + + public Set getSourceFiles() + { + return sourceFiles; + } + public void setOutputLocation(String outputLocation) { this.outputLocation = outputLocation; @@ -112,5 +131,15 @@ public class CompilerConfiguration { return debug; } + + public void setNoWarn( boolean noWarn ) + { + this.noWarn = noWarn; + } + + public boolean isNoWarn() + { + return noWarn; + } } diff --git a/maven-mboot2/src/main/java/compile/CompilerError.java b/maven-mboot2/src/main/java/compile/CompilerError.java index 186517f1f3..4729583eb6 100644 --- a/maven-mboot2/src/main/java/compile/CompilerError.java +++ b/maven-mboot2/src/main/java/compile/CompilerError.java @@ -97,6 +97,18 @@ public class CompilerError this.message = message; } + /** + * The error message constructor. + * + * @param message The actual error text produced by the language processor + * @param error whether it was an error or informational + */ + public CompilerError( String message, boolean error ) + { + this.message = message; + this.error = error; + } + /** * Return the filename associated with this compiler error. * @@ -173,6 +185,13 @@ public class CompilerError public String toString() { - return file + ":" + "[" + startline + "," + startcolumn + "] " + message; + if ( file != null ) + { + return file + ":" + "[" + startline + "," + startcolumn + "] " + message; + } + else + { + return message; + } } } diff --git a/maven-mboot2/src/main/java/compile/JavacCompiler.java b/maven-mboot2/src/main/java/compile/JavacCompiler.java index 60494bab17..f31f9af711 100644 --- a/maven-mboot2/src/main/java/compile/JavacCompiler.java +++ b/maven-mboot2/src/main/java/compile/JavacCompiler.java @@ -25,8 +25,6 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; -import java.io.OutputStream; -import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; @@ -45,7 +43,8 @@ public class JavacCompiler { } - public List compile( CompilerConfiguration config ) throws Exception + public List compile( CompilerConfiguration config ) + throws Exception { File destinationDir = new File( config.getOutputLocation() ); @@ -61,8 +60,9 @@ public class JavacCompiler return Collections.EMPTY_LIST; } - System.out.println( "Compiling " + sources.length + " source file" + ( sources.length == 1 ? "" : "s" ) - + " to " + destinationDir.getAbsolutePath() ); + // TODO: use getLogger() - but for some reason it is null when this is used + System.out.println( "Compiling " + sources.length + " source file" + ( sources.length == 1 ? "" : "s" ) + + " to " + destinationDir.getAbsolutePath() ); Map compilerOptions = config.getCompilerOptions(); @@ -72,25 +72,57 @@ public class JavacCompiler args.add( destinationDir.getAbsolutePath() ); - args.add( "-nowarn" ); + if ( config.isNoWarn() ) + { + args.add( "-nowarn" ); + } - args.add( "-classpath" ); + List classpathEntries = config.getClasspathEntries(); + if ( classpathEntries != null && !classpathEntries.isEmpty() ) + { + args.add( "-classpath" ); - args.add( getClasspathString( config.getClasspathEntries() ) ); + args.add( getPathString( classpathEntries ) ); + } if ( config.isDebug() ) { args.add( "-g" ); } + List sourceLocations = config.getSourceLocations(); + if ( sourceLocations != null && !sourceLocations.isEmpty() ) + { + args.add( "-sourcepath" ); + + args.add( getPathString( sourceLocations ) ); + } + + // TODO: this could be much improved + if ( !compilerOptions.containsKey( "-target" ) ) + { + if ( !compilerOptions.containsKey( "-source" ) ) + { + // If omitted, later JDKs complain about a 1.1 target + args.add( "-source" ); + args.add( "1.3" ); + } + + // Required, or it defaults to the target of your JDK (eg 1.5) + args.add( "-target" ); + args.add( "1.1" ); + } + Iterator it = compilerOptions.entrySet().iterator(); while ( it.hasNext() ) { Map.Entry entry = (Map.Entry) it.next(); args.add( entry.getKey() ); - if ( (entry.getValue() != null) ) + if ( ( entry.getValue() != null ) ) + { args.add( entry.getValue() ); + } } for ( int i = 0; i < sources.length; i++ ) @@ -102,32 +134,34 @@ public class JavacCompiler File toolsJar = new File( System.getProperty( "java.home" ), "../lib/tools.jar" ); - cl.addURL( toolsJar.toURL() ); + if ( toolsJar.exists() ) + { + cl.addURL( toolsJar.toURL() ); + } - Class c = cl.loadClass( "sun.tools.javac.Main" ); - - Constructor cons = c.getConstructor( new Class[] { OutputStream.class, String.class } ); + Class c = cl.loadClass( "com.sun.tools.javac.Main" ); ByteArrayOutputStream err = new ByteArrayOutputStream(); - Object compiler = cons.newInstance( new Object[] { err, "javac" } ); + Method compile = c.getMethod( "compile", new Class[]{String[].class} ); - Method compile = c.getMethod( "compile", new Class[] { String[].class } ); + Integer ok = (Integer) compile.invoke( null, new Object[]{args.toArray( new String[0] )} ); - Boolean ok = (Boolean) compile.invoke( compiler, new Object[] { args.toArray( new String[0] ) } ); + List messages = parseModernStream( + new BufferedReader( new InputStreamReader( new ByteArrayInputStream( err.toByteArray() ) ) ) ); - List messages = parseModernStream( new BufferedReader( new InputStreamReader( new ByteArrayInputStream( err.toByteArray() ) ) ) ); - - if ( !ok.booleanValue() && messages.isEmpty() ) + if ( ok.intValue() != 0 && messages.isEmpty() ) { - // TODO: don't throw exception - throw new Exception( "Failure executing javac, but could not parse the error:\n\n" + err.toString() ); + // TODO: exception? + messages.add( new CompilerError( + "Failure executing javac, but could not parse the error:\n\n" + err.toString(), true ) ); } return messages; } - protected List parseModernStream( BufferedReader input ) throws IOException + protected List parseModernStream( BufferedReader input ) + throws IOException { List errors = new ArrayList(); @@ -143,14 +177,19 @@ public class JavacCompiler // most errors terminate with the '^' char do { - if ( (line = input.readLine()) == null ) + if ( ( line = input.readLine() ) == null ) { return errors; } + // TODO: there should be a better way to parse these if ( buffer.length() == 0 && line.startsWith( "error: " ) ) { - errors.add( new CompilerError( line ) ); + errors.add( new CompilerError( line, true ) ); + } + else if ( buffer.length() == 0 && line.startsWith( "Note: " ) ) + { + // skip this one - it is JDK 1.5 telling us that the interface is deprecated. } else { @@ -200,11 +239,13 @@ public class JavacCompiler } catch ( NoSuchElementException nse ) { - return new CompilerError( "no more tokens - could not parse error message: " + error ); + // TODO: exception? + return new CompilerError( "no more tokens - could not parse error message: " + error, true ); } catch ( Exception nse ) { - return new CompilerError( "could not parse error message: " + error ); + // TODO: exception? + return new CompilerError( "could not parse error message: " + error, true ); } } @@ -212,4 +253,4 @@ public class JavacCompiler { return "Sun Javac Compiler"; } -} \ No newline at end of file +}