Skip to content

Commit

Permalink
[FIXED JENKINS-15025] Maven-Tycho 0.15.0 build - no additional info in
Browse files Browse the repository at this point in the history
console log about compilation failure 

Move Maven3ResultProcessor to external class
CQI - change failNever variable name
CQI - change return type of process method to boolean instead of int
(default for CLI)
Bug fix on compare project
  • Loading branch information
Nikolas Falco committed Oct 8, 2013
1 parent 8c008c5 commit 0cb0175
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 153 deletions.
159 changes: 6 additions & 153 deletions src/main/java/hudson/maven/Maven3Builder.java
Expand Up @@ -42,7 +42,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -51,20 +50,13 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;

import org.apache.maven.InternalErrorException;
import org.apache.maven.cli.CLIManager;
import org.apache.maven.eventspy.EventSpy;
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.ExecutionEvent;
import org.apache.maven.execution.ExecutionListener;
import org.apache.maven.lifecycle.LifecycleExecutionException;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.StringUtils;
import org.jvnet.hudson.maven3.listeners.HudsonMavenExecutionResult;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -135,11 +127,11 @@ public Result call() throws IOException {

Method launchMethod = maven3MainClass.getMethod( "launch", String[].class );

Integer res = (Integer) launchMethod.invoke(null, new Object[] {goals.toArray(new String[goals.size()])} );
/*Integer res = (Integer) */launchMethod.invoke(null, new Object[] {goals.toArray(new String[goals.size()])} );

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

int r = res.intValue();
// int r = res.intValue();

// now check the completion status of async ops
long startTime = System.nanoTime();
Expand All @@ -166,10 +158,11 @@ public Result call() throws IOException {
//mavenExecutionResult = Maven3Launcher.getMavenExecutionResult();

// manage of Maven error threaded as in MavenCli, delegated by Maven3Launcher.launch
MavenResultProcessor summary = new MavenResultProcessor(mavenExecutionListener.logger);
r = summary.processResult();
Maven3ResultProcessor summary = new Maven3ResultProcessor(mavenExecutionListener.logger);
summary.processCLIArguments(goals);
boolean isFail = summary.processFailure(mavenExecutionResult);

if(r==0) {
if(isFail) {
if(mavenExecutionListener.hasTestFailures()){
return Result.UNSTABLE;
}
Expand Down Expand Up @@ -200,146 +193,6 @@ public Result call() throws IOException {
}
}

private class MavenResultProcessor {

private org.slf4j.Logger slf4jLogger;
boolean showErrors = false; // default from DefaultMavenExecutionRequest.showErrors
boolean failFast = false; // default from DefaultMavenExecutionRequest.reactorFailureBehavior

public MavenResultProcessor( org.slf4j.Logger slf4jLogger )
{
this.slf4jLogger = slf4jLogger;

/*
* Following values comes from MavenExecutionRequest that is not
* reachable. Request can easily get into an
* ExecutionListener.sessionEnded call from MavenSession argument,
* in this scenario all implementations must be patched to handle
* errors.
*/
// need to calculate some values as in the maven request.
showErrors = goals.contains(CLIManager.DEBUG) || goals.contains(CLIManager.ERRORS);
failFast = goals.contains(CLIManager.FAIL_NEVER);
}

// partial copy of MavenCli.execute()
private int processResult() {
if ( !mavenExecutionResult.getThrowables().isEmpty() )
{
ExceptionHandler handler = new DefaultExceptionHandler();

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

MavenProject project = null;

for ( Throwable exception : mavenExecutionResult.getThrowables() )
{
ExceptionSummary summary = handler.handleException( exception );

logSummary( summary, references, "", showErrors );

if ( project == null && exception instanceof LifecycleExecutionException )
{
project = ( (LifecycleExecutionException) exception ).getProject();
}
}

slf4jLogger.error( "" );

if ( !showErrors )
{
slf4jLogger.error( "To see the full stack trace of the errors, re-run Maven with the -e switch." );
}
if ( !slf4jLogger.isDebugEnabled() )
{
slf4jLogger.error( "Re-run Maven using the -X switch to enable full debug logging." );
}

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

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

if ( project != null && !project.equals( mavenExecutionResult.getMavenProjectInfos().get( 0 ) ) )
{
slf4jLogger.error( "" );
slf4jLogger.error( "After correcting the problems, you can resume the build with the command" );
slf4jLogger.error( " mvn <goals> -rf :" + project.getArtifactId() );
}

if ( failFast )
{
slf4jLogger.info( "Build failures were ignored." );

return 0;
}
else
{
return 1;
}
}
else
{
return 0;
}
}

// copy of MavenCli.logSummary()
private void logSummary(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 ) ) ) {
slf4jLogger.error( line, summary.getException() );
} else {
slf4jLogger.error(line);
}
}

indent += " ";

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

private static final class JenkinsEventSpy extends MavenExecutionListener implements EventSpy,Serializable{
private static final long serialVersionUID = 4942789836756366117L;

Expand Down
169 changes: 169 additions & 0 deletions src/main/java/hudson/maven/Maven3ResultProcessor.java
@@ -0,0 +1,169 @@
package hudson.maven;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.maven.InternalErrorException;
import org.apache.maven.cli.CLIManager;
import org.apache.maven.exception.DefaultExceptionHandler;
import org.apache.maven.exception.ExceptionHandler;
import org.apache.maven.exception.ExceptionSummary;
import org.apache.maven.lifecycle.LifecycleExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.StringUtils;
import org.jvnet.hudson.maven3.listeners.HudsonMavenExecutionResult;
import org.jvnet.hudson.maven3.listeners.MavenProjectInfo;

public class Maven3ResultProcessor {

private org.slf4j.Logger slf4jLogger;
boolean showErrors = false; // default from DefaultMavenExecutionRequest.showErrors
boolean failNever = false; // default from DefaultMavenExecutionRequest.reactorFailureBehavior

public Maven3ResultProcessor( org.slf4j.Logger slf4jLogger )
{
this.slf4jLogger = slf4jLogger;
}

// partial copy paste of MavenCli.execute()
public boolean processFailure(HudsonMavenExecutionResult result) {
if ( !result.getThrowables().isEmpty() )
{
ExceptionHandler handler = new DefaultExceptionHandler();

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

MavenProject project = null;

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

logSummary( summary, references, "", showErrors );

if ( project == null && exception instanceof LifecycleExecutionException )
{
project = ( (LifecycleExecutionException) exception ).getProject();
}
}

slf4jLogger.error( "" );

if ( !showErrors )
{
slf4jLogger.error( "To see the full stack trace of the errors, re-run Maven with the -e switch." );
}
if ( !slf4jLogger.isDebugEnabled() )
{
slf4jLogger.error( "Re-run Maven using the -X switch to enable full debug logging." );
}

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

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

if ( project != null && !project.equals( convert( result.getMavenProjectInfos().get( 0 )) ) )
{
slf4jLogger.error( "" );
slf4jLogger.error( "After correcting the problems, you can resume the build with the command" );
slf4jLogger.error( " mvn <goals> -rf :" + project.getArtifactId() );
}

if ( failNever )
{
slf4jLogger.info( "Build failures were ignored." );

return false;
}
else
{
return true;
}
}
else
{
return false;
}
}

// copy paste of MavenCli.logSummary()
private void logSummary(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 ) ) ) {
slf4jLogger.error( line, summary.getException() );
} else {
slf4jLogger.error(line);
}
}

indent += " ";

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

private MavenProject convert(MavenProjectInfo prjInfo)
{
MavenProject prj = new MavenProject();
prj.setArtifactId(prjInfo.getArtifactId());
prj.setGroupId(prjInfo.getGroupId());
prj.setVersion(prjInfo.getVersion());

return prj;
}

/*
* Following values comes from MavenExecutionRequest that is not
* reachable. Request can easily get into an
* ExecutionListener.sessionEnded call from MavenSession argument,
* in this scenario all implementations must be patched to handle
* errors.
*/
public void processCLIArguments(List<String> goals)
{
// need to calculate some values as in the maven request.
showErrors = goals.contains("-" + CLIManager.DEBUG) || goals.contains("-" + CLIManager.ERRORS);
failNever = goals.contains("-" + CLIManager.FAIL_NEVER);
}
}

0 comments on commit 0cb0175

Please sign in to comment.