1 package hudson.tasks; 2 3 import hudson.FilePath; 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 org.kohsuke.stapler.StaplerRequest; 11 12 import java.io.File ; 13 import java.io.IOException ; 14 15 20 public class ArtifactArchiver extends Publisher { 21 22 25 private final String artifacts; 26 27 30 private final String excludes; 31 32 35 private final boolean latestOnly; 36 37 public ArtifactArchiver(String artifacts, String excludes, boolean latestOnly) { 38 this.artifacts = artifacts; 39 this.excludes = excludes; 40 this.latestOnly = latestOnly; 41 } 42 43 public String getArtifacts() { 44 return artifacts; 45 } 46 47 public String getExcludes() { 48 return excludes; 49 } 50 51 public boolean isLatestOnly() { 52 return latestOnly; 53 } 54 55 public boolean perform(Build build, Launcher launcher, BuildListener listener) throws InterruptedException { 56 Project p = build.getProject(); 57 58 File dir = build.getArtifactsDir(); 59 dir.mkdirs(); 60 61 try { 62 p.getWorkspace().copyRecursiveTo(artifacts,excludes,new FilePath(dir)); 63 } catch (IOException e) { 64 Util.displayIOException(e,listener); 65 e.printStackTrace(listener.error("Failed to archive artifacts: "+artifacts)); 66 return true; 67 } 68 69 if(latestOnly) { 70 Build b = p.getLastSuccessfulBuild(); 71 if(b!=null) { 72 while(true) { 73 b = b.getPreviousBuild(); 74 if(b==null) break; 75 76 File ad = b.getArtifactsDir(); 78 if(ad.exists()) { 79 listener.getLogger().println("Deleting old artifacts from "+b.getDisplayName()); 80 try { 81 Util.deleteRecursive(ad); 82 } catch (IOException e) { 83 e.printStackTrace(listener.error(e.getMessage())); 84 } 85 } 86 } 87 } 88 } 89 90 return true; 91 } 92 93 public Descriptor<Publisher> getDescriptor() { 94 return DESCRIPTOR; 95 } 96 97 98 public static final Descriptor<Publisher> DESCRIPTOR = new Descriptor<Publisher>(ArtifactArchiver.class) { 99 public String getDisplayName() { 100 return "Archive the artifacts"; 101 } 102 103 public String getHelpFile() { 104 return "/help/project-config/archive-artifact.html"; 105 } 106 107 public Publisher newInstance(StaplerRequest req) { 108 return new ArtifactArchiver( 109 req.getParameter("artifacts").trim(), 110 Util.fixEmpty(req.getParameter("artifacts_excludes").trim()), 111 req.getParameter("artifacts_latest_only")!=null); 112 } 113 }; 114 } 115 | Popular Tags |