KickJava   Java API By Example, From Geeks To Geeks.

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


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.MalformedURLException JavaDoc;
10 import java.text.ParseException JavaDoc;
11
12 import org.apache.tools.ant.BuildException;
13 import org.apache.tools.ant.Project;
14
15 import fr.jayasoft.ivy.Ivy;
16 import fr.jayasoft.ivy.ModuleDescriptor;
17 import fr.jayasoft.ivy.ModuleRevisionId;
18 import fr.jayasoft.ivy.filter.FilterHelper;
19 import fr.jayasoft.ivy.report.ResolveReport;
20 import fr.jayasoft.ivy.util.Message;
21
22 /**
23  * This task allow to call the Ivy dependency resolution from ant.
24  *
25  * @author Xavier Hanin
26  *
27  */

28 public class IvyResolve extends IvyTask {
29     private File JavaDoc _file = null;
30     private String JavaDoc _conf = null;
31     private File JavaDoc _cache = null;
32     private String JavaDoc _organisation = null;
33     private String JavaDoc _module = null;
34     private String JavaDoc _revision = null;
35     private String JavaDoc _pubdate = null;
36     private boolean _inline = false;
37     private boolean _haltOnFailure = true;
38     private boolean _useCacheOnly = false;
39     private String JavaDoc _type = null;
40     private boolean _transitive = true;
41     private boolean _changing = false;
42     private Boolean JavaDoc _keep = null;
43     private String JavaDoc _failureProperty = null;
44     private boolean _useOrigin = false;
45     
46     public boolean isUseOrigin() {
47         return _useOrigin;
48     }
49     
50     public void setUseOrigin(boolean useOrigin) {
51         _useOrigin = useOrigin;
52     }
53     
54     public String JavaDoc getDate() {
55         return _pubdate;
56     }
57     public void setDate(String JavaDoc pubdate) {
58         _pubdate = pubdate;
59     }
60     public String JavaDoc getRevision() {
61         return _revision;
62     }
63     public void setRevision(String JavaDoc revision) {
64         _revision = revision;
65     }
66     public File JavaDoc getCache() {
67         return _cache;
68     }
69     public void setCache(File JavaDoc cache) {
70         _cache = cache;
71     }
72     public String JavaDoc getConf() {
73         return _conf;
74     }
75     public void setConf(String JavaDoc conf) {
76         _conf = conf;
77     }
78     public File JavaDoc getFile() {
79         return _file;
80     }
81     public void setFile(File JavaDoc file) {
82         _file = file;
83     }
84     public boolean isHaltonfailure() {
85         return _haltOnFailure;
86     }
87     public void setHaltonfailure(boolean haltOnFailure) {
88         _haltOnFailure = haltOnFailure;
89     }
90     public void setShowprogress(boolean show) {
91         Message.setShowProgress(show);
92     }
93     public boolean isUseCacheOnly() {
94         return _useCacheOnly;
95     }
96     public void setUseCacheOnly(boolean useCacheOnly) {
97         _useCacheOnly = useCacheOnly;
98     }
99     public String JavaDoc getType() {
100         return _type;
101     }
102     public void setType(String JavaDoc type) {
103         _type = type;
104     }
105     /**
106      * @deprecated Use {@link #setFailureProperty(String)} instead
107      */

108     public void setFailurePropery(String JavaDoc failureProperty) {
109         log("The 'failurepropery' attribute is deprecated. " +
110                 "Please use the 'failureproperty' attribute instead", Project.MSG_WARN);
111         setFailureProperty(failureProperty);
112     }
113     public void setFailureProperty(String JavaDoc failureProperty) {
114         _failureProperty = failureProperty;
115     }
116     public String JavaDoc getFailureProperty() {
117         return _failureProperty;
118     }
119     
120     public void execute() throws BuildException {
121         Ivy ivy = getIvyInstance();
122         try {
123             _conf = getProperty(_conf, ivy, "ivy.configurations");
124             _type = getProperty(_type, ivy, "ivy.resolve.default.type.filter");
125             if (_cache == null) {
126                 _cache = ivy.getDefaultCache();
127             }
128             String JavaDoc[] confs = splitConfs(_conf);
129             
130             ResolveReport report;
131             if (isInline()) {
132                 if (_organisation == null) {
133                     throw new BuildException("'organisation' is required when using inline mode");
134                 }
135                 if (_module == null) {
136                     throw new BuildException("'module' is required when using inline mode");
137                 }
138                 if (_file != null) {
139                     throw new BuildException("'file' not allowed when using inline mode");
140                 }
141                 if (_revision == null) {
142                     _revision = "latest.integration";
143                 }
144                 report = ivy.resolve(
145                         ModuleRevisionId.newInstance(_organisation, _module, _revision),
146                         confs,
147                         _transitive,
148                         _changing,
149                         _cache,
150                         getPubDate(_pubdate, null),
151                         doValidate(ivy),
152                         _useCacheOnly,
153                         _useOrigin,
154                         FilterHelper.getArtifactTypeFilter(_type));
155                 
156             } else {
157                 if (_organisation != null) {
158                     throw new BuildException("'organisation' not allowed when not using 'org' attribute");
159                 }
160                 if (_module != null) {
161                     throw new BuildException("'module' not allowed when not using 'org' attribute");
162                 }
163                 if (_file == null) {
164                     _file = new File JavaDoc(getProject().getBaseDir(), getProperty(ivy, "ivy.dep.file"));
165                 }
166                 _revision = getProperty(_revision, ivy, "ivy.revision");
167                 report = ivy.resolve(
168                         _file.toURL(),
169                         _revision,
170                         confs,
171                         _cache,
172                         getPubDate(_pubdate, null),
173                         doValidate(ivy),
174                         _useCacheOnly,
175                         _transitive,
176                         _useOrigin,
177                         FilterHelper.getArtifactTypeFilter(_type));
178             }
179             if (report.hasError()) {
180                 if (_failureProperty != null) {
181                     getProject().setProperty(_failureProperty, "true");
182                 }
183                 if (isHaltonfailure()) {
184                     throw new BuildException("resolve failed - see output for details");
185                 }
186             }
187             setResolved(report, isKeep());
188             
189             if (isKeep()) {
190                 ModuleDescriptor md = report.getModuleDescriptor();
191                 // put resolved infos in ant properties and ivy variables
192
// putting them in ivy variables is important to be able to change from one resolve call to the other
193
getProject().setProperty("ivy.organisation", md.getModuleRevisionId().getOrganisation());
194                 ivy.setVariable("ivy.organisation", md.getModuleRevisionId().getOrganisation());
195                 getProject().setProperty("ivy.module", md.getModuleRevisionId().getName());
196                 ivy.setVariable("ivy.module", md.getModuleRevisionId().getName());
197                 getProject().setProperty("ivy.revision", md.getResolvedModuleRevisionId().getRevision());
198                 ivy.setVariable("ivy.revision", md.getResolvedModuleRevisionId().getRevision());
199                 boolean hasChanged = report.hasChanged();
200                 getProject().setProperty("ivy.deps.changed", String.valueOf(hasChanged));
201                 ivy.setVariable("ivy.deps.changed", String.valueOf(hasChanged));
202                 if (_conf.trim().equals("*")) {
203                     getProject().setProperty("ivy.resolved.configurations", mergeConfs(md.getConfigurationsNames()));
204                     ivy.setVariable("ivy.resolved.configurations", mergeConfs(md.getConfigurationsNames()));
205                 } else {
206                     getProject().setProperty("ivy.resolved.configurations", _conf);
207                     ivy.setVariable("ivy.resolved.configurations", _conf);
208                 }
209             }
210         } catch (MalformedURLException JavaDoc e) {
211             throw new BuildException("unable to convert given ivy file to url: "+_file+": "+e, e);
212         } catch (ParseException JavaDoc e) {
213             log(e.getMessage(), Project.MSG_ERR);
214             throw new BuildException("syntax errors in ivy file: "+e, e);
215         } catch (Exception JavaDoc e) {
216             throw new BuildException("impossible to resolve dependencies: "+e, e);
217         }
218     }
219     public String JavaDoc getModule() {
220         return _module;
221     }
222     public void setModule(String JavaDoc module) {
223         _module = module;
224     }
225     public String JavaDoc getOrganisation() {
226         return _organisation;
227     }
228     public void setOrganisation(String JavaDoc organisation) {
229         _organisation = organisation;
230     }
231     public boolean isTransitive() {
232         return _transitive;
233     }
234     public void setTransitive(boolean transitive) {
235         _transitive = transitive;
236     }
237     public boolean isChanging() {
238         return _changing;
239     }
240     public void setChanging(boolean changing) {
241         _changing = changing;
242     }
243     public boolean isKeep() {
244         return _keep == null ? _organisation == null : _keep.booleanValue();
245     }
246     public void setKeep(boolean keep) {
247         _keep = Boolean.valueOf(keep);
248     }
249     public boolean isInline() {
250         return _inline;
251     }
252     public void setInline(boolean inline) {
253         _inline = inline;
254     }
255 }
256
Popular Tags