问题
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