o first pass at allowing the logging to be configurable

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@292888 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jason van Zyl 2005-10-01 01:17:36 +00:00
parent f96ed8ac07
commit 7ff412aecf
10 changed files with 641 additions and 18 deletions

View File

@ -1,8 +1,11 @@
import org.apache.maven.cli.ConsoleDownloadMonitor;
import org.apache.maven.embedder.*;
import org.apache.maven.project.*;
import org.apache.maven.monitor.event.*;
import java.io.*;
import java.util.*;
import org.codehaus.plexus.logging.*;
import org.codehaus.plexus.logging.console.*;
public class Plugin
{
@ -14,6 +17,8 @@ public Plugin()
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
maven.setClassLoader( classLoader );
maven.setLogger( new MavenEmbedderConsoleLogger() );
maven.start();
@ -29,9 +34,13 @@ public Plugin()
MavenProject pom = maven.readProjectWithDependencies( pomFile );
EventDispatcher eventDispatcher = new DefaultEventDispatcher();
maven.execute( pom, Collections.singletonList( "package" ), eventDispatcher, null, targetDirectory );
EventMonitor eventMonitor = new DefaultEventMonitor( new PlexusLoggerAdapter( new MavenEmbedderConsoleLogger() ) );
System.out.println( "<<<<<<<<<<<<<<<<<<<<<<<<<");
maven.execute( pom, Collections.singletonList( "package" ), eventMonitor, new ConsoleDownloadMonitor(), targetDirectory );
System.out.println( "<<<<<<<<<<<<<<<<<<<<<<<<<");
}
public static void main( String[] args )

View File

@ -1,3 +1,10 @@
o need to check in changes to plexus, maven-core, embedder
o task segment still using the wrong logger
x child container
x mojos need to feed into the logging
- surefire needs to feed into the logging
When you create the assembly you must manually install it in your
local repository for now in order the maven-embedder-it tests to
work. jvz.
@ -67,3 +74,12 @@ plugin registry and settings should be stored in the tool specific locations
<eu> one more thing I need in embedder - writeModel() method
<jdcasey> you know something else that would rock for the eclipse plugin? if
F5 triggered a re-resolution of the poms, and recreation of the .classpath,
etc.
<jdcasey> that way, if the pom was upgraded to the next version-SNAPSHOT,
you'd get it
<jdcasey> :)
<jdcasey> configurable on|off, of course
<jdcasey> F5 is refresh, BTW

View File

@ -21,6 +21,11 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-container-default</artifactId>
<version>1.0-alpha-7-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings</artifactId>
@ -58,10 +63,6 @@
<artifactId>maven-artifact</artifactId>
<version>2.0-beta-3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-container-default</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-provider-api</artifactId>

View File

@ -0,0 +1,124 @@
package org.apache.maven.embedder;
/*
* Copyright 2001-2005 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.
*/
/**
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
* @version $Id$
*/
public abstract class AbstractMavenEmbedderLogger
implements MavenEmbedderLogger
{
private int threshold = MavenEmbedderLogger.LEVEL_INFO;
private String name;
public int getThreshold()
{
return threshold;
}
public void setThreshold( int threshold )
{
this.threshold = threshold;
}
public String getName()
{
return name;
}
public void debug( String message )
{
debug( message, null );
}
public boolean isDebugEnabled()
{
return threshold <= LEVEL_DEBUG;
}
public void info( String message )
{
info( message, null );
}
public boolean isInfoEnabled()
{
return threshold <= LEVEL_INFO;
}
public void warn( String message )
{
warn( message, null );
}
public boolean isWarnEnabled()
{
return threshold <= LEVEL_WARN;
}
public void error( String message )
{
error( message, null );
}
public boolean isErrorEnabled()
{
return threshold <= LEVEL_ERROR;
}
public void fatalError( String message )
{
fatalError( message, null );
}
public boolean isFatalErrorEnabled()
{
return threshold <= LEVEL_FATAL;
}
protected boolean isValidThreshold( int threshold )
{
if ( threshold == LEVEL_DEBUG )
{
return true;
}
if ( threshold == LEVEL_INFO )
{
return true;
}
if ( threshold == LEVEL_WARN )
{
return true;
}
if ( threshold == LEVEL_ERROR )
{
return true;
}
if ( threshold == LEVEL_FATAL )
{
return true;
}
if ( threshold == LEVEL_DISABLED )
{
return true;
}
return false;
}
}

View File

@ -44,6 +44,8 @@
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.MavenExecutionResponse;
import org.apache.maven.monitor.event.EventDispatcher;
import org.apache.maven.monitor.event.DefaultEventDispatcher;
import org.apache.maven.monitor.event.EventMonitor;
import org.codehaus.classworlds.ClassWorld;
import org.codehaus.classworlds.DuplicateRealmException;
import org.codehaus.plexus.PlexusContainerException;
@ -106,6 +108,8 @@ public class MavenEmbedder
private PluginDescriptorBuilder pluginDescriptorBuilder;
private EventDispatcher eventDispatcher;
// ----------------------------------------------------------------------
// Configuration
// ----------------------------------------------------------------------
@ -118,6 +122,8 @@ public class MavenEmbedder
private ClassLoader classLoader;
private MavenEmbedderLogger logger;
// ----------------------------------------------------------------------
// User options
// ----------------------------------------------------------------------
@ -235,6 +241,16 @@ public File getLocalRepositoryDirectory()
return localRepositoryDirectory;
}
public MavenEmbedderLogger getLogger()
{
return logger;
}
public void setLogger( MavenEmbedderLogger logger )
{
this.logger = logger;
}
// ----------------------------------------------------------------------
// Embedder Client Contract
// ----------------------------------------------------------------------
@ -354,23 +370,25 @@ private SummaryPluginDescriptor makeMockPlugin( String groupId, String artifactI
public void execute( MavenProject project,
List goals,
EventDispatcher eventDispatcher,
EventMonitor eventMonitor,
TransferListener transferListener,
File executionRootDirectory )
throws CycleDetectedException, LifecycleExecutionException, MojoExecutionException
{
execute( Collections.singletonList( project ), goals, eventDispatcher, transferListener, executionRootDirectory );
execute( Collections.singletonList( project ), goals, eventMonitor, transferListener, executionRootDirectory );
}
public void execute( List projects,
List goals,
EventDispatcher eventDispatcher,
EventMonitor eventMonitor,
TransferListener transferListener,
File executionRootDirectory )
throws CycleDetectedException, LifecycleExecutionException, MojoExecutionException
{
ReactorManager rm = new ReactorManager( projects );
eventDispatcher.addEventMonitor( eventMonitor );
rm.setFailureBehavior( ReactorManager.FAIL_AT_END );
MavenSession session = new MavenSession( embedder.getContainer(),
@ -531,6 +549,13 @@ public void start()
embedder = new Embedder();
if ( logger != null )
{
System.out.println( "logger = " + logger );
embedder.setLoggerManager( new MavenEmbedderLoggerManager( new PlexusLoggerAdapter( logger ) ) );
}
try
{
ClassWorld classWorld = new ClassWorld();
@ -550,6 +575,8 @@ public void start()
pluginDescriptorBuilder = new PluginDescriptorBuilder();
eventDispatcher = new DefaultEventDispatcher();
profileManager = new DefaultProfileManager( embedder.getContainer() );
mavenProjectBuilder = (MavenProjectBuilder) embedder.lookup( MavenProjectBuilder.ROLE );

View File

@ -0,0 +1,97 @@
package org.apache.maven.embedder;/*
* Copyright 2001-2005 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.
*/
/**
* Logger sending everything to the standard output streams.
* This is mainly for the cases when you have a utility that
* does not have a logger to supply.
*
* @author <a href="mailto:dev@avalon.codehaus.org">Avalon Development Team</a>
* @version $Id$
*/
public final class MavenEmbedderConsoleLogger
extends AbstractMavenEmbedderLogger
{
public void debug( String message, Throwable throwable )
{
if ( isDebugEnabled() )
{
System.out.print( "[ maven embedder DEBUG] " );
System.out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( System.out );
}
}
}
public void info( String message, Throwable throwable )
{
if ( isInfoEnabled() )
{
System.out.print( "[ maven embedder INFO] " );
System.out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( System.out );
}
}
}
public void warn( String message, Throwable throwable )
{
if ( isWarnEnabled() )
{
System.out.print( "[ maven embedder WARNING] " );
System.out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( System.out );
}
}
}
public void error( String message, Throwable throwable )
{
if ( isErrorEnabled() )
{
System.out.print( "[ maven embedder ERROR] " );
System.out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( System.out );
}
}
}
public void fatalError( String message, Throwable throwable )
{
if ( isFatalErrorEnabled() )
{
System.out.print( "[ maven embedder FATAL ERROR] " );
System.out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( System.out );
}
}
}
}

View File

@ -0,0 +1,70 @@
package org.apache.maven.embedder;
/*
* Copyright 2001-2005 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.
*/
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id:$
*/
public interface MavenEmbedderLogger
{
int LEVEL_DEBUG = 0;
int LEVEL_INFO = 1;
int LEVEL_WARN = 2;
int LEVEL_ERROR = 3;
int LEVEL_FATAL = 4;
int LEVEL_DISABLED = 5;
void debug( String message );
void debug( String message, Throwable throwable );
boolean isDebugEnabled();
void info( String message );
void info( String message, Throwable throwable );
boolean isInfoEnabled();
void warn( String message );
void warn( String message, Throwable throwable );
boolean isWarnEnabled();
void error( String message );
void error( String message, Throwable throwable );
boolean isErrorEnabled();
void fatalError( String message );
void fatalError( String message, Throwable throwable );
boolean isFatalErrorEnabled();
void setThreshold( int threshold );
int getThreshold();
}

View File

@ -0,0 +1,148 @@
package org.apache.maven.embedder;
/*
* Copyright 2001-2005 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 org.codehaus.plexus.logging.AbstractLoggerManager;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.logging.LoggerManager;
import org.codehaus.plexus.logging.console.ConsoleLogger;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
/**
* This is a simple logger manager that will only write the logging statements to the console.
* <p/>
* Sample configuration:
* <pre>
* <logging>
* <implementation>org.codehaus.plexus.logging.ConsoleLoggerManager</implementation>
* <logger>
* <threshold>DEBUG</threshold>
* </logger>
* </logging>
* </pre>
*
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
* @version $Id$
*/
public class MavenEmbedderLoggerManager
extends AbstractLoggerManager
implements LoggerManager, Initializable
{
/**
* Message of this level or higher will be logged.
* <p/>
* This field is set by the plexus container thus the name is 'threshold'. The field
* currentThreshold contains the current setting of the threshold.
*/
private String threshold = "info";
private int currentThreshold;
private Logger logger;
public MavenEmbedderLoggerManager( Logger logger )
{
this.logger = logger;
}
public void initialize()
{
debug( "Initializing ConsoleLoggerManager: " + this.hashCode() + "." );
currentThreshold = parseThreshold( threshold );
if ( currentThreshold == -1 )
{
debug( "Could not parse the threshold level: '" + threshold + "', setting to debug." );
currentThreshold = Logger.LEVEL_DEBUG;
}
}
public void setThreshold( int currentThreshold )
{
this.currentThreshold = currentThreshold;
}
/**
* @return Returns the threshold.
*/
public int getThreshold()
{
return currentThreshold;
}
public void setThreshold( String role, String roleHint, int threshold )
{
}
public int getThreshold( String role, String roleHint )
{
return currentThreshold;
}
public Logger getLoggerForComponent( String role, String roleHint )
{
return logger;
}
public void returnComponentLogger( String role, String roleHint )
{
}
public int getActiveLoggerCount()
{
return 1;
}
private int parseThreshold( String text )
{
text = text.trim().toLowerCase();
if ( text.equals( "debug" ) )
{
return ConsoleLogger.LEVEL_DEBUG;
}
else if ( text.equals( "info" ) )
{
return ConsoleLogger.LEVEL_INFO;
}
else if ( text.equals( "warn" ) )
{
return ConsoleLogger.LEVEL_WARN;
}
else if ( text.equals( "error" ) )
{
return ConsoleLogger.LEVEL_ERROR;
}
else if ( text.equals( "fatal" ) )
{
return ConsoleLogger.LEVEL_FATAL;
}
return -1;
}
/**
* Remove this method and all references when this code is verified.
*
* @param msg
*/
private void debug( String msg )
{
}
}

View File

@ -0,0 +1,127 @@
package org.apache.maven.embedder;
import org.codehaus.plexus.logging.Logger;/*
* Copyright 2001-2005 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.
*/
/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @version $Id:$
*/
public class PlexusLoggerAdapter
implements Logger
{
private MavenEmbedderLogger logger;
public PlexusLoggerAdapter( MavenEmbedderLogger logger )
{
this.logger = logger;
}
public void debug( String message )
{
logger.debug( message );
}
public void debug( String message, Throwable throwable )
{
logger.debug( message, throwable );
}
public boolean isDebugEnabled()
{
return logger.isDebugEnabled();
}
public void info( String message )
{
logger.info( message );
}
public void info( String message, Throwable throwable )
{
logger.info( message, throwable );
}
public boolean isInfoEnabled()
{
return logger.isInfoEnabled();
}
public void warn( String message )
{
logger.warn( message );
}
public void warn( String message, Throwable throwable )
{
logger.warn( message, throwable );
}
public boolean isWarnEnabled()
{
return logger.isWarnEnabled();
}
public void error( String message )
{
logger.error( message );
}
public void error( String message, Throwable throwable )
{
logger.error( message );
}
public boolean isErrorEnabled()
{
return logger.isErrorEnabled();
}
public void fatalError( String message )
{
logger.fatalError( message );
}
public void fatalError( String message, Throwable throwable )
{
logger.fatalError( message, throwable );
}
public boolean isFatalErrorEnabled()
{
return logger.isFatalErrorEnabled();
}
public void setThreshold( int i )
{
logger.setThreshold( i );
}
public int getThreshold()
{
return logger.getThreshold();
}
public String getName()
{
return toString();
}
public Logger getChildLogger( String name )
{
return this;
}
}

View File

@ -1,19 +1,21 @@
package org.apache.maven.embedder;
import junit.framework.TestCase;
import org.apache.maven.model.Model;
import org.apache.maven.project.MavenProject;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.monitor.event.EventDispatcher;
import org.apache.maven.monitor.event.DefaultEventDispatcher;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.cli.ConsoleDownloadMonitor;
import org.apache.maven.model.Model;
import org.apache.maven.monitor.event.DefaultEventMonitor;
import org.apache.maven.monitor.event.EventMonitor;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.logging.console.ConsoleLogger;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.util.Set;
import java.util.Collections;
import java.util.List;
import java.util.Set;
public class MavenEmbedderTest
extends TestCase
@ -35,6 +37,8 @@ protected void setUp()
maven.setClassLoader( classLoader );
maven.setLogger( new MavenEmbedderConsoleLogger() );
maven.start();
}
@ -69,9 +73,9 @@ public void testPhaseExecution()
MavenProject pom = maven.readProjectWithDependencies( pomFile );
EventDispatcher eventDispatcher = new DefaultEventDispatcher();
EventMonitor eventMonitor = new DefaultEventMonitor( new PlexusLoggerAdapter( new MavenEmbedderConsoleLogger() ) );
maven.execute( pom, Collections.singletonList( "package" ), eventDispatcher, new ConsoleDownloadMonitor(), targetDirectory );
maven.execute( pom, Collections.singletonList( "package" ), eventMonitor, new ConsoleDownloadMonitor(), targetDirectory );
File jar = new File( targetDirectory, "target/embedder-test-project-1.0-SNAPSHOT.jar" );