Added debug option to compiler configuration.

Copied compiler classes from plexus compiler.
Allow bootstrapping with debug info.


git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163301 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Carlos Sanchez Gonzalez 2005-01-09 22:38:43 +00:00
parent 380b263ad0
commit 763811d0fa
9 changed files with 282 additions and 69 deletions

View File

@ -2,20 +2,18 @@
Bootstrapping Maven Bootstrapping Maven
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
To bootstrap Maven you must have a ~/maven.properties file with the following To bootstrap Maven you must have a ~/.m2/maven.properties file with the following
entries: entry:
maven.home = /path/to/your/maven/installation
maven.repo.local = /path/to/your/local/repository maven.repo.local = /path/to/your/local/repository
Once you have your ~/maven.properties setup then: Set the environment variable M2_HOME pointing to the dir where you want Maven2 installed.
java -jar mboot.jar You can set the parameters passed to the Java VM when running Maven2 bootstrap,
setting the environment variable MAVEN_OPTS, e.g.
e.g. to run in offline mode, set MAVEN_OPTS=-Dmaven.online=false
e.g. to build maven with debug info, set MAVEN_OPTS=-Dmaven.compiler.debug=true
Should do the trick to produce a working installation of Maven Then run m2-bootstrap-all.bat (in Windows) or m2-bootstrap-all.sh (in Unix)
in ${maven.home}.
NOTE: You must run these instructions from this directory! NOTE: You must run these instructions from this directory!
NOTE: If you want to run in offline mode where no downloading is done
then add: 'maven.online = false' to your ~/maven.properties file.

View File

@ -111,7 +111,7 @@ call .\build
@REM Build Maven2 @REM Build Maven2
cd .. cd ..
%MAVEN_JAVA_EXE% %MAVEN_CMD_LINE_ARGS% -jar mboot.jar %MAVEN_JAVA_EXE% %MAVEN_CMD_LINE_ARGS% %MAVEN_OPTS% -jar mboot.jar
echo Running integration tests echo Running integration tests
cd maven-core-it cd maven-core-it

View File

@ -26,7 +26,7 @@ ret=$?; if [ $ret != 0 ]; then exit $ret; fi
echo " Building maven2 components ... " echo " Building maven2 components ... "
echo "-----------------------------------------------------------------------" echo "-----------------------------------------------------------------------"
$JAVA_HOME/bin/java $ARGS -jar mboot.jar $JAVA_HOME/bin/java $ARGS $MAVEN_OPTS -jar mboot.jar
ret=$?; if [ $ret != 0 ]; then exit $ret; fi ret=$?; if [ $ret != 0 ]; then exit $ret; fi
) )
ret=$?; if [ $ret != 0 ]; then exit $ret; fi ret=$?; if [ $ret != 0 ]; then exit $ret; fi

View File

@ -1,4 +1,5 @@
import compile.CompilerConfiguration;
import compile.JavacCompiler; import compile.JavacCompiler;
import download.ArtifactDownloader; import download.ArtifactDownloader;
import jar.JarMojo; import jar.JarMojo;
@ -860,14 +861,31 @@ public class MBoot
if ( sourceDirectories != null ) if ( sourceDirectories != null )
{ {
List errors = compiler.compile( classpath( dependencies, extraClasspath ), sourceDirectories, outputDirectory ); CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
for ( Iterator i = errors.iterator(); i.hasNext(); ) compilerConfiguration.setOutputLocation(outputDirectory);
compilerConfiguration.setClasspathEntries(Arrays.asList(classpath( dependencies, extraClasspath )));
compilerConfiguration.setSourceLocations(Arrays.asList(sourceDirectories));
/* Compile with debugging info */
String debugAsString = System.getProperty( "maven.compiler.debug" );
if ( debugAsString != null )
{
if ( Boolean.valueOf( debugAsString ).booleanValue() )
{
compilerConfiguration.setDebug( true );
}
}
List messages = compiler.compile(compilerConfiguration);
for ( Iterator i = messages.iterator(); i.hasNext(); )
{ {
System.out.println( i.next() ); System.out.println( i.next() );
} }
if ( errors.size() > 0 ) if ( messages.size() > 0 )
{ {
throw new Exception( "Compilation error." ); throw new Exception( "Compilation error." );
} }

View File

@ -5,41 +5,64 @@ import util.DirectoryScanner;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
*
*
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a> * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @author <a href="mailto:michal.maczka@dimatics.com">Michal Maczka</a> * @author <a href="mailto:michal.maczka@dimatics.com">Michal Maczka</a>
*
* @version $Id$ * @version $Id$
*/ */
public abstract class AbstractCompiler public abstract class AbstractCompiler
implements Compiler
{ {
private static String PS = System.getProperty( "path.separator" ); private static String PS = System.getProperty( "path.separator" );
public String getClasspathString( String[] classpathElements ) public String getClasspathString( List classpathElements )
throws Exception throws Exception
{ {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
for ( int i = 0; i < classpathElements.length; i++ ) for ( Iterator it = classpathElements.iterator(); it.hasNext(); )
{ {
sb.append( classpathElements[i] ).append( PS ); String element = (String) it.next();
sb.append( element ).append( PS );
} }
return sb.toString(); return sb.toString();
} }
protected String[] getSourceFiles( String[] sourceDirectories ) protected String[] getSourceFiles( CompilerConfiguration config )
{ {
List sources = new ArrayList(); List sources = new ArrayList();
for ( int i = 0; i < sourceDirectories.length; i++ ) for ( Iterator it = config.getSourceLocations().iterator(); it.hasNext(); )
{ {
String sourceLocation = (String) it.next();
DirectoryScanner scanner = new DirectoryScanner(); DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir( sourceDirectories[i] ); scanner.setBasedir( sourceLocation );
scanner.setIncludes( new String[]{"**/*.java"} ); 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(); scanner.scan();
@ -47,7 +70,7 @@ public abstract class AbstractCompiler
for ( int j = 0; j < sourceDirectorySources.length; j++ ) for ( int j = 0; j < sourceDirectorySources.length; j++ )
{ {
File f = new File( sourceDirectories[i], sourceDirectorySources[j] ); File f = new File( sourceLocation, sourceDirectorySources[j] );
sources.add( f.getPath() ); sources.add( f.getPath() );
} }

View File

@ -0,0 +1,20 @@
package compile;
import java.util.List;
import java.util.Map;
/**
*
*
* @author <a href="mailto:jason@plexus.org">Jason van Zyl</a>
*
* @version $Id$
*/
public interface Compiler
{
static String ROLE = Compiler.class.getName();
List compile( CompilerConfiguration configuration )
throws Exception;
}

View File

@ -0,0 +1,116 @@
/* Created on Oct 4, 2004 */
package compile;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* @author jdcasey
*/
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;
public void setOutputLocation(String outputLocation)
{
this.outputLocation = outputLocation;
}
public String getOutputLocation()
{
return outputLocation;
}
public void addClasspathEntry(String classpathEntry)
{
this.classpathEntries.add(classpathEntry);
}
public void setClasspathEntries(List classpathEntries) {
this.classpathEntries = new LinkedList(classpathEntries);
}
public List getClasspathEntries() {
return Collections.unmodifiableList(classpathEntries);
}
public void addSourceLocation(String sourceLocation) {
this.sourceLocations.add(sourceLocation);
}
public void setSourceLocations(List sourceLocations) {
this.sourceLocations = new LinkedList(sourceLocations);
}
public List getSourceLocations() {
return Collections.unmodifiableList(sourceLocations);
}
public void addInclude(String include) {
this.includes.add(include);
}
public void setIncludes(Set includes) {
this.includes = new HashSet(includes);
}
public Set getIncludes() {
return Collections.unmodifiableSet(includes);
}
public void addExclude(String exclude) {
this.excludes.add(exclude);
}
public void setExcludes(Set excludes) {
this.excludes = new HashSet(excludes);
}
public Set getExcludes() {
return Collections.unmodifiableSet(excludes);
}
public void addCompilerOption(String optionName, String optionValue) {
this.compilerOptions.put(optionName, optionValue);
}
public void setCompilerOptions(Map compilerOptions) {
this.compilerOptions = new TreeMap(compilerOptions);
}
public Map getCompilerOptions() {
return Collections.unmodifiableMap(compilerOptions);
}
/**
* @param debug The debug to set.
*/
public void setDebug( boolean debug )
{
this.debug = debug;
}
/**
* Compile with debug info
*
* @return Returns the debug.
*/
public boolean isDebug()
{
return debug;
}
}

View File

@ -1,3 +1,20 @@
/**
*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package compile; package compile;
/** /**
@ -51,13 +68,15 @@ public class CompilerError
* @param endcolumn The end column number of the offending program text * @param endcolumn The end column number of the offending program text
* @param message The actual error text produced by the language processor * @param message The actual error text produced by the language processor
*/ */
public CompilerError( String file, public CompilerError(
String file,
boolean error, boolean error,
int startline, int startline,
int startcolumn, int startcolumn,
int endline, int endline,
int endcolumn, int endcolumn,
String message ) String message
)
{ {
this.file = file; this.file = file;
this.error = error; this.error = error;

View File

@ -1,6 +1,21 @@
package compile; /**
*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import util.IsolatedClassLoader; package compile;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@ -12,10 +27,14 @@ import java.io.OutputStream;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import util.IsolatedClassLoader;
public class JavacCompiler public class JavacCompiler
extends AbstractCompiler extends AbstractCompiler
{ {
@ -25,42 +44,44 @@ public class JavacCompiler
{ {
} }
public List compile( String[] classpathElements, String[] sourceDirectories, String destinationDirectory ) public List compile( CompilerConfiguration config ) throws Exception
throws Exception
{ {
/* File destinationDir = new File( config.getOutputLocation() );
for ( int i = 0; i < classpathElements.length; i++ )
{
System.out.println( "classpathElement = " + classpathElements[i] );
}
*/
File destinationDir = new File( destinationDirectory );
if ( !destinationDir.exists() ) if ( !destinationDir.exists() )
{ {
destinationDir.mkdirs(); destinationDir.mkdirs();
} }
String[] sources = getSourceFiles( sourceDirectories ); String[] sources = getSourceFiles( config );
int j = 5; Map compilerOptions = config.getCompilerOptions();
String[] args = new String[sources.length + j]; List args = new ArrayList( sources.length + 5 + compilerOptions.size() * 2 );
args[0] = "-d"; args.add( "-d" );
args[1] = destinationDir.getAbsolutePath(); args.add( destinationDir.getAbsolutePath() );
args[2] = "-nowarn"; args.add( "-nowarn" );
args[3] = "-classpath"; args.add( "-classpath" );
args[4] = getClasspathString( classpathElements ); args.add( getClasspathString( config.getClasspathEntries() ) );
Iterator it = compilerOptions.entrySet().iterator();
while ( it.hasNext() )
{
Map.Entry entry = (Map.Entry) it.next();
args.add( entry.getKey() );
if ( (entry.getValue() != null) )
args.add( entry.getValue() );
}
for ( int i = 0; i < sources.length; i++ ) for ( int i = 0; i < sources.length; i++ )
{ {
args[i + j] = sources[i]; args.add( sources[i] );
} }
IsolatedClassLoader cl = new IsolatedClassLoader(); IsolatedClassLoader cl = new IsolatedClassLoader();
@ -71,23 +92,22 @@ public class JavacCompiler
Class c = cl.loadClass( "sun.tools.javac.Main" ); Class c = cl.loadClass( "sun.tools.javac.Main" );
Constructor cons = c.getConstructor( new Class[]{OutputStream.class, String.class} ); Constructor cons = c.getConstructor( new Class[] { OutputStream.class, String.class } );
ByteArrayOutputStream err = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream();
Object compiler = cons.newInstance( new Object[]{err, "javac"} ); 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 } );
Boolean ok = (Boolean) compile.invoke( compiler, new Object[]{args} ); 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() ) ) ) );
return messages; return messages;
} }
protected List parseModernStream( BufferedReader input ) protected List parseModernStream( BufferedReader input ) throws IOException
throws IOException
{ {
List errors = new ArrayList(); List errors = new ArrayList();
@ -103,7 +123,7 @@ public class JavacCompiler
// most errors terminate with the '^' char // most errors terminate with the '^' char
do do
{ {
if ( ( line = input.readLine() ) == null ) if ( (line = input.readLine()) == null )
{ {
return errors; return errors;
} }
@ -111,8 +131,7 @@ public class JavacCompiler
buffer.append( line ); buffer.append( line );
buffer.append( '\n' ); buffer.append( '\n' );
} } while ( !line.endsWith( "^" ) );
while ( !line.endsWith( "^" ) );
// add the error bean // add the error bean
errors.add( parseModernError( buffer.toString() ) ); errors.add( parseModernError( buffer.toString() ) );