1 package hudson.tasks; 2 3 import hudson.CopyOnWrite; 4 import hudson.Launcher; 5 import hudson.Util; 6 import hudson.model.Build; 7 import hudson.model.BuildListener; 8 import hudson.model.Descriptor; 9 import hudson.model.Project; 10 import hudson.util.FormFieldValidator; 11 import hudson.util.ArgumentListBuilder; 12 import org.kohsuke.stapler.StaplerRequest; 13 import org.kohsuke.stapler.StaplerResponse; 14 15 import javax.servlet.ServletException ; 16 import java.io.File ; 17 import java.io.IOException ; 18 import java.util.Map ; 19 20 23 public class Ant extends Builder { 24 28 private final String targets; 29 30 33 private final String antName; 34 35 public Ant(String targets,String antName) { 36 this.targets = targets; 37 this.antName = antName; 38 } 39 40 public String getTargets() { 41 return targets; 42 } 43 44 48 public AntInstallation getAnt() { 49 for( AntInstallation i : DESCRIPTOR.getInstallations() ) { 50 if(antName!=null && i.getName().equals(antName)) 51 return i; 52 } 53 return null; 54 } 55 56 public boolean perform(Build build, Launcher launcher, BuildListener listener) { 57 Project proj = build.getProject(); 58 59 ArgumentListBuilder args = new ArgumentListBuilder(); 60 61 String execName; 62 if(launcher.isUnix()) 63 execName = "ant"; 64 else 65 execName = "ant.bat"; 66 67 String normalizedTarget = targets.replaceAll("[\t\r\n]+"," "); 68 69 AntInstallation ai = getAnt(); 70 if(ai==null) { 71 args.add(execName); 72 } else { 73 File exec = ai.getExecutable(); 74 if(!ai.getExists()) { 75 listener.fatalError(exec+" doesn't exist"); 76 return false; 77 } 78 args.add(exec.getPath()); 79 } 80 args.addTokenized(normalizedTarget); 81 82 Map <String ,String > env = build.getEnvVars(); 83 if(ai!=null) 84 env.put("ANT_HOME",ai.getAntHome()); 85 86 if(!launcher.isUnix()) { 87 args.prepend("cmd.exe","/C"); 92 args.add("&&","exit","%%ERRORLEVEL%%"); 93 } 94 95 try { 96 int r = launcher.launch(args.toCommandArray(),env,listener.getLogger(),proj.getModuleRoot()).join(); 97 return r==0; 98 } catch (IOException e) { 99 Util.displayIOException(e,listener); 100 e.printStackTrace( listener.fatalError("command execution failed") ); 101 return false; 102 } 103 } 104 105 public Descriptor<Builder> getDescriptor() { 106 return DESCRIPTOR; 107 } 108 109 public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl(); 110 111 public static final class DescriptorImpl extends Descriptor<Builder> { 112 @CopyOnWrite 113 private volatile AntInstallation[] installations = new AntInstallation[0]; 114 115 private DescriptorImpl() { 116 super(Ant.class); 117 load(); 118 } 119 120 protected void convert(Map <String ,Object > oldPropertyBag) { 121 if(oldPropertyBag.containsKey("installations")) 122 installations = (AntInstallation[]) oldPropertyBag.get("installations"); 123 } 124 125 public String getHelpFile() { 126 return "/help/project-config/ant.html"; 127 } 128 129 public String getDisplayName() { 130 return "Invoke top-level Ant targets"; 131 } 132 133 public AntInstallation[] getInstallations() { 134 return installations; 135 } 136 137 public boolean configure(StaplerRequest req) { 138 boolean r = true; 139 140 int i; 141 String [] names = req.getParameterValues("ant_name"); 142 String [] homes = req.getParameterValues("ant_home"); 143 int len; 144 if(names!=null && homes!=null) 145 len = Math.min(names.length,homes.length); 146 else 147 len = 0; 148 AntInstallation[] insts = new AntInstallation[len]; 149 150 for( i=0; i<len; i++ ) { 151 if(names[i].length()==0 || homes[i].length()==0) continue; 152 insts[i] = new AntInstallation(names[i],homes[i]); 153 } 154 155 this.installations = insts; 156 157 save(); 158 159 return r; 160 } 161 162 public Builder newInstance(StaplerRequest req) { 163 return new Ant(req.getParameter("ant_targets"),req.getParameter("ant_version")); 164 } 165 166 172 public void doCheckAntHome( StaplerRequest req, StaplerResponse rsp ) throws IOException , ServletException { 173 new FormFieldValidator(req,rsp,true) { 175 public void check() throws IOException , ServletException { 176 File f = getFileParameter("value"); 177 if(!f.isDirectory()) { 178 error(f+" is not a directory"); 179 return; 180 } 181 182 File antJar = new File (f,"lib/ant.jar"); 183 if(!antJar.exists()) { 184 error(f+" doesn't look like an Ant directory"); 185 return; 186 } 187 188 ok(); 189 } 190 }.process(); 191 } 192 } 193 194 public static final class AntInstallation { 195 private final String name; 196 private final String antHome; 197 198 public AntInstallation(String name, String antHome) { 199 this.name = name; 200 this.antHome = antHome; 201 } 202 203 206 public String getAntHome() { 207 return antHome; 208 } 209 210 213 public String getName() { 214 return name; 215 } 216 217 public File getExecutable() { 218 String execName; 219 if(File.separatorChar=='\\') 220 execName = "ant.bat"; 221 else 222 execName = "ant"; 223 224 return new File (getAntHome(),"bin/"+execName); 225 } 226 227 230 public boolean getExists() { 231 return getExecutable().exists(); 232 } 233 } 234 } 235 | Popular Tags |