Jenkins - MavenBuild.getMavenArtifacts() Returns N

2019-04-17 18:12发布

问题:

Since Jenkins 1.460, calling getMavenArtifacts() on an instance of MavenBuild is returning null, whereas previously that would work fine.

Have there been breaking changes in the Jenkins API, or is this a Jenkins bug?

The code I'm executing is a post-build System Groovy script, that exposes the Maven version of a build as an environment variable for subsequent steps in the Jenkins build process to use:

import hudson.model.*;
import hudson.util.*;

def thr = Thread.currentThread();
def currentBuild = thr?.executable;
def mavenVer = currentBuild.getMavenArtifacts().getModuleRecords()[0].mainArtifact.version;
def newParamAction = new hudson.model.ParametersAction(new hudson.model.StringParameterValue("MAVEN_VERSION", mavenVer));
currentBuild.addAction(newParamAction);

回答1:

I found a workaround, although I don't know why an unannounced breaking change was made to the API - hopefully it's a bug that will get fixed.

Substitute:

def mavenVer = currentBuild.getMavenArtifacts().getModuleRecords()[0].mainArtifact.version;

for

def mavenVer = currentBuild.getParent().getModules().toArray()[0].getVersion();