diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java index 772f984494..78b054c0a9 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java @@ -40,6 +40,7 @@ import org.apache.maven.Maven; import org.apache.maven.cli.event.DefaultEventSpyContext; import org.apache.maven.cli.event.ExecutionEventLogger; import org.apache.maven.cli.logging.Slf4jLoggerManager; +import org.apache.maven.cli.logging.Slf4jStdoutLogger; import org.apache.maven.cli.transfer.ConsoleMavenTransferListener; import org.apache.maven.cli.transfer.QuietMavenTransferListener; import org.apache.maven.cli.transfer.Slf4jMavenTransferListener; @@ -262,6 +263,12 @@ public class MavenCli private void cli( CliRequest cliRequest ) throws Exception { + // + // Parsing errors can happen during the processing of the arguments and we prefer not having to check if the logger is null + // and make construct this so we can use an SLF4J logger everywhere + // + slf4jLogger = new Slf4jStdoutLogger(); + CLIManager cliManager = new CLIManager(); try diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/logging/PrintStreamLogger.java b/maven-embedder/src/main/java/org/apache/maven/cli/logging/PrintStreamLogger.java deleted file mode 100644 index 53025a0daa..0000000000 --- a/maven-embedder/src/main/java/org/apache/maven/cli/logging/PrintStreamLogger.java +++ /dev/null @@ -1,187 +0,0 @@ -package org.apache.maven.cli.logging; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 java.io.PrintStream; - -import org.apache.maven.Maven; -import org.codehaus.plexus.logging.AbstractLogger; -import org.codehaus.plexus.logging.Logger; - -/** - * Logs to a user-supplied {@link PrintStream}. - * - * @author Benjamin Bentmann - */ -public class PrintStreamLogger - extends AbstractLogger -{ - - public interface Provider - { - PrintStream getStream(); - } - - private Provider provider; - - private static final String FATAL_ERROR = "[FATAL] "; - - private static final String ERROR = "[ERROR] "; - - private static final String WARNING = "[WARNING] "; - - private static final String INFO = "[INFO] "; - - private static final String DEBUG = "[DEBUG] "; - - public PrintStreamLogger( Provider provider ) - { - super( Logger.LEVEL_INFO, Maven.class.getName() ); - - if ( provider == null ) - { - throw new IllegalArgumentException( "output stream provider missing" ); - } - this.provider = provider; - } - - public PrintStreamLogger( PrintStream out ) - { - super( Logger.LEVEL_INFO, Maven.class.getName() ); - - setStream( out ); - } - - public void setStream( final PrintStream out ) - { - if ( out == null ) - { - throw new IllegalArgumentException( "output stream missing" ); - } - - this.provider = new Provider() - { - public PrintStream getStream() - { - return out; - } - }; - } - - public void debug( String message, Throwable throwable ) - { - if ( isDebugEnabled() ) - { - PrintStream out = provider.getStream(); - - out.print( DEBUG ); - out.println( message ); - - if ( null != throwable ) - { - throwable.printStackTrace( out ); - } - } - } - - public void info( String message, Throwable throwable ) - { - if ( isInfoEnabled() ) - { - PrintStream out = provider.getStream(); - - out.print( INFO ); - out.println( message ); - - if ( null != throwable ) - { - throwable.printStackTrace( out ); - } - } - } - - public void warn( String message, Throwable throwable ) - { - if ( isWarnEnabled() ) - { - PrintStream out = provider.getStream(); - - out.print( WARNING ); - out.println( message ); - - if ( null != throwable ) - { - throwable.printStackTrace( out ); - } - } - } - - public void error( String message, Throwable throwable ) - { - if ( isErrorEnabled() ) - { - PrintStream out = provider.getStream(); - - out.print( ERROR ); - out.println( message ); - - if ( null != throwable ) - { - throwable.printStackTrace( out ); - } - } - } - - public void fatalError( String message, Throwable throwable ) - { - if ( isFatalErrorEnabled() ) - { - PrintStream out = provider.getStream(); - - out.print( FATAL_ERROR ); - out.println( message ); - - if ( null != throwable ) - { - throwable.printStackTrace( out ); - } - } - } - - public void close() - { - PrintStream out = provider.getStream(); - - if ( out == System.out || out == System.err ) - { - out.flush(); - } - else - { - out.close(); - } - } - - public Logger getChildLogger( String arg0 ) - { - return this; - } - -} diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/logging/Slf4jStdoutLogger.java b/maven-embedder/src/main/java/org/apache/maven/cli/logging/Slf4jStdoutLogger.java new file mode 100644 index 0000000000..f1a27ae511 --- /dev/null +++ b/maven-embedder/src/main/java/org/apache/maven/cli/logging/Slf4jStdoutLogger.java @@ -0,0 +1,284 @@ +package org.apache.maven.cli.logging; + +import java.io.PrintStream; + +import org.slf4j.Logger; +import org.slf4j.Marker; + +public class Slf4jStdoutLogger + implements Logger +{ + private static final String ERROR = "[ERROR] "; + + private PrintStream out = System.out; + + // + // These are the only methods we need in our primordial logger + // + public void error( String msg ) + { + out.print( ERROR ); + out.println( msg ); + } + + public void error( String msg, Throwable t ) + { + error( msg ); + + if ( null != t ) + { + t.printStackTrace( out ); + } + } + + // + // Don't need any of this + // + public String getName() + { + return null; + } + + public boolean isTraceEnabled() + { + return false; + } + + public void trace( String msg ) + { + } + + public void trace( String format, Object arg ) + { + } + + public void trace( String format, Object arg1, Object arg2 ) + { + } + + public void trace( String format, Object... arguments ) + { + } + + public void trace( String msg, Throwable t ) + { + } + + public boolean isTraceEnabled( Marker marker ) + { + return false; + } + + public void trace( Marker marker, String msg ) + { + } + + public void trace( Marker marker, String format, Object arg ) + { + } + + public void trace( Marker marker, String format, Object arg1, Object arg2 ) + { + } + + public void trace( Marker marker, String format, Object... argArray ) + { + } + + public void trace( Marker marker, String msg, Throwable t ) + { + } + + public boolean isDebugEnabled() + { + return false; + } + + public void debug( String msg ) + { + } + + public void debug( String format, Object arg ) + { + } + + public void debug( String format, Object arg1, Object arg2 ) + { + } + + public void debug( String format, Object... arguments ) + { + } + + public void debug( String msg, Throwable t ) + { + } + + public boolean isDebugEnabled( Marker marker ) + { + return false; + } + + public void debug( Marker marker, String msg ) + { + } + + public void debug( Marker marker, String format, Object arg ) + { + } + + public void debug( Marker marker, String format, Object arg1, Object arg2 ) + { + } + + public void debug( Marker marker, String format, Object... arguments ) + { + } + + public void debug( Marker marker, String msg, Throwable t ) + { + } + + public boolean isInfoEnabled() + { + return false; + } + + public void info( String msg ) + { + } + + public void info( String format, Object arg ) + { + } + + public void info( String format, Object arg1, Object arg2 ) + { + } + + public void info( String format, Object... arguments ) + { + } + + public void info( String msg, Throwable t ) + { + } + + public boolean isInfoEnabled( Marker marker ) + { + return false; + } + + public void info( Marker marker, String msg ) + { + } + + public void info( Marker marker, String format, Object arg ) + { + } + + public void info( Marker marker, String format, Object arg1, Object arg2 ) + { + } + + public void info( Marker marker, String format, Object... arguments ) + { + } + + public void info( Marker marker, String msg, Throwable t ) + { + } + + public boolean isWarnEnabled() + { + return false; + } + + public void warn( String msg ) + { + } + + public void warn( String format, Object arg ) + { + } + + public void warn( String format, Object... arguments ) + { + } + + public void warn( String format, Object arg1, Object arg2 ) + { + } + + public void warn( String msg, Throwable t ) + { + } + + public boolean isWarnEnabled( Marker marker ) + { + return false; + } + + public void warn( Marker marker, String msg ) + { + } + + public void warn( Marker marker, String format, Object arg ) + { + } + + public void warn( Marker marker, String format, Object arg1, Object arg2 ) + { + } + + public void warn( Marker marker, String format, Object... arguments ) + { + } + + public void warn( Marker marker, String msg, Throwable t ) + { + } + + public boolean isErrorEnabled() + { + return false; + } + + public void error( String format, Object arg ) + { + } + + public void error( String format, Object arg1, Object arg2 ) + { + } + + public void error( String format, Object... arguments ) + { + } + + public boolean isErrorEnabled( Marker marker ) + { + return false; + } + + public void error( Marker marker, String msg ) + { + } + + public void error( Marker marker, String format, Object arg ) + { + } + + public void error( Marker marker, String format, Object arg1, Object arg2 ) + { + } + + public void error( Marker marker, String format, Object... arguments ) + { + } + + public void error( Marker marker, String msg, Throwable t ) + { + } + +}