1 package hudson.model; 2 3 import hudson.tasks.BuildStep; 4 import hudson.tasks.BuildWrapper; 5 import hudson.tasks.BuildWrapper.Environment; 6 import hudson.tasks.Builder; 7 import hudson.tasks.Publisher; 8 import hudson.tasks.test.AbstractTestResultAction; 9 import hudson.triggers.SCMTrigger; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.util.ArrayList ; 14 import java.util.List ; 15 import java.util.Map ; 16 17 22 public final class Build extends AbstractBuild<Project,Build> { 23 24 27 Build(Project project) throws IOException { 28 super(project); 29 } 30 31 34 Build(Project project, File buildDir) throws IOException { 35 super(project,buildDir); 36 } 37 38 @Override 39 public AbstractTestResultAction getTestResultAction() { 40 return getAction(AbstractTestResultAction.class); 41 } 42 43 47 private transient List <Environment> buildEnvironments; 48 49 @Override 50 protected void onStartBuilding() { 51 SCMTrigger t = (SCMTrigger)project.getTriggers().get(SCMTrigger.DESCRIPTOR); 52 if(t==null) { 53 super.onStartBuilding(); 54 } else { 55 synchronized(t) { 56 try { 57 t.abort(); 58 } catch (InterruptedException e) { 59 Thread.currentThread().interrupt(); 61 } 62 super.onStartBuilding(); 63 } 64 } 65 } 66 67 @Override 68 protected void onEndBuilding() { 69 SCMTrigger t = (SCMTrigger)project.getTriggers().get(SCMTrigger.DESCRIPTOR); 70 if(t==null) { 71 super.onEndBuilding(); 72 } else { 73 synchronized(t) { 74 super.onEndBuilding(); 75 t.startPolling(); 76 } 77 } 78 } 79 80 @Override 81 public Map <String ,String > getEnvVars() { 82 Map <String ,String > env = super.getEnvVars(); 83 84 if(buildEnvironments!=null) { 85 for (Environment e : buildEnvironments) 86 e.buildEnvVars(env); 87 } 88 89 return env; 90 } 91 92 @Override 98 public void run() { 99 run(new RunnerImpl()); 100 } 101 102 private class RunnerImpl extends AbstractRunner { 103 protected Result doRun(BuildListener listener) throws Exception { 104 if(!preBuild(listener,project.getBuilders())) 105 return Result.FAILURE; 106 if(!preBuild(listener,project.getPublishers())) 107 return Result.FAILURE; 108 109 buildEnvironments = new ArrayList <Environment>(); 110 try { 111 for( BuildWrapper w : project.getBuildWrappers().values() ) { 112 Environment e = w.setUp(Build.this, launcher, listener); 113 if(e==null) 114 return Result.FAILURE; 115 buildEnvironments.add(e); 116 } 117 118 119 if(!build(listener,project.getBuilders())) 120 return Result.FAILURE; 121 } finally { 122 for( int i=buildEnvironments.size()-1; i>=0; i-- ) 124 buildEnvironments.get(i).tearDown(Build.this,listener); 125 buildEnvironments = null; 126 } 127 128 return null; 129 } 130 131 public void post(BuildListener listener) { 132 try { 134 for( Publisher bs : project.getPublishers().values() ) 135 bs.perform(Build.this, launcher, listener); 136 } catch (InterruptedException e) { 137 e.printStackTrace(listener.fatalError("aborted")); 138 setResult(Result.FAILURE); 139 } catch (IOException e) { 140 e.printStackTrace(listener.fatalError("failed")); 141 setResult(Result.FAILURE); 142 } 143 } 144 145 private boolean build(BuildListener listener, Map <?, Builder> steps) throws IOException , InterruptedException { 146 for( Builder bs : steps.values() ) 147 if(!bs.perform(Build.this, launcher, listener)) 148 return false; 149 return true; 150 } 151 152 private boolean preBuild(BuildListener listener,Map <?,? extends BuildStep> steps) { 153 for( BuildStep bs : steps.values() ) 154 if(!bs.prebuild(Build.this,listener)) 155 return false; 156 return true; 157 } 158 } 159 } 160 | Popular Tags |