KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > savant > ant > taskdefs > ArtifactCopyTask


1 /*
2  * Copyright (c) 2002-2004, Inversoft, All Rights Reserved
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.savant.ant.taskdefs;
8
9
10 import java.io.File JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13
14 import org.apache.tools.ant.BuildException;
15 import org.apache.tools.ant.taskdefs.Copy;
16 import org.apache.tools.ant.types.FileSet;
17 import org.apache.tools.ant.types.PatternSet;
18
19 import com.inversoft.savant.Artifact;
20 import com.inversoft.savant.ArtifactGroup;
21 import com.inversoft.savant.LocalCacheStore;
22 import com.inversoft.savant.SavantException;
23 import com.inversoft.savant.ant.types.ArtifactGroupType;
24 import com.inversoft.savant.ant.types.ArtifactType;
25
26
27 /**
28  * <p>
29  * This class is used to copy artifacts from the local cache
30  * store to a directory. This class can be used the same as
31  * a normal copy task, but also supports nested artifact
32  * and artifactgroup elements.
33  * </p>
34  *
35  * @author Brian Pontarelli
36  */

37 public class ArtifactCopyTask extends Copy {
38
39     private File JavaDoc localCache;
40     private final List JavaDoc artifacts = new ArrayList JavaDoc();
41     private final List JavaDoc groups = new ArrayList JavaDoc();
42
43
44     /**
45      * Sets the location of the local cache. This can also be changed via a
46      * System property. See {@link LocalCacheStore} for more details.
47      *
48      * @param localCache The local cache location
49      */

50     public void setLocalcache(File JavaDoc localCache) {
51         this.localCache = localCache;
52     }
53
54     /**
55      * Adds a new artifact definition to this list of artifacts to copy.
56      *
57      * @param artifact The artifact to copy
58      */

59     public void addArtifact(ArtifactType artifact) {
60         artifacts.add(artifact.getProxy());
61     }
62
63     /**
64      * Adds a new artifact group definition to this list of artifacts to copy.
65      *
66      * @param group The new group
67      */

68     public void addArtifactgroup(ArtifactGroupType group) {
69         groups.add(group.getProxy());
70     }
71
72     /**
73      * Performs the copy of all the artifacts.
74      */

75     public void execute() {
76         LocalCacheStore lcs;
77         try {
78             lcs = new LocalCacheStore(localCache);
79         } catch (SavantException se) {
80             log("Invalid local cache location [" + localCache.getAbsolutePath() + "]");
81             throw new BuildException(se);
82         }
83
84         FileSet fs = new FileSet();
85         fs.setProject(getProject());
86         fs.setDir(lcs.getLocation());
87         addIncludes(fs, artifacts);
88
89         for (int i = 0; i < groups.size(); i++) {
90             ArtifactGroup artifactGroup = (ArtifactGroup) groups.get(i);
91             addIncludes(fs, artifactGroup.getArtifacts());
92         }
93
94         super.addFileset(fs);
95         super.execute();
96     }
97
98     /**
99      * Creates a file set include definition for all the artifacts in the given
100      * list.
101      */

102     private void addIncludes(FileSet fs, List JavaDoc artifacts) {
103         for (int i = 0; i < artifacts.size(); i++) {
104             Artifact artifact = (Artifact) artifacts.get(i);
105             PatternSet.NameEntry ne = fs.createInclude();
106             ne.setName(artifact.getArtifactFile());
107         }
108     }
109 }
Popular Tags