Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'JENKINS-15025'
  • Loading branch information
Nikolas Falco committed Jul 15, 2013
2 parents ef08900 + 588e9cb commit 361071d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 19 deletions.
26 changes: 7 additions & 19 deletions maven-plugin/src/main/java/hudson/maven/Maven3Builder.java
Expand Up @@ -94,8 +94,9 @@ public Result call() throws IOException {

registerSystemProperties();

listener.getLogger().println(formatArgs(goals));
PrintStream logger = listener.getLogger();

logger.println(formatArgs(goals));

int r = Maven3Main.launch( goals.toArray(new String[goals.size()]));

Expand All @@ -111,7 +112,6 @@ public Result call() throws IOException {

if(profile) {
NumberFormat n = NumberFormat.getInstance();
PrintStream logger = listener.getLogger();
logger.println("Total overhead was "+format(n,mavenExecutionListener.overheadTime)+"ms");
Channel ch = Channel.current();
logger.println("Class loading " +format(n,ch.classLoadingTime.get()) +"ms, "+ch.classLoadingCount+" classes");
Expand All @@ -120,30 +120,18 @@ public Result call() throws IOException {

mavenExecutionResult = Maven3Launcher.getMavenExecutionResult();

PrintStream logger = listener.getLogger();

if(r==0 && mavenExecutionResult.getThrowables().isEmpty()) {
if(mavenExecutionListener.hasTestFailures()){
return Result.UNSTABLE;
}
return Result.SUCCESS;
}

if (!mavenExecutionResult.getThrowables().isEmpty()) {
logger.println( "mavenExecutionResult exceptions not empty");
for(Throwable throwable : mavenExecutionResult.getThrowables()) {
logger.println("message : " + throwable.getMessage());
if (throwable.getCause()!=null) {
logger.println("cause : " + throwable.getCause().getMessage());
}
logger.println("Stack trace : ");
throwable.printStackTrace( logger );
}

}

// manage of Maven error are moved to ExecutionEventLogger, they are
// threaded as in MavenCli

if(markAsSuccess) {
listener.getLogger().println(Messages.MavenBuilder_Failed());
logger.println(Messages.MavenBuilder_Failed());
if(mavenExecutionListener.hasTestFailures()){
return Result.UNSTABLE;
}
Expand Down Expand Up @@ -200,7 +188,7 @@ public MavenExecutionListener(AbstractMavenBuilder maven3Builder) {

// TODO: we should think about reusing the code in org.apache.maven.cli.DefaultMavenExecutionRequestBuilder#logging?
// E.g. there's also the option to redirect logging to a file which is handled there, but not here.
PrintStreamLogger logger = new PrintStreamLogger( maven3Builder.listener.getLogger() );
PrintStreamLogger logger = new PrintStreamLogger( /* maven3Builder.listener.getLogger() */ System.out );
if (maven3Builder.isDebug()) {
logger.setThreshold(PrintStreamLogger.LEVEL_DEBUG);
} else if (maven3Builder.isQuiet()) {
Expand Down
Expand Up @@ -23,7 +23,13 @@

import java.io.IOException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.maven.InternalErrorException;
import org.apache.maven.exception.DefaultExceptionHandler;
import org.apache.maven.exception.ExceptionHandler;
import org.apache.maven.exception.ExceptionSummary;
import org.apache.maven.execution.AbstractExecutionListener;
import org.apache.maven.execution.BuildFailure;
import org.apache.maven.execution.BuildSuccess;
Expand All @@ -34,6 +40,7 @@
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.StringUtils;

/**
* Logs execution events to a user-supplied logger.
Expand Down Expand Up @@ -139,6 +146,8 @@ public void sessionEnded( ExecutionEvent event )

logger.info( chars( '-', LINE_LENGTH ) );
}

logErrors( event.getSession() );
}

private void logReactorSummary( MavenSession session )
Expand Down Expand Up @@ -187,6 +196,72 @@ else if ( buildSummary instanceof BuildFailure )
}
}

private void logErrors( MavenSession session ) {
MavenExecutionResult result = session.getResult();

// show all errors and them references as in MavenCli
if (!result.getExceptions().isEmpty()) {
ExceptionHandler handler = new DefaultExceptionHandler();

Map<String, String> references = new LinkedHashMap<String, String>();

for (Throwable exception : result.getExceptions()) {
ExceptionSummary summary = handler.handleException(exception);

logErrorSummary(summary, references, "", logger.isDebugEnabled());
}

if (!references.isEmpty()) {
logger.error("For more information about the errors and possible solutions"
+ ", please read the following articles:");

for (Map.Entry<String, String> entry : references.entrySet()) {
logger.error(entry.getValue() + " " + entry.getKey());
}
}
}
}

private void logErrorSummary(ExceptionSummary summary, Map<String, String> references, String indent, boolean showErrors) {
String referenceKey = "";

if (StringUtils.isNotEmpty(summary.getReference())) {
referenceKey = references.get(summary.getReference());
if (referenceKey == null) {
referenceKey = "[Help " + (references.size() + 1) + "]";
references.put(summary.getReference(), referenceKey);
}
}

String msg = summary.getMessage();

if (StringUtils.isNotEmpty(referenceKey)) {
if (msg.indexOf('\n') < 0) {
msg += " -> " + referenceKey;
} else {
msg += "\n-> " + referenceKey;
}
}

String[] lines = msg.split("(\r\n)|(\r)|(\n)");

for (int i = 0; i < lines.length; i++) {
String line = indent + lines[i].trim();

if (i == lines.length - 1 && (showErrors || (summary.getException() instanceof InternalErrorException))) {
logger.error(line, summary.getException());
} else {
logger.error(line);
}
}

indent += " ";

for (ExceptionSummary child : summary.getChildren()) {
logErrorSummary(child, references, indent, showErrors);
}
}

private void logResult( MavenSession session )
{
logger.info( chars( '-', LINE_LENGTH ) );
Expand Down

0 comments on commit 361071d

Please sign in to comment.