io.sealights.agents.plugin.BuildEndNotifier Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sealights-maven-plugin
Show all versions of sealights-maven-plugin
A maven plugin to run java agents.
package io.sealights.agents.plugin;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.apache.maven.execution.BuildFailure;
import org.apache.maven.execution.BuildSuccess;
import org.apache.maven.execution.BuildSummary;
import org.apache.maven.execution.ExecutionListener;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import io.sealights.onpremise.agents.java.agent.plugins.engine.lifecycle.BuildEndInfo;
import io.sealights.onpremise.agents.java.agent.plugins.engine.lifecycle.BuildEndListener;
public enum BuildEndNotifier implements InvocationHandler {
INSTANCE;
private static final String METHOD_NAME_OF_SESSION_ENDED = "sessionEnded";
private MavenSession session;
private ExecutionListener mavenExecutionListener;
private BuildEndListener listener;
private boolean isInitialized = false;
public synchronized boolean initialize(MavenSession session, BuildEndListener listener) {
if (isInitialized) {
return false;
}
this.session = session;
this.listener = listener;
MavenExecutionRequest request = session.getRequest();
mavenExecutionListener = session.getRequest().getExecutionListener();
Object instance = Proxy.newProxyInstance(
getClass().getClassLoader(),
new Class[]{ExecutionListener.class},
this);
request.setExecutionListener((ExecutionListener) instance);
isInitialized = true;
return true;
}
public Object invoke(Object target, Method method, Object[] args) throws Throwable {
if (isEndOfBuild(method)) {
MavenProject project = session.getResult().getProject();
BuildSummary buildSummary = session.getResult().getBuildSummary(project);
BuildEndInfo buildInfo = new BuildEndInfo();
buildInfo.setBuildDurationMSec(buildSummary.getTime());
buildInfo.setSuccessful(buildSummary instanceof BuildSuccess);
if (buildSummary instanceof BuildSuccess) {
buildInfo.setSuccessful(true);
}
if (buildSummary instanceof BuildFailure) {
BuildFailure failure = (BuildFailure) buildSummary;
buildInfo.setFailureReason(failure.toString());
}
listener.notifyBuildEndInfo(buildInfo);
}
return method.invoke(mavenExecutionListener, args);
}
private boolean isEndOfBuild(Method method) {
return method.getName().equals(METHOD_NAME_OF_SESSION_ENDED);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy