Jenkins插件获取multy构建器插件中的构建器列表 - 返回hudson.maven.MavenModuleSet无法强制转换为hudson.model.Project

时间:2014-12-30 13:32:58

标签: maven plugins jenkins

我编写的Jenkins插件包含2个构建器(builderA& builderB) builderA有几个输入字段,将在builderB之前添加到Jenkins作业 builderB没有输入

我在builderB.perform(AbstractBuild构建,Launcher启动器,BuildListener侦听器)中使用以下代码从builderA中提取信息

//loop on all the builders in this project and find  BuilderA
//from there we taking the CI/Release
String component;
String version;
Project buildProject = (Project) build.getProject();
List<Builder> blist = buildProject.getBuilders();
for (Builder b : blist) {
    if (b instanceof BuilderA) {
        listener.getLogger().println("BuilderA was found!!);
        BuilderA ba = (BuilderA) b;
        component = ba.getComponent();
        version = ba.getVersion();
        break;
    }
}

如果我创建 Freestyle项目,这项工作很棒。

如果我创建 Maven项目行: Project buildProject =(Project)build.getProject(); 抛出“ java.lang.ClassCastException:hudson。 maven.MavenModuleSet无法强制转换为hudson.model.Project

问题:如何在 Maven项目中获取构建器列表。

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。我更改了我的代码以支持2种不同的Jenkins作业类型:

List<Builder> blist = new ArrayList<Builder>();
if (build.getProject() instanceof hudson.maven.MavenModuleSet) {
  listener.getLogger().println("Build type: hudson.maven.MavenModuleSet");
  MavenModuleSet mms = (MavenModuleSet) build.getProject();
  DescribableList<Builder, Descriptor<Builder>> dl = mms.getPrebuilders();
  Collection<Builder> c = dl.toMap().values();
  blist.addAll(c);
}
else if (build.getProject() instanceof hudson.model.Project) {
  listener.getLogger().println("Build type: hudson.model.Project");
  Project<?, ?> buildProject = (Project<?, ?>) build.getProject();
  blist = buildProject.getBuilders();
}

还在pom.xml中添加了对引用的引用:

groupId=org.jenkins-ci.main
artifactId=maven-plugin
version=2.0