0

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!

4

1 回答 1

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;
}
于 2014-08-29T10:36:07.540 回答