1 37 package net.sourceforge.cruisecontrol.publishers; 38 39 import net.sourceforge.cruisecontrol.CruiseControlException; 40 import net.sourceforge.cruisecontrol.Publisher; 41 import net.sourceforge.cruisecontrol.util.ValidationHelper; 42 import net.sourceforge.cruisecontrol.util.XMLLogHelper; 43 import org.apache.tools.ant.Project; 44 import org.apache.tools.ant.taskdefs.Copy; 45 import org.apache.tools.ant.types.FileSet; 46 import org.apache.tools.ant.util.FileUtils; 47 import org.jdom.Element; 48 49 import java.io.File ; 50 import java.io.IOException ; 51 52 public class ArtifactsPublisher implements Publisher { 53 54 private Copy copier = new Copy(); 55 56 private String destDir; 57 private String targetDirectory; 58 private String targetFile; 59 private String subdirectory; 60 private boolean publishOnFailure = true; 61 62 public void setDest(String dir) { 63 destDir = dir; 64 } 65 66 public void setDir(String pDir) { 67 targetDirectory = pDir; 68 } 69 70 public void setFile(String file) { 71 targetFile = file; 72 } 73 74 public void setPublishOnFailure(boolean shouldPublish) { 75 publishOnFailure = shouldPublish; 76 } 77 78 public void publish(Element cruisecontrolLog) 79 throws CruiseControlException { 80 XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog); 81 if (shouldPublish(helper.isBuildSuccessful())) { 82 Project project = new Project(); 83 String timestamp = helper.getBuildTimestamp(); 84 File destinationDirectory = getDestinationDirectory(timestamp); 85 86 if (targetDirectory != null) { 87 publishDirectory(project, destinationDirectory); 88 } 89 if (targetFile != null) { 90 publishFile(destinationDirectory); 91 } 92 } 93 } 94 95 protected boolean shouldPublish(final boolean buildSuccessful) { 96 return buildSuccessful || publishOnFailure; 97 } 98 99 File getDestinationDirectory(String timestamp) { 100 String targetDir = timestamp; 101 if (subdirectory != null) { 102 targetDir = timestamp + File.separatorChar + subdirectory; 103 } 104 return new File (destDir, targetDir); 105 } 106 107 void publishFile(File uniqueDest) throws CruiseControlException { 108 File file = new File (targetFile); 109 if (!file.exists()) { 110 throw new CruiseControlException("target file " + file.getAbsolutePath() + " does not exist"); 111 } 112 FileUtils utils = FileUtils.newFileUtils(); 113 try { 114 utils.copyFile(file, new File (uniqueDest, file.getName())); 115 } catch (IOException e) { 116 throw new CruiseControlException(e); 117 } 118 } 119 120 void publishDirectory(Project project, File uniqueDest) throws CruiseControlException { 121 File directory = new File (targetDirectory); 122 if (!directory.exists()) { 123 throw new CruiseControlException("target directory " + directory.getAbsolutePath() + " does not exist"); 124 } 125 if (!directory.isDirectory()) { 126 throw new CruiseControlException("target directory " + directory.getAbsolutePath() + " is not a directory"); 127 } 128 FileSet set = new FileSet(); 129 set.setDir(directory); 130 copier.addFileset(set); 131 copier.setTodir(uniqueDest); 132 copier.setProject(project); 133 try { 134 copier.execute(); 135 } catch (Exception e) { 136 throw new CruiseControlException(e); 137 } 138 } 139 140 public void validate() throws CruiseControlException { 141 ValidationHelper.assertIsSet(destDir, "dest", this.getClass()); 142 143 ValidationHelper.assertFalse(targetDirectory == null && targetFile == null, 144 "'dir' or 'file' must be specified in configuration file."); 145 146 ValidationHelper.assertFalse(targetDirectory != null && targetFile != null, 147 "only one of 'dir' or 'file' may be specified."); 148 } 149 150 public void setSubdirectory(String subdir) { 151 subdirectory = subdir; 152 } 153 } 154 | Popular Tags |