KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > ant > IvyPublish


1 /*
2  * This file is subject to the license found in LICENCE.TXT in the root directory of the project.
3  *
4  * #SNAPSHOT#
5  */

6 package fr.jayasoft.ivy.ant;
7
8 import java.io.File JavaDoc;
9 import java.net.URL JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.Date JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.apache.tools.ant.BuildException;
19
20 import fr.jayasoft.ivy.Artifact;
21 import fr.jayasoft.ivy.ArtifactRevisionId;
22 import fr.jayasoft.ivy.Ivy;
23 import fr.jayasoft.ivy.ModuleRevisionId;
24 import fr.jayasoft.ivy.util.IvyPatternHelper;
25 import fr.jayasoft.ivy.util.Message;
26
27 /**
28  * This task allow to publish a module revision to an Ivy repository.
29  *
30  * @author Xavier Hanin
31  *
32  */

33 public class IvyPublish extends IvyTask {
34     private String JavaDoc _organisation;
35     private String JavaDoc _module;
36     private String JavaDoc _revision;
37     private String JavaDoc _pubRevision;
38     private File JavaDoc _cache;
39     private String JavaDoc _srcivypattern;
40     private String JavaDoc _status;
41     private String JavaDoc _conf=null;
42     private String JavaDoc _pubdate;
43     private String JavaDoc _deliverTarget;
44     private String JavaDoc _publishResolverName = null;
45     private List JavaDoc _artifactspattern = new ArrayList JavaDoc();
46     private File JavaDoc _deliveryList;
47     private boolean _publishivy = true;
48     private boolean _warnonmissing = true;
49     private boolean _haltonmissing = true;
50     private boolean _overwrite = false;
51     private boolean _update = false;
52     private boolean _replacedynamicrev = true;
53     private boolean _forcedeliver;
54     private Collection JavaDoc _artifacts = new ArrayList JavaDoc();
55     
56     public File JavaDoc getCache() {
57         return _cache;
58     }
59     public void setCache(File JavaDoc cache) {
60         _cache = cache;
61     }
62     public String JavaDoc getSrcivypattern() {
63         return _srcivypattern;
64     }
65     public void setSrcivypattern(String JavaDoc destivypattern) {
66         _srcivypattern = destivypattern;
67     }
68     /**
69      * @deprecated use getSrcivypattern instead
70      * @return
71      */

72     public String JavaDoc getDeliverivypattern() {
73         return _srcivypattern;
74     }
75     /**
76      * @deprecated use setSrcivypattern instead
77      * @return
78      */

79     public void setDeliverivypattern(String JavaDoc destivypattern) {
80         _srcivypattern = destivypattern;
81     }
82     public String JavaDoc getModule() {
83         return _module;
84     }
85     public void setModule(String JavaDoc module) {
86         _module = module;
87     }
88     public String JavaDoc getOrganisation() {
89         return _organisation;
90     }
91     public void setOrganisation(String JavaDoc organisation) {
92         _organisation = organisation;
93     }
94     public String JavaDoc getPubdate() {
95         return _pubdate;
96     }
97     public void setPubdate(String JavaDoc pubdate) {
98         _pubdate = pubdate;
99     }
100     public String JavaDoc getPubrevision() {
101         return _pubRevision;
102     }
103     public void setPubrevision(String JavaDoc pubRevision) {
104         _pubRevision = pubRevision;
105     }
106     public String JavaDoc getRevision() {
107         return _revision;
108     }
109     public void setRevision(String JavaDoc revision) {
110         _revision = revision;
111     }
112     public String JavaDoc getStatus() {
113         return _status;
114     }
115     public void setStatus(String JavaDoc status) {
116         _status = status;
117     }
118     public void setConf(String JavaDoc conf) {
119         _conf = conf;
120     }
121     public void setDelivertarget(String JavaDoc deliverTarget) {
122         _deliverTarget = deliverTarget;
123     }
124     public void setDeliveryList(File JavaDoc deliveryList) {
125         _deliveryList = deliveryList;
126     }
127     public String JavaDoc getResolver() {
128         return _publishResolverName;
129     }
130     public void setResolver(String JavaDoc publishResolverName) {
131         _publishResolverName = publishResolverName;
132     }
133     public String JavaDoc getArtifactspattern() {
134         return (String JavaDoc) (_artifactspattern.isEmpty()?null:_artifactspattern.get(0));
135     }
136     public void setArtifactspattern(String JavaDoc artifactsPattern) {
137         _artifactspattern.clear();
138         _artifactspattern.add(artifactsPattern);
139     }
140     public void addArtifactspattern(String JavaDoc artifactsPattern) {
141         _artifactspattern.add(artifactsPattern);
142     }
143     public void addConfiguredArtifacts(ArtifactsPattern p) {
144         _artifactspattern.add(p.getPattern());
145     }
146     public boolean isReplacedynamicrev() {
147         return _replacedynamicrev;
148     }
149     public void setReplacedynamicrev(boolean replacedynamicrev) {
150         _replacedynamicrev = replacedynamicrev;
151     }
152     
153     public void execute() throws BuildException {
154         Ivy ivy = getIvyInstance();
155         _organisation = getProperty(_organisation, ivy, "ivy.organisation");
156         _module = getProperty(_module, ivy, "ivy.module");
157         _revision = getProperty(_revision, ivy, "ivy.revision");
158         _pubRevision = getProperty(_pubRevision, ivy, "ivy.deliver.revision");
159         if (_cache == null) {
160             _cache = ivy.getDefaultCache();
161         }
162         if (_artifactspattern.isEmpty()) {
163             String JavaDoc p = getProperty(null, ivy, "ivy.publish.src.artifacts.pattern");
164             if (p != null) {
165                 _artifactspattern.add(p);
166             }
167         }
168         if (_srcivypattern == null) {
169             _srcivypattern = getArtifactspattern();
170         }
171         _status = getProperty(_status, ivy, "ivy.status");
172         if (_organisation == null) {
173             throw new BuildException("no organisation provided for ivy publish task: It can either be set explicitely via the attribute 'organisation' or via 'ivy.organisation' property or a prior call to <resolve/>");
174         }
175         if (_module == null) {
176             throw new BuildException("no module name provided for ivy publish task: It can either be set explicitely via the attribute 'module' or via 'ivy.module' property or a prior call to <resolve/>");
177         }
178         if (_revision == null) {
179             throw new BuildException("no module revision provided for ivy publish task: It can either be set explicitely via the attribute 'revision' or via 'ivy.revision' property or a prior call to <resolve/>");
180         }
181         if (_artifactspattern.isEmpty()) {
182             throw new BuildException("no artifacts pattern: either provide it through parameter or through ivy.publish.src.artifacts.pattern property");
183         }
184         if (_publishResolverName == null) {
185             throw new BuildException("no publish deliver name: please provide it through parameter 'resolver'");
186         }
187         if ("working".equals(_revision)) {
188             _revision = "working@"+Ivy.getLocalHostName();
189         }
190         Date JavaDoc pubdate = getPubDate(_pubdate, new Date JavaDoc());
191         if (_pubRevision == null) {
192             if (_revision.startsWith("working@")) {
193                 _pubRevision = Ivy.DATE_FORMAT.format(pubdate);
194             } else {
195                 _pubRevision = _revision;
196             }
197         }
198         if (_status == null) {
199             throw new BuildException("no status provided: either provide it as parameter or through the ivy.status.default property");
200         }
201         ModuleRevisionId mrid = ModuleRevisionId.newInstance(_organisation, _module, _revision);
202         try {
203             File JavaDoc ivyFile = new File JavaDoc(IvyPatternHelper.substitute(_srcivypattern, _organisation, _module, _pubRevision, "ivy", "ivy", "xml"));
204             if (null!=_conf && isPublishivy()) {
205                 if (isPublishivy()) {
206                     Message.warn("Impossible to publish ivy file when conf is specified");
207                     Message.warn("Please, set publishivy to false");
208                     setPublishivy(false);
209                 }
210             }
211             if (_publishivy && (!ivyFile.exists() || _forcedeliver)) {
212                 IvyDeliver deliver = new IvyDeliver();
213                 deliver.setProject(getProject());
214                 deliver.setCache(getCache());
215                 deliver.setDeliverpattern(getSrcivypattern());
216                 deliver.setDelivertarget(_deliverTarget);
217                 deliver.setDeliveryList(_deliveryList);
218                 deliver.setModule(getModule());
219                 deliver.setOrganisation(getOrganisation());
220                 deliver.setPubdate(Ivy.DATE_FORMAT.format(pubdate));
221                 deliver.setPubrevision(getPubrevision());
222                 deliver.setRevision(getRevision());
223                 deliver.setStatus(getStatus());
224                 deliver.setValidate(doValidate(ivy));
225                 deliver.setReplacedynamicrev(isReplacedynamicrev());
226                 
227                 deliver.execute();
228             }
229             
230             Collection JavaDoc missing = ivy.publish(
231                     mrid,
232                     _pubRevision,
233                     _cache,
234                     _artifactspattern,
235                     _publishResolverName,
236                     _publishivy?_srcivypattern:null,
237                     getStatus(),
238                     pubdate,
239                     (Artifact[]) _artifacts.toArray(new Artifact[_artifacts.size()]), doValidate(ivy),
240                     _overwrite,
241                     _update,
242                     _conf);
243             if (_warnonmissing) {
244                 for (Iterator JavaDoc iter = missing.iterator(); iter.hasNext();) {
245                     Artifact artifact = (Artifact)iter.next();
246                     Message.warn("missing artifact: "+artifact);
247                 }
248             }
249             if (_haltonmissing && !missing.isEmpty()) {
250                 throw new BuildException("missing published artifacts for "+mrid+": "+missing);
251             }
252             
253         } catch (Exception JavaDoc e) {
254             throw new BuildException("impossible to publish artifacts for "+mrid+": "+e, e);
255         }
256     }
257     public PublishArtifact createArtifact() {
258         PublishArtifact art = new PublishArtifact();
259         _artifacts .add(art);
260         return art;
261     }
262     public boolean isPublishivy() {
263         return _publishivy;
264     }
265     
266     public void setPublishivy(boolean publishivy) {
267         _publishivy = publishivy;
268     }
269     public boolean isWarnonmissing() {
270         return _warnonmissing;
271     }
272     
273     public void setWarnonmissing(boolean warnonmissing) {
274         _warnonmissing = warnonmissing;
275     }
276     public boolean isHaltonmissing() {
277         return _haltonmissing;
278     }
279     
280     public void setHaltonmissing(boolean haltonmissing) {
281         _haltonmissing = haltonmissing;
282     }
283     public boolean isOverwrite() {
284         return _overwrite;
285     }
286     public void setOverwrite(boolean overwrite) {
287         _overwrite = overwrite;
288     }
289     public void setForcedeliver(boolean b) {
290         _forcedeliver = b;
291     }
292     public boolean isForcedeliver() {
293         return _forcedeliver;
294     }
295     public boolean isUpdate() {
296         return _update;
297     }
298     public void setUpdate(boolean update) {
299         _update = update;
300     }
301     public class PublishArtifact implements Artifact {
302         private String JavaDoc _ext;
303         private String JavaDoc _name;
304         private String JavaDoc _type;
305
306         public String JavaDoc[] getConfigurations() {
307             return null;
308         }
309
310         public String JavaDoc getExt() {
311             return _ext==null?_type:_ext;
312         }
313
314         public ArtifactRevisionId getId() {
315             return null;
316         }
317
318         public ModuleRevisionId getModuleRevisionId() {
319             return null;
320         }
321
322         public String JavaDoc getName() {
323             return _name;
324         }
325
326         public Date JavaDoc getPublicationDate() {
327             return null;
328         }
329
330         public String JavaDoc getType() {
331             return _type;
332         }
333
334         public URL JavaDoc getUrl() {
335             return null;
336         }
337
338         public void setExt(String JavaDoc ext) {
339             _ext = ext;
340         }
341
342         public void setName(String JavaDoc name) {
343             _name = name;
344         }
345
346         public void setType(String JavaDoc type) {
347             _type = type;
348         }
349
350         public String JavaDoc getAttribute(String JavaDoc attName) {
351             return null;
352         }
353
354         public Map JavaDoc getAttributes() {
355             return new HashMap JavaDoc();
356         }
357
358         public String JavaDoc getExtraAttribute(String JavaDoc attName) {
359             return null;
360         }
361
362         public Map JavaDoc getExtraAttributes() {
363             return new HashMap JavaDoc();
364         }
365
366         public String JavaDoc getStandardAttribute(String JavaDoc attName) {
367             return null;
368         }
369
370         public Map JavaDoc getStandardAttributes() {
371             return new HashMap JavaDoc();
372         }
373     }
374     public static class ArtifactsPattern {
375         private String JavaDoc _pattern;
376
377         public String JavaDoc getPattern() {
378             return _pattern;
379         }
380
381         public void setPattern(String JavaDoc pattern) {
382             _pattern = pattern;
383         }
384     }
385 }
386
Popular Tags