Skip to content
Permalink
Browse files

JENKINS-45265 : Handle invoker plugin results

  • Loading branch information...
bguerin committed Jul 3, 2017
1 parent ca98a6a commit 12f30da719a607e125bfee1f9bac6b0b4ed4a32e
@@ -47,6 +47,7 @@ Maven build executions inside the `withMaven(){...}` wil be detected and Jenkins
* Archive and fingerprint generated Maven artifacts and Maven attached artifacts
* Publish JUnit / Surefire reports (if the [Jenkins JUnit Plugin](http://wiki.jenkins-ci.org/display/JENKINS/JUnit+Plugin) is installed)
* Publish Findbugs reports (if the [Jenkins FindBugs Plugin](http://wiki.jenkins-ci.org/display/JENKINS/FindBugs+Plugin) is installed)
* Publish Invoker reports (if the [Jenkins Maven Invoker Plugin](https://wiki.jenkins.io/display/JENKINS/Maven+Invoker+Plugin) is installed)
(i) The detection of Maven builds require to use Maven 3.2+.
@@ -56,6 +57,7 @@ Maven build executions inside the `withMaven(){...}` wil be detected and Jenkins
|Generated Artifact|Archiving and the fingerprinting of the artifacts and attached artifacts generated by the Maven build (jar, sources jar, javadocs jar...)| | `.skip-archive-generated-artifacts`|
| Generated JUnit and Surefire reports | Publishing of the JUnit / Surefire reports generated by the Maven build |[JUnit Plugin](http://wiki.jenkins-ci.org/display/JENKINS/JUnit+Plugin) | `.skip-publish-junit-results` |
| Generated Findbugs reports | Publishing of the Findbugs reports generated by the Maven build | [FindBugs Plugin](https://wiki.jenkins-ci.org/display/JENKINS/FindBugs+Plugin) | `.skip-publish-findbugs-results` |
| Generated Invoker reports | Publishing of the Invoker reports generated by the Maven build | [Maven Invoker Plugin](https://wiki.jenkins.io/display/JENKINS/Maven+Invoker+Plugin) | `.skip-publish-invoker-results` |
(1) Jenkins Plugin to publish the reports on the Jenkins build page
@@ -119,6 +119,13 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>1.3</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>findbugs</artifactId>
@@ -0,0 +1,206 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.pipeline.maven.publishers;

import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Run;
import hudson.model.StreamBuildListener;
import hudson.model.TaskListener;
import hudson.tasks.junit.JUnitResultArchiver;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.pipeline.maven.MavenSpyLogProcessor;
import org.jenkinsci.plugins.maveninvoker.MavenInvokerRecorder;
import org.jenkinsci.plugins.pipeline.maven.MavenPublisher;
import org.jenkinsci.plugins.pipeline.maven.util.XmlUtils;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.kohsuke.stapler.DataBoundConstructor;
import org.w3c.dom.Element;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Nonnull;

public class InvokerRunsPublisher extends MavenPublisher {
private static final Logger LOGGER = Logger.getLogger(InvokerRunsPublisher.class.getName());
private static final String GROUP_ID = "org.apache.maven.plugins";
private static final String ARTIFACT_ID = "maven-invoker-plugin";
private static final String GOAL = "run";

private static final long serialVersionUID = 1L;

@DataBoundConstructor
public InvokerRunsPublisher() {

}

/*
<ExecutionEvent type="MojoSucceeded" class="org.apache.maven.lifecycle.internal.DefaultExecutionEvent" _time="2017-06-25 20:47:25.741">
<project baseDir="/home/jenkins/workspace/aProject" file="/home/jenkins/workspace/aProject/pom.xml" groupId="org.myorg" name="My Project" artifactId="my-maven-plugin" version="1.0.0-SNAPSHOT">
<build sourceDirectory="/home/jenkins/workspace/aProject/src/main/java" directory="/home/jenkins/workspace/aProject/target"/>
</project>
<plugin executionId="integration-test" goal="run" groupId="org.apache.maven.plugins" artifactId="maven-invoker-plugin" version="2.0.0">
<projectsDirectory>${invoker.projectsDirectory}</projectsDirectory>
<cloneProjectsTo>/var/lib/jenkins/workspace/ncjira-maven-plugin-pipeline/target/it</cloneProjectsTo>
<reportsDirectory>${invoker.reportsDirectory}</reportsDirectory>
</plugin>
</ExecutionEvent>
*/
@Override
public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsElt) throws IOException, InterruptedException {

TaskListener listener = context.get(TaskListener.class);
if (listener == null) {
LOGGER.warning("TaskListener is NULL, default to stderr");
listener = new StreamBuildListener((OutputStream) System.err);
}

try {
Class.forName("org.jenkinsci.plugins.maveninvoker.MavenInvokerRecorder");
} catch (ClassNotFoundException e) {
listener.getLogger().print("[withMaven] Jenkins ");
listener.hyperlink("https://wiki.jenkins.io/display/JENKINS/Maven+Invoker+Plugin", "Maven Invoker Plugin");
listener.getLogger().print(" not found, don't display " + GROUP_ID + ":" + ARTIFACT_ID + ":" + GOAL + " results in pipeline screen.");
return;
}

List<Element> sureFireTestEvents = XmlUtils.getExecutionEvents(mavenSpyLogsElt, GROUP_ID, ARTIFACT_ID, GOAL);

executeReporter(context, listener, sureFireTestEvents);
}

private void executeReporter(StepContext context, TaskListener listener, List<Element> testEvents) throws IOException, InterruptedException {
FilePath workspace = context.get(FilePath.class);
final String fileSeparatorOnAgent = XmlUtils.getFileSeparatorOnRemote(workspace);

Run run = context.get(Run.class);
Launcher launcher = context.get(Launcher.class);

if (testEvents.isEmpty()) {
if (LOGGER.isLoggable(Level.FINE)) {
listener.getLogger().println("[withMaven] No " + GROUP_ID + ":" + ARTIFACT_ID + ":" + GOAL + " execution found");
}
return;
}

for (Element testEvent : testEvents) {
String surefireEventType = testEvent.getAttribute("type");
if (!surefireEventType.equals("MojoSucceeded") && !surefireEventType.equals("MojoFailed")) {
continue;
}
Element projectElt = XmlUtils.getUniqueChildElement(testEvent, "project");
Element pluginElt = XmlUtils.getUniqueChildElement(testEvent, "plugin");
Element reportsDirectoryElt = XmlUtils.getUniqueChildElementOrNull(pluginElt, "reportsDirectory");
Element cloneProjectsToElt = XmlUtils.getUniqueChildElementOrNull(pluginElt, "cloneProjectsTo");
Element projectsDirectoryElt = XmlUtils.getUniqueChildElementOrNull(pluginElt, "projectsDirectory");
MavenSpyLogProcessor.MavenArtifact mavenArtifact = XmlUtils.newMavenArtifact(projectElt);
MavenSpyLogProcessor.PluginInvocation pluginInvocation = XmlUtils.newPluginInvocation(pluginElt);

String reportsDirectory = expandAndRelativize(reportsDirectoryElt, "reportsDirectory", testEvent, projectElt, workspace,listener);
String projectsDirectory = expandAndRelativize(projectsDirectoryElt, "projectsDirectory", testEvent, projectElt, workspace,listener);
String cloneProjectsTo = expandAndRelativize(cloneProjectsToElt, "cloneProjectsTo", testEvent, projectElt, workspace,listener);
if (reportsDirectory == null || projectsDirectory == null ) continue;

String testResults = reportsDirectory + fileSeparatorOnAgent + "*.xml";
listener.getLogger().println("[withMaven] Archive invoker results for Maven artifact " + mavenArtifact.toString() + " generated by " +
pluginInvocation + ": " + testResults);
MavenInvokerRecorder archiver = new MavenInvokerRecorder("**/" + reportsDirectory + "/BUILD*.xml", "**/" + (cloneProjectsTo != null ? cloneProjectsTo : projectsDirectory));

try {
archiver.perform(run, workspace, launcher, listener);
} catch (Exception e) {
listener.error("[withMaven] Silently ignore exception archiving Invoker runs for Maven artifact " + mavenArtifact.toString() + " generated by " + pluginInvocation + ": " + e);
LOGGER.log(Level.WARNING, "Exception processing " + XmlUtils.toString(testEvent), e);
}

}
}

private String expandAndRelativize(Element element, String name, Element testEvent, Element projectElt, FilePath workspace, TaskListener listener) {
if (element == null) {
listener.getLogger().println("[withMaven] No <" + name + "> element found for <plugin> in " + XmlUtils.toString(testEvent));
return null;
}

String result = element.getTextContent().trim();

if (result.contains("${invoker.projectsDirectory}")) {
result = result.replace("${invoker.projectsDirectory}", "${basedir}/src/it");
} else if (result.contains("${invoker.reportsDirectory}")) {
result = result.replace("${invoker.reportsDirectory}", "${project.build.directory}/invoker-reports");
}

if (result.contains("${project.build.directory}")) {
String projectBuildDirectory = XmlUtils.getProjectBuildDirectory(projectElt);
if (projectBuildDirectory == null || projectBuildDirectory.isEmpty()) {
listener.getLogger().println("[withMaven] '${project.build.directory}' found for <project> in " + XmlUtils.toString(testEvent));
return null;
}

result = result.replace("${project.build.directory}", projectBuildDirectory);

} else if (result.contains("${basedir}")) {
String baseDir = projectElt.getAttribute("baseDir");
if (baseDir.isEmpty()) {
listener.getLogger().println("[withMaven] '${basedir}' found for <project> in " + XmlUtils.toString(testEvent));
return null;
}

result = result.replace("${basedir}", baseDir);
}

return XmlUtils.getPathInWorkspace(result, workspace);
}

/**
* Don't use the symbol "junit", it would collide with hudson.tasks.junit.JUnitResultArchiver
*/
@Symbol("invokerPublisher")
@Extension
public static class DescriptorImpl extends MavenPublisher.DescriptorImpl {
@Nonnull
@Override
public String getDisplayName() {
return "Invoker Publisher";
}

@Override
public int ordinal() {
return 10;
}


@Nonnull
@Override
public String getSkipFileName() {
return ".skip-publish-invoker-runs";
}
}
}
@@ -0,0 +1,31 @@
<!--
The MIT License
Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Seiji Sogabe, Tom Huybrechts
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">

<st:include page="maven-publisher" class="${descriptor.clazz}"/>

</j:jelly>
@@ -0,0 +1,3 @@
<div>
Skip the publishing of invoker reports.
</div>
@@ -0,0 +1,6 @@
<div>
Publish Invoker reports generated by "<code>org.apache.maven.plugins:maven-invoker-plugin:run</code>".<br/>

Invoker results are by default published if the
<a href="https://wiki.jenkins.io/display/JENKINS/Maven+Invoker+Plugin">Jenkins Maven Invoker Plugin</a> is installed.
</div>
@@ -32,6 +32,7 @@
import org.jenkinsci.plugins.pipeline.maven.eventspy.handler.DependencyResolutionRequestHandler;
import org.jenkinsci.plugins.pipeline.maven.eventspy.handler.DependencyResolutionResultHandler;
import org.jenkinsci.plugins.pipeline.maven.eventspy.handler.FailsafeTestExecutionHandler;
import org.jenkinsci.plugins.pipeline.maven.eventspy.handler.InvokerRunExecutionHandler;
import org.jenkinsci.plugins.pipeline.maven.eventspy.handler.JarJarExecutionHandler;
import org.jenkinsci.plugins.pipeline.maven.eventspy.handler.MavenEventHandler;
import org.jenkinsci.plugins.pipeline.maven.eventspy.handler.MavenExecutionRequestHandler;
@@ -115,6 +116,7 @@ public void init(EventSpy.Context context) throws Exception {
handlers.add(new FailsafeTestExecutionHandler(reporter));
handlers.add(new SurefireTestExecutionHandler(reporter));
handlers.add(new JarJarExecutionHandler(reporter));
handlers.add(new InvokerRunExecutionHandler(reporter));
handlers.add(new DefaultSettingsBuildingRequestHandler(reporter));
handlers.add(new MavenExecutionRequestHandler(reporter));
handlers.add(new DependencyResolutionRequestHandler(reporter));
@@ -0,0 +1,58 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.pipeline.maven.eventspy.handler;

import org.apache.maven.execution.ExecutionEvent;
import org.jenkinsci.plugins.pipeline.maven.eventspy.reporter.MavenEventReporter;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class InvokerRunExecutionHandler extends AbstractExecutionHandler {

public InvokerRunExecutionHandler(MavenEventReporter reporter) {
super(reporter);
}

@Nullable
protected ExecutionEvent.Type getSupportedType() {
return ExecutionEvent.Type.MojoSucceeded;
}

@Nullable
@Override
protected String getSupportedPluginGoal() {
return "org.apache.maven.plugins:maven-invoker-plugin:run";
}

@Nonnull
@Override
protected List<String> getConfigurationParametersToReport(ExecutionEvent executionEvent) {
return Arrays.asList("projectsDirectory", "cloneProjectsTo", "reportsDirectory");
}
}

0 comments on commit 12f30da

Please sign in to comment.
You can’t perform that action at this time.