我目前正在寻找一种方法来获取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();


提前致谢!

最佳答案

好吧,显然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;
}

10-08 12:53