1 package hudson.tasks; 2 3 import hudson.CopyOnWrite; 4 import hudson.FilePath.FileCallable; 5 import hudson.Functions; 6 import hudson.Launcher; 7 import hudson.Util; 8 import hudson.model.Build; 9 import hudson.model.BuildListener; 10 import hudson.model.Descriptor; 11 import hudson.model.Project; 12 import hudson.remoting.VirtualChannel; 13 import hudson.util.ArgumentListBuilder; 14 import hudson.util.FormFieldValidator; 15 import org.kohsuke.stapler.StaplerRequest; 16 import org.kohsuke.stapler.StaplerResponse; 17 18 import javax.servlet.ServletException ; 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.Map ; 22 import java.util.StringTokenizer ; 23 24 29 public class Maven extends Builder { 30 34 private final String targets; 35 36 39 private final String mavenName; 40 41 public Maven(String targets,String mavenName) { 42 this.targets = targets; 43 this.mavenName = mavenName; 44 } 45 46 public String getTargets() { 47 return targets; 48 } 49 50 54 public MavenInstallation getMaven() { 55 for( MavenInstallation i : DESCRIPTOR.getInstallations() ) { 56 if(mavenName !=null && i.getName().equals(mavenName)) 57 return i; 58 } 59 return null; 60 } 61 62 66 private static final class DecideDefaultMavenCommand implements FileCallable<String > { 67 private final String arguments; 69 70 public DecideDefaultMavenCommand(String arguments) { 71 this.arguments = arguments; 72 } 73 74 public String invoke(File ws, VirtualChannel channel) throws IOException { 75 String seed=null; 76 77 StringTokenizer tokens = new StringTokenizer (arguments); 79 while(tokens.hasMoreTokens()) { 80 String t = tokens.nextToken(); 81 if(t.equals("-f") && tokens.hasMoreTokens()) { 82 File file = new File (ws,tokens.nextToken()); 83 if(!file.exists()) 84 continue; if(file.isDirectory()) 86 seed = "maven"; 88 else 89 seed = "mvn"; 91 break; 92 } 93 } 94 95 if(seed==null) { 96 if(new File (ws,"pom.xml").exists()) 97 seed = "mvn"; 98 else 99 seed = "maven"; 101 } 102 103 if(Functions.isWindows()) 104 seed += ".bat"; 105 return seed; 106 } 107 } 108 109 public boolean perform(Build build, Launcher launcher, BuildListener listener) throws IOException , InterruptedException { 110 Project proj = build.getProject(); 111 112 String normalizedTarget = targets.replaceAll("[\t\r\n]+"," "); 113 114 ArgumentListBuilder args = new ArgumentListBuilder(); 115 MavenInstallation ai = getMaven(); 116 if(ai==null) { 117 String execName = proj.getWorkspace().act(new DecideDefaultMavenCommand(normalizedTarget)); 118 args.add(execName).addTokenized(normalizedTarget); 119 } else { 120 File exec = ai.getExecutable(); 121 if(exec==null) { 122 listener.fatalError("Couldn't find any executable in "+ai.getMavenHome()); 123 return false; 124 } 125 if(!exec.exists()) { 126 listener.fatalError(exec+" doesn't exist"); 127 return false; 128 } 129 args.add(exec.getPath()).addTokenized(normalizedTarget); 130 } 131 132 Map <String ,String > env = build.getEnvVars(); 133 if(ai!=null) 134 env.put("MAVEN_HOME",ai.getMavenHome()); 135 env.put("MAVEN_TERMINATE_CMD","on"); 138 139 try { 140 int r = launcher.launch(args.toCommandArray(),env,listener.getLogger(),proj.getModuleRoot()).join(); 141 return r==0; 142 } catch (IOException e) { 143 Util.displayIOException(e,listener); 144 e.printStackTrace( listener.fatalError("command execution failed") ); 145 return false; 146 } 147 } 148 149 public Descriptor<Builder> getDescriptor() { 150 return DESCRIPTOR; 151 } 152 153 public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl(); 154 155 public static final class DescriptorImpl extends Descriptor<Builder> { 156 @CopyOnWrite 157 private volatile MavenInstallation[] installations = new MavenInstallation[0]; 158 159 private DescriptorImpl() { 160 super(Maven.class); 161 load(); 162 } 163 164 165 protected void convert(Map <String , Object > oldPropertyBag) { 166 if(oldPropertyBag.containsKey("installations")) 167 installations = (MavenInstallation[]) oldPropertyBag.get("installations"); 168 } 169 170 public String getHelpFile() { 171 return "/help/project-config/maven.html"; 172 } 173 174 public String getDisplayName() { 175 return "Invoke top-level Maven targets"; 176 } 177 178 public MavenInstallation[] getInstallations() { 179 return installations; 180 } 181 182 public boolean configure(StaplerRequest req) { 183 boolean r = true; 184 185 int i; 186 String [] names = req.getParameterValues("maven_name"); 187 String [] homes = req.getParameterValues("maven_home"); 188 int len; 189 if(names!=null && homes!=null) 190 len = Math.min(names.length,homes.length); 191 else 192 len = 0; 193 MavenInstallation[] insts = new MavenInstallation[len]; 194 195 for( i=0; i<len; i++ ) { 196 if(Util.nullify(names[i])==null) continue; 197 if(Util.nullify(homes[i])==null) continue; 198 insts[i] = new MavenInstallation(names[i],homes[i]); 199 } 200 201 this.installations = insts; 202 203 save(); 204 205 return r; 206 } 207 208 public Builder newInstance(StaplerRequest req) { 209 return new Maven(req.getParameter("maven_targets"),req.getParameter("maven_version")); 210 } 211 212 213 219 public void doCheckMavenHome( StaplerRequest req, StaplerResponse rsp ) throws IOException , ServletException { 220 new FormFieldValidator(req,rsp,true) { 222 public void check() throws IOException , ServletException { 223 File f = getFileParameter("value"); 224 if(!f.isDirectory()) { 225 error(f+" is not a directory"); 226 return; 227 } 228 229 232 ok(); 233 } 234 }.process(); 235 } 236 } 237 238 public static final class MavenInstallation { 239 private final String name; 240 private final String mavenHome; 241 242 public MavenInstallation(String name, String mavenHome) { 243 this.name = name; 244 this.mavenHome = mavenHome; 245 } 246 247 250 public String getMavenHome() { 251 return mavenHome; 252 } 253 254 public File getHomeDir() { 255 return new File (mavenHome); 256 } 257 258 261 public String getName() { 262 return name; 263 } 264 265 public File getExecutable() { 266 File exe = getExeFile("maven"); 267 if(exe.exists()) 268 return exe; 269 exe = getExeFile("mvn"); 270 if(exe.exists()) 271 return exe; 272 return null; 273 } 274 275 private File getExeFile(String execName) { 276 if(File.separatorChar=='\\') 277 execName += ".bat"; 278 return new File (getMavenHome(), "bin/" + execName); 279 } 280 281 284 public boolean getExists() { 285 return getExecutable()!=null; 286 } 287 } 288 } 289 | Popular Tags |