1 package hudson.maven; 2 3 import hudson.FilePath; 4 import hudson.Util; 5 import hudson.tasks.Maven; 6 import hudson.tasks.Maven.MavenInstallation; 7 import hudson.model.AbstractProject; 8 import hudson.model.DependencyGraph; 9 import hudson.model.Executor; 10 import hudson.model.Hudson; 11 import hudson.model.Item; 12 import hudson.model.ItemGroup; 13 import hudson.model.Items; 14 import hudson.model.Job; 15 import hudson.model.Node; 16 import hudson.model.SCMedItem; 17 import hudson.model.TopLevelItem; 18 import hudson.model.TopLevelItemDescriptor; 19 import hudson.util.CopyOnWriteMap; 20 import org.kohsuke.stapler.StaplerRequest; 21 import org.kohsuke.stapler.StaplerResponse; 22 23 import javax.servlet.ServletException ; 24 import java.io.File ; 25 import java.io.FileFilter ; 26 import java.io.IOException ; 27 import java.util.Collection ; 28 import java.util.HashSet ; 29 import java.util.Map ; 30 import java.util.Set ; 31 import java.util.logging.Level ; 32 import java.util.logging.Logger ; 33 34 44 public final class MavenModuleSet extends AbstractProject<MavenModuleSet,MavenModuleSetBuild> implements TopLevelItem, ItemGroup<MavenModule>, SCMedItem { 45 48 transient Map<ModuleName,MavenModule> modules = new CopyOnWriteMap.Tree<ModuleName,MavenModule>(); 49 50 53 private ModuleName rootModule; 54 55 private String rootPOM; 56 57 private String goals; 58 59 62 private String defaultGoals; 63 64 68 private String mavenName; 69 70 public MavenModuleSet(String name) { 71 super(Hudson.getInstance(),name); 72 } 73 74 public String getUrlChildPrefix() { 75 return "."; 77 } 78 79 public Hudson getParent() { 80 return Hudson.getInstance(); 81 } 82 83 public Collection <MavenModule> getItems() { 84 return modules.values(); 85 } 86 87 public Collection <MavenModule> getModules() { 88 return getItems(); 89 } 90 91 public MavenModule getItem(String name) { 92 return modules.get(ModuleName.fromString(name)); 93 } 94 95 public MavenModule getModule(String name) { 96 return getItem(name); 97 } 98 99 public Object getDynamic(String token, StaplerRequest req, StaplerResponse rsp) { 100 if(ModuleName.isValid(token)) 101 return getModule(token); 102 return super.getDynamic(token,req,rsp); 103 } 104 105 public File getRootDirFor(MavenModule child) { 106 return new File (new File (getRootDir(),"modules"),child.getModuleName().toFileSystemName()); 107 } 108 109 public Collection <Job> getAllJobs() { 110 Set <Job> jobs = new HashSet <Job>(getItems()); 111 jobs.add(this); 112 return jobs; 113 } 114 115 @Override 116 protected boolean isBuildBlocked() { 117 if(super.isBuildBlocked()) 118 return true; 119 for (MavenModule m : modules.values()) { 122 if(m.isBuilding()) 123 return true; 124 } 125 return false; 126 } 127 128 131 public FilePath getWorkspace() { 132 Node node = getLastBuiltOn(); 133 if(node==null) node = Hudson.getInstance(); 134 return node.getWorkspaceFor(this); 135 } 136 137 @Override 138 public MavenModuleSetBuild newBuild() throws IOException { 139 MavenModuleSetBuild lastBuild = new MavenModuleSetBuild(this); 140 builds.put(lastBuild); 141 return lastBuild; 142 } 143 144 @Override 145 protected MavenModuleSetBuild loadBuild(File dir) throws IOException { 146 return new MavenModuleSetBuild(this,dir); 147 } 148 149 @Override 150 public boolean isFingerprintConfigured() { 151 return true; 152 } 153 154 public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOException { 155 super.onLoad(parent, name); 156 157 File modulesDir = new File (getRootDir(),"modules"); 158 modulesDir.mkdirs(); 160 File [] subdirs = modulesDir.listFiles(new FileFilter () { 161 public boolean accept(File child) { 162 return child.isDirectory(); 163 } 164 }); 165 modules = new CopyOnWriteMap.Tree<ModuleName,MavenModule>(); 166 for (File subdir : subdirs) { 167 try { 168 MavenModule item = (MavenModule) Items.load(this,subdir); 169 modules.put(item.getModuleName(), item); 170 } catch (IOException e) { 171 e.printStackTrace(); } 173 } 174 } 175 176 186 public synchronized int assignBuildNumber() throws IOException { 187 updateNextBuildNumber(); 189 190 return super.assignBuildNumber(); 191 } 192 193 public synchronized int getNextBuildNumber() { 194 try { 195 updateNextBuildNumber(); 196 } catch (IOException e) { 197 LOGGER.log(Level.SEVERE,"Failed to save the next build number",e); 198 } 199 return nextBuildNumber; 200 } 201 202 private void updateNextBuildNumber() throws IOException { 203 int next = this.nextBuildNumber; 204 for (MavenModule m : modules.values()) 205 next = Math.max(next,m.getNextBuildNumber()); 206 207 if(this.nextBuildNumber!=next) { 208 this.nextBuildNumber=next; 209 this.saveNextBuildNumber(); 210 } 211 212 for (MavenModule m : modules.values()) 213 m.updateNextBuildNumber(next); 214 } 215 216 protected void buildDependencyGraph(DependencyGraph graph) { 217 } 219 220 public MavenModule getRootModule() { 221 return modules.get(rootModule); 222 } 223 224 227 public String getRootPOM() { 228 if(rootPOM==null) return "pom.xml"; 229 return rootPOM; 230 } 231 232 public AbstractProject<?,?> asProject() { 233 return this; 234 } 235 236 239 public String getGoals() { 240 if(goals==null) { 241 if(defaultGoals!=null) return defaultGoals; 242 return "install"; 243 } 244 return goals; 245 } 246 247 251 public MavenInstallation getMaven() { 252 for( MavenInstallation i : DESCRIPTOR.getMavenDescriptor().getInstallations() ) { 253 if(mavenName==null || i.getName().equals(mavenName)) 254 return i; 255 } 256 return null; 257 } 258 259 268 public String getUserConfiguredGoals() { 269 return goals; 270 } 271 272 void reconfigure(PomInfo rootPom) throws IOException { 273 if(this.rootModule!=null && this.rootModule.equals(rootModule)) 274 return; this.rootModule = rootPom.name; 276 this.defaultGoals = rootPom.defaultGoal; 277 save(); 278 } 279 280 286 public synchronized void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOException , ServletException { 287 if(!Hudson.adminCheck(req,rsp)) 288 return; 289 290 rootPOM = Util.fixEmpty(req.getParameter("rootPOM").trim()); 291 if(rootPOM.equals("pom.xml")) rootPOM=null; 293 goals = Util.fixEmpty(req.getParameter("goals").trim()); 294 mavenName = req.getParameter("maven_version"); 295 296 super.doConfigSubmit(req,rsp); 297 298 save(); 299 } 300 301 public TopLevelItemDescriptor getDescriptor() { 302 return DESCRIPTOR; 303 } 304 305 public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl(); 306 307 public static final class DescriptorImpl extends TopLevelItemDescriptor { 308 private DescriptorImpl() { 309 super(MavenModuleSet.class); 310 } 311 312 public String getDisplayName() { 313 return "Build a maven2 project (alpha)"; 314 } 315 316 public MavenModuleSet newInstance(String name) { 317 return new MavenModuleSet(name); 318 } 319 320 public Maven.DescriptorImpl getMavenDescriptor() { 321 return Maven.DESCRIPTOR; 322 } 323 } 324 325 private static final Logger LOGGER = Logger.getLogger(MavenModuleSet.class.getName()); 326 } 327 | Popular Tags |