Improve events reporting for forked lifecycles (#411)

This commit is contained in:
Guillaume Nodet
2021-05-18 16:08:15 +02:00
committed by GitHub
parent 6a7f172412
commit 4cfaf7aaf9
11 changed files with 297 additions and 16 deletions

View File

@@ -20,6 +20,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import org.apache.maven.execution.ExecutionEvent;
import org.apache.maven.execution.MavenSession;
@@ -78,16 +79,26 @@ public class ClientDispatcher extends BuildEventListener {
throw new IllegalStateException("Could not compute the 90th percentile of the projects length from " + projects);
}
public void projectStarted(ExecutionEvent event) {
queue.add(Message.projectStarted(event.getProject().getArtifactId()));
private final Map<String, Boolean> projects = new ConcurrentHashMap<>();
public void projectStarted(String projectId) {
projects.put(projectId, Boolean.TRUE);
queue.add(Message.projectStarted(projectId));
}
public void projectLogMessage(String projectId, String event) {
if (projectId != null) {
Boolean b = projects.get(projectId);
if (b != Boolean.TRUE) {
}
}
queue.add(projectId == null ? Message.log(trimTrailingEols(event)) : Message.log(projectId, trimTrailingEols(event)));
}
public void projectFinished(ExecutionEvent event) {
queue.add(Message.projectStopped(event.getProject().getArtifactId()));
@Override
public void projectFinished(String projectId) {
projects.put(projectId, Boolean.FALSE);
queue.add(Message.projectStopped(projectId));
}
public void mojoStarted(ExecutionEvent event) {

View File

@@ -27,13 +27,13 @@ public abstract class BuildEventListener {
public void sessionStarted(ExecutionEvent event) {
}
public void projectStarted(ExecutionEvent event) {
public void projectStarted(String projectId) {
}
public void projectLogMessage(String projectId, String event) {
}
public void projectFinished(ExecutionEvent event) {
public void projectFinished(String projectId) {
}
public void mojoStarted(ExecutionEvent event) {
@@ -64,11 +64,11 @@ public abstract class BuildEventListener {
public abstract void sessionStarted(ExecutionEvent event);
public abstract void projectStarted(ExecutionEvent event);
public abstract void projectStarted(String projectId);
public abstract void projectLogMessage(String projectId, String event);
public abstract void projectFinished(ExecutionEvent event);
public abstract void projectFinished(String projectId);
public abstract void mojoStarted(ExecutionEvent event);

View File

@@ -50,7 +50,7 @@ public class LoggingExecutionListener implements ExecutionListener {
@Override
public void projectStarted(ExecutionEvent event) {
setMdc(event);
buildEventListener.projectStarted(event);
buildEventListener.projectStarted(event.getProject().getArtifactId());
delegate.projectStarted(event);
}
@@ -58,21 +58,21 @@ public class LoggingExecutionListener implements ExecutionListener {
public void projectSucceeded(ExecutionEvent event) {
setMdc(event);
delegate.projectSucceeded(event);
buildEventListener.projectFinished(event);
buildEventListener.projectFinished(event.getProject().getArtifactId());
}
@Override
public void projectFailed(ExecutionEvent event) {
setMdc(event);
delegate.projectFailed(event);
buildEventListener.projectFinished(event);
buildEventListener.projectFinished(event.getProject().getArtifactId());
}
@Override
public void projectSkipped(ExecutionEvent event) {
setMdc(event);
delegate.projectSkipped(event);
buildEventListener.projectFinished(event);
buildEventListener.projectFinished(event.getProject().getArtifactId());
}
@Override
@@ -104,23 +104,25 @@ public class LoggingExecutionListener implements ExecutionListener {
public void forkStarted(ExecutionEvent event) {
setMdc(event);
delegate.forkStarted(event);
ProjectBuildLogAppender.setForkingProjectId(event.getProject().getArtifactId());
}
@Override
public void forkSucceeded(ExecutionEvent event) {
setMdc(event);
delegate.forkSucceeded(event);
ProjectBuildLogAppender.setForkingProjectId(null);
}
@Override
public void forkFailed(ExecutionEvent event) {
setMdc(event);
delegate.forkFailed(event);
ProjectBuildLogAppender.setForkingProjectId(null);
}
@Override
public void forkedProjectStarted(ExecutionEvent event) {
setMdc(event);
buildEventListener.projectStarted(ProjectBuildLogAppender.getProjectId());
delegate.forkedProjectStarted(event);
}
@@ -128,12 +130,16 @@ public class LoggingExecutionListener implements ExecutionListener {
public void forkedProjectSucceeded(ExecutionEvent event) {
setMdc(event);
delegate.forkedProjectSucceeded(event);
buildEventListener.projectFinished(ProjectBuildLogAppender.getProjectId());
ProjectBuildLogAppender.setProjectId(null);
}
@Override
public void forkedProjectFailed(ExecutionEvent event) {
setMdc(event);
delegate.forkedProjectFailed(event);
buildEventListener.projectFinished(ProjectBuildLogAppender.getProjectId());
ProjectBuildLogAppender.setProjectId(null);
}
private void setMdc(ExecutionEvent event) {

View File

@@ -35,12 +35,21 @@ public class ProjectBuildLogAppender extends AppenderBase<ILoggingEvent> impleme
private static final String KEY_PROJECT_ID = "maven.project.id";
private static final ThreadLocal<String> PROJECT_ID = new InheritableThreadLocal<>();
private static final ThreadLocal<String> FORKING_PROJECT_ID = new InheritableThreadLocal<>();
public static String getProjectId() {
return PROJECT_ID.get();
}
public static void setProjectId(String projectId) {
String forkingProjectId = FORKING_PROJECT_ID.get();
if (forkingProjectId != null) {
if (projectId != null) {
projectId = forkingProjectId + "/" + projectId;
} else {
projectId = forkingProjectId;
}
}
if (projectId != null) {
PROJECT_ID.set(projectId);
MDC.put(KEY_PROJECT_ID, projectId);
@@ -50,6 +59,14 @@ public class ProjectBuildLogAppender extends AppenderBase<ILoggingEvent> impleme
}
}
public static void setForkingProjectId(String forkingProjectId) {
if (forkingProjectId != null) {
FORKING_PROJECT_ID.set(forkingProjectId);
} else {
FORKING_PROJECT_ID.remove();
}
}
public static void updateMdc() {
String id = getProjectId();
if (id != null) {
@@ -92,8 +109,7 @@ public class ProjectBuildLogAppender extends AppenderBase<ILoggingEvent> impleme
@Override
protected void append(ILoggingEvent event) {
Map<String, String> mdc = event.getMDCPropertyMap();
String projectId = mdc != null ? mdc.get(KEY_PROJECT_ID) : null;
String projectId = event.getMDCPropertyMap().get(KEY_PROJECT_ID);
buildEventListener.projectLogMessage(projectId, layout.doLayout(event));
}