如何检索作业可用的所有Jenkins构建步骤的列表?

时间:2014-08-29 07:32:05

标签: java jenkins jenkins-plugins custom-build-step

我目前正在寻找一种获取Jenkins工作所有可用构建步骤列表的方法。

有点像:

FreeStyleProject newJob = Jenkins.getInstance().createProject( FreeStyleProject.class, this.jobName );
// TODO: Get the available build steps of the created job, following is pseudocode
newJob.getAvailableBuilders();

提前致谢!

1 个答案:

答案 0 :(得分:1)

嗯,显然Builder.all()确实存在。

这解决了我的问题:

List<BuildStepDescriptor<? extends Builder>> builders = new ArrayList<BuildStepDescriptor<? extends Builder>>();
for ( hudson.model.Descriptor<Builder> descriptor : Builder.all() ) {
    if ( !( descriptor instanceof BuildStepDescriptor ) ) {
        continue;
    }
    BuildStepDescriptor<? extends Builder> buildStepDescriptor = (BuildStepDescriptor) descriptor;
    // newJob is a FreeStyleProject
    if ( buildStepDescriptor.isApplicable( newJob.getClass() ) && hasDbc( buildStepDescriptor.clazz ) ) {
        builders.add( buildStepDescriptor );
    }
}
for ( BuildStepDescriptor<? extends Builder> buildStepDescriptor : builders ) {
    System.out.println( buildStepDescriptor.getDisplayName() );
}

private boolean hasDbc( final Class<?> clazz ) {
    for ( Constructor<?> constructor : clazz.getConstructors() ) {
        if ( constructor.isAnnotationPresent( DataBoundConstructor.class ) ) {
            return true;
        }
    }
    return false;
}