从groovy修改Jenkins工作中的Sonar jdk

时间:2014-05-09 09:40:21

标签: groovy jenkins sonarqube

如何从Sonar的groovy脚本中设置jdk版本,以便在Jenkins作业配置中使用。我要求声纳jdk与用于通过maven构建的作业实际jdk不同。我已经设法进入舞台,我可以检索Sonar使用的作业jdk版本,但无法找到设置它的方法。有什么建议吗?

import hudson.util.*;
import hudson.tasks.Publisher;

def h = Hudson.getInstance();
def projects = h.getProjects();

for (project in projects) {
    def DescribableList<Publisher,Descriptor<Publisher>> publishers = project.getPublishersList();
    for (publisher in publishers) {
      def desc = publisher.getDescriptor();
      if (desc.getId().equals("hudson.plugins.sonar.SonarPublisher")) {
        println(publisher.getJDK());        
        //((hudson.plugins.sonar.SonarPublisher) publisher).setJDK(h.getJDK("Sun JDK 1.7.45"));
      }
    }
}

2 个答案:

答案 0 :(得分:0)

由于SonarPublisher没有jdk的setter,你需要在job的发布者中用新的实例替换实际的实例:

def jobs = Hudson.instance.getItems(AbstractProject.class)

jobs.each {
  sonar = it.publishersList.get(hudson.plugins.sonar.SonarPublisher.class)
  if(sonar){
     newSonar = new hudson.plugins.sonar.SonarPublisher(
        sonar.installationName,
        sonar.branch,
        sonar.language,
        sonar.triggers,
        sonar.jobAdditionalProperties,
        sonar.mavenOpts,
        sonar.mavenInstallationName,
        sonar.rootPom,
        "Oracle 1.8.0", //set your JDK name here
        sonar.settings,
        sonar.globalSettings,
        sonar.usePrivateRepository)

    it.publishersList.replace(newSonar)
  }
}

答案 1 :(得分:0)

在SonarPublisher的最新版本中(我正在使用SonarQube Scanner 2.8),您可以直接设置JDK,这对我有用:

def jobs = Hudson.instance.getItems(AbstractProject.class)

jobs.each {
    sonar = it.publishersList.get(hudson.plugins.sonar.SonarPublisher.class)
    if(sonar) {
        sonar.setJdk("jdk18") // set your JDK name here
        it.publishersList.replace(sonar)
    }
}