KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > tasks > ArtifactArchiver


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 JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 /**
16  * Copies the artifacts into an archive directory.
17  *
18  * @author Kohsuke Kawaguchi
19  */

20 public class ArtifactArchiver extends Publisher {
21
22     /**
23      * Comma-separated list of files/directories to be archived.
24      */

25     private final String JavaDoc artifacts;
26
27     /**
28      * Possibly null 'excludes' pattern as in Ant.
29      */

30     private final String JavaDoc excludes;
31
32     /**
33      * Just keep the last successful artifact set, no more.
34      */

35     private final boolean latestOnly;
36
37     public ArtifactArchiver(String JavaDoc artifacts, String JavaDoc excludes, boolean latestOnly) {
38         this.artifacts = artifacts;
39         this.excludes = excludes;
40         this.latestOnly = latestOnly;
41     }
42
43     public String JavaDoc getArtifacts() {
44         return artifacts;
45     }
46
47     public String JavaDoc 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 JavaDoc {
56         Project p = build.getProject();
57
58         File JavaDoc dir = build.getArtifactsDir();
59         dir.mkdirs();
60
61         try {
62             p.getWorkspace().copyRecursiveTo(artifacts,excludes,new FilePath(dir));
63         } catch (IOException JavaDoc 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                     // remove old artifacts
77
File JavaDoc 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 JavaDoc 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 JavaDoc getDisplayName() {
100             return "Archive the artifacts";
101         }
102
103         public String JavaDoc 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