1 37 package net.sourceforge.cruisecontrol.sourcecontrols; 38 39 import java.io.File ; 40 import java.io.IOException ; 41 import java.util.ArrayList ; 42 import java.util.Date ; 43 import java.util.Hashtable ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.Map ; 47 48 import net.sourceforge.cruisecontrol.CruiseControlException; 49 import net.sourceforge.cruisecontrol.Modification; 50 import net.sourceforge.cruisecontrol.SourceControl; 51 import net.sourceforge.cruisecontrol.util.ValidationHelper; 52 53 import org.apache.log4j.Logger; 54 import org.jdom.Element; 55 import org.jdom.JDOMException; 56 import org.jdom.input.SAXBuilder; 57 58 64 public class MavenSnapshotDependency implements SourceControl { 65 66 private Hashtable properties = new Hashtable (); 67 private String property; 68 private List modifications; 69 private File projectFile; 70 private File localRepository = new File (System.getProperty("user.home") + "/.maven/repository/"); 71 private String user; 72 73 74 private static Logger log = Logger.getLogger(MavenSnapshotDependency.class); 75 76 79 public void setProjectFile(String s) { 80 projectFile = new File (s); 81 } 82 83 86 public void setLocalRepository(String s) { 87 localRepository = new File (s); 88 } 89 90 93 public void setUser(String s) { 94 user = s; 95 } 96 97 public void setProperty(String property) { 98 this.property = property; 99 } 100 101 public Map getProperties() { 102 return properties; 103 } 104 105 public void validate() throws CruiseControlException { 106 ValidationHelper.assertIsSet(projectFile, "projectFile", this.getClass()); 107 ValidationHelper.assertTrue(projectFile.exists(), 108 "Project file '" + projectFile.getAbsolutePath() + "' does not exist."); 109 ValidationHelper.assertFalse(projectFile.isDirectory(), 110 "The directory '" + projectFile.getAbsolutePath() 111 + "' cannot be used as the projectFile for MavenSnapshotDependency."); 112 113 ValidationHelper.assertTrue(localRepository.exists(), 114 "Local Maven repository '" + localRepository.getAbsolutePath() + "' does not exist."); 115 ValidationHelper.assertTrue(localRepository.isDirectory(), 116 "Local Maven repository '" + localRepository.getAbsolutePath() 117 + "' must be a directory."); 118 } 119 120 129 public List getModifications(Date lastBuild, Date now) { 130 modifications = new ArrayList (); 131 132 checkProjectDependencies(projectFile, lastBuild.getTime()); 133 134 return modifications; 135 } 136 137 141 private void addRevision(File dependency) { 142 Modification mod = new Modification("maven"); 143 Modification.ModifiedFile modfile = mod.createModifiedFile(dependency.getName(), dependency.getParent()); 144 modfile.action = "change"; 145 146 mod.userName = user; 147 mod.modifiedTime = new Date (dependency.lastModified()); 148 mod.comment = "Maven project dependency: timestamp change detected."; 149 modifications.add(mod); 150 151 if (property != null) { 152 properties.put(property, "true"); 153 } 154 } 155 156 160 private void checkProjectDependencies(File projectFile, long lastBuild) { 161 List filenames = getSnapshotFilenames(projectFile); 162 Iterator itr = filenames.iterator(); 163 while (itr.hasNext()) { 164 String filename = (String ) itr.next(); 165 File dependency = new File (filename); 166 checkFile(dependency, lastBuild); 167 } 168 } 169 170 173 List getSnapshotFilenames(File mavenFile) { 174 List filenames = new ArrayList (); 175 Element mavenElement; 176 SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser"); 177 try { 178 mavenElement = builder.build(mavenFile).getRootElement(); 179 } catch (JDOMException e) { 180 log.error("failed to load project file [" 181 + (mavenFile != null ? mavenFile.getAbsolutePath() : "") 182 + "]", e); 183 return filenames; 184 } catch (IOException e) { 185 log.error("failed to load project file [" 186 + (mavenFile != null ? mavenFile.getAbsolutePath() : "") 187 + "]", e); 188 return filenames; 189 } 190 Element depsRoot = mavenElement.getChild("dependencies"); 191 192 if (depsRoot == null) { 194 return filenames; 195 } 196 List dependencies = depsRoot.getChildren(); 197 Iterator itr = dependencies.iterator(); 198 while (itr.hasNext()) { 199 Element dependency = (Element) itr.next(); 200 String versionText = dependency.getChildText("version"); 201 if (versionText != null && versionText.endsWith("SNAPSHOT")) { 202 String groupId = dependency.getChildText("groupId"); 203 String artifactId = dependency.getChildText("artifactId"); 204 String id = dependency.getChildText("id"); 205 String type = dependency.getChildText("type"); 206 if (type == null) { 207 type = "jar"; 208 } 209 210 StringBuffer fileName = new StringBuffer (); 213 fileName.append(localRepository.getAbsolutePath()); 214 fileName.append('/'); 215 if (groupId != null) { 216 fileName.append(groupId); 217 } else { 218 fileName.append(id); 219 } 220 fileName.append('/'); 221 fileName.append(type); 222 fileName.append('s'); 223 fileName.append('/'); 224 if (artifactId != null) { 225 fileName.append(artifactId); 226 } else { 227 fileName.append(id); 228 } 229 fileName.append('-'); 230 fileName.append(versionText); 231 fileName.append('.'); 232 if ("uberjar".equals(type) || "ejb".equals(type) 233 || "plugin".equals(type)) { 234 fileName.append("jar"); 235 } else { 236 fileName.append(type); 237 } 238 File file = new File (fileName.toString()); 239 filenames.add(file.getAbsolutePath()); 240 } 241 } 242 return filenames; 243 } 244 245 246 private void checkFile(File file, long lastBuild) { 247 if ((!file.isDirectory()) && (file.lastModified() > lastBuild)) { 248 addRevision(file); 249 } 250 } 251 } 252 | Popular Tags |