How can I retrieve a list of all Jenkins build steps available to a job?

只谈情不闲聊 提交于 2019-12-23 04:41:59

问题


I'm currently looking for a way to get a list of all available build steps for a Jenkins job.

Kinda like:

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

Thanks in advance!


回答1:


Well, apparently Builder.all() does exist.

This solved my problem:

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;
}


来源:https://stackoverflow.com/questions/25563603/how-can-i-retrieve-a-list-of-all-jenkins-build-steps-available-to-a-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!