001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.ant;
019:
020: import java.io.File;
021: import java.net.MalformedURLException;
022: import java.text.ParseException;
023: import java.util.Arrays;
024: import java.util.Collection;
025:
026: import org.apache.ivy.Ivy;
027: import org.apache.ivy.core.LogOptions;
028: import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
029: import org.apache.ivy.core.module.id.ModuleRevisionId;
030: import org.apache.ivy.core.report.ResolveReport;
031: import org.apache.ivy.core.resolve.ResolveOptions;
032: import org.apache.ivy.core.resolve.ResolveProcessException;
033: import org.apache.ivy.core.settings.IvySettings;
034: import org.apache.ivy.util.Message;
035: import org.apache.ivy.util.filter.FilterHelper;
036: import org.apache.tools.ant.BuildException;
037: import org.apache.tools.ant.Project;
038:
039: /**
040: * This task allow to call the Ivy dependency resolution from ant.
041: */
042: public class IvyResolve extends IvyTask {
043: private File file = null;
044:
045: private String conf = null;
046:
047: private String organisation = null;
048:
049: private String module = null;
050:
051: private String revision = null;
052:
053: private String pubdate = null;
054:
055: private boolean inline = false;
056:
057: private boolean haltOnFailure = true;
058:
059: private boolean useCacheOnly = false;
060:
061: private String type = null;
062:
063: private boolean transitive = true;
064:
065: private boolean refresh = false;
066:
067: private boolean changing = false;
068:
069: private Boolean keep = null;
070:
071: private String failureProperty = null;
072:
073: private boolean useOrigin = false;
074:
075: private String resolveId = null;
076:
077: private String log = ResolveOptions.LOG_DEFAULT;
078:
079: public boolean isUseOrigin() {
080: return useOrigin;
081: }
082:
083: public void setUseOrigin(boolean useOrigin) {
084: this .useOrigin = useOrigin;
085: }
086:
087: public String getDate() {
088: return pubdate;
089: }
090:
091: public void setDate(String pubdate) {
092: this .pubdate = pubdate;
093: }
094:
095: public String getRevision() {
096: return revision;
097: }
098:
099: public void setRevision(String revision) {
100: this .revision = revision;
101: }
102:
103: public void setCache(File cache) {
104: cacheAttributeNotSupported();
105: }
106:
107: public String getConf() {
108: return conf;
109: }
110:
111: public void setConf(String conf) {
112: this .conf = conf;
113: }
114:
115: public File getFile() {
116: return file;
117: }
118:
119: public void setFile(File file) {
120: this .file = file;
121: }
122:
123: public boolean isHaltonfailure() {
124: return haltOnFailure;
125: }
126:
127: public void setHaltonfailure(boolean haltOnFailure) {
128: this .haltOnFailure = haltOnFailure;
129: }
130:
131: public void setShowprogress(boolean show) {
132: Message.setShowProgress(show);
133: }
134:
135: public boolean isUseCacheOnly() {
136: return useCacheOnly;
137: }
138:
139: public void setUseCacheOnly(boolean useCacheOnly) {
140: this .useCacheOnly = useCacheOnly;
141: }
142:
143: public String getType() {
144: return type;
145: }
146:
147: public void setType(String type) {
148: this .type = type;
149: }
150:
151: public boolean isRefresh() {
152: return refresh;
153: }
154:
155: public void setRefresh(boolean refresh) {
156: this .refresh = refresh;
157: }
158:
159: public String getLog() {
160: return log;
161: }
162:
163: public void setLog(String log) {
164: this .log = log;
165: }
166:
167: /**
168: * @deprecated Use {@link #setFailureProperty(String)} instead
169: */
170: public void setFailurePropery(String failureProperty) {
171: log("The 'failurepropery' attribute is deprecated. "
172: + "Please use the 'failureproperty' attribute instead",
173: Project.MSG_WARN);
174: setFailureProperty(failureProperty);
175: }
176:
177: public void setFailureProperty(String failureProperty) {
178: this .failureProperty = failureProperty;
179: }
180:
181: public String getFailureProperty() {
182: return failureProperty;
183: }
184:
185: public void doExecute() throws BuildException {
186: Ivy ivy = getIvyInstance();
187: IvySettings settings = ivy.getSettings();
188: try {
189: conf = getProperty(conf, settings, "ivy.configurations");
190: type = getProperty(type, settings,
191: "ivy.resolve.default.type.filter");
192: String[] confs = splitConfs(conf);
193:
194: ResolveReport report;
195: if (isInline()) {
196: if (organisation == null) {
197: throw new BuildException(
198: "'organisation' is required when using inline mode");
199: }
200: if (module == null) {
201: throw new BuildException(
202: "'module' is required when using inline mode");
203: }
204: if (file != null) {
205: throw new BuildException(
206: "'file' not allowed when using inline mode");
207: }
208: if (!getAllowedLogOptions().contains(log)) {
209: throw new BuildException(
210: "invalid option for 'log': " + log
211: + ". Available options are "
212: + getAllowedLogOptions());
213: }
214: for (int i = 0; i < confs.length; i++) {
215: if ("*".equals(confs[i])) {
216: confs[i] = "*(public)";
217: }
218: }
219: if (revision == null) {
220: revision = "latest.integration";
221: }
222: report = ivy.resolve(ModuleRevisionId.newInstance(
223: organisation, module, revision),
224: getResolveOptions(ivy, confs, settings),
225: changing);
226:
227: } else {
228: if (organisation != null) {
229: throw new BuildException(
230: "'organisation' not allowed when not using 'org' attribute");
231: }
232: if (module != null) {
233: throw new BuildException(
234: "'module' not allowed when not using 'org' attribute");
235: }
236: if (file == null) {
237: file = getProject().resolveFile(
238: getProperty(settings, "ivy.dep.file"));
239: }
240: report = ivy.resolve(file.toURL(), getResolveOptions(
241: ivy, confs, settings));
242: }
243: if (report.hasError()) {
244: if (failureProperty != null) {
245: getProject().setProperty(failureProperty, "true");
246: }
247: if (isHaltonfailure()) {
248: throw new BuildException(
249: "resolve failed - see output for details");
250: }
251: }
252: setResolved(report, resolveId, isKeep());
253:
254: if (isKeep()) {
255: ModuleDescriptor md = report.getModuleDescriptor();
256: // put resolved infos in ant properties and ivy variables
257: // putting them in ivy variables is important to be able to change from one resolve
258: // call to the other
259: getProject().setProperty("ivy.organisation",
260: md.getModuleRevisionId().getOrganisation());
261: settings.setVariable("ivy.organisation", md
262: .getModuleRevisionId().getOrganisation());
263: getProject().setProperty("ivy.module",
264: md.getModuleRevisionId().getName());
265: settings.setVariable("ivy.module", md
266: .getModuleRevisionId().getName());
267: getProject().setProperty("ivy.revision",
268: md.getResolvedModuleRevisionId().getRevision());
269: settings.setVariable("ivy.revision", md
270: .getResolvedModuleRevisionId().getRevision());
271: boolean hasChanged = report.hasChanged();
272: getProject().setProperty("ivy.deps.changed",
273: String.valueOf(hasChanged));
274: settings.setVariable("ivy.deps.changed", String
275: .valueOf(hasChanged));
276: if (conf.trim().equals("*")) {
277: getProject().setProperty(
278: "ivy.resolved.configurations",
279: mergeConfs(md.getConfigurationsNames()));
280: settings.setVariable("ivy.resolved.configurations",
281: mergeConfs(md.getConfigurationsNames()));
282: } else {
283: getProject().setProperty(
284: "ivy.resolved.configurations", conf);
285: settings.setVariable("ivy.resolved.configurations",
286: conf);
287: }
288: if (file != null) {
289: getProject().setProperty("ivy.resolved.file",
290: file.getAbsolutePath());
291: settings.setVariable("ivy.resolved.file", file
292: .getAbsolutePath());
293: }
294: if (resolveId != null) {
295: getProject().setProperty(
296: "ivy.organisation." + resolveId,
297: md.getModuleRevisionId().getOrganisation());
298: settings.setVariable("ivy.organisation."
299: + resolveId, md.getModuleRevisionId()
300: .getOrganisation());
301: getProject().setProperty("ivy.module." + resolveId,
302: md.getModuleRevisionId().getName());
303: settings.setVariable("ivy.module." + resolveId, md
304: .getModuleRevisionId().getName());
305: getProject().setProperty(
306: "ivy.revision." + resolveId,
307: md.getResolvedModuleRevisionId()
308: .getRevision());
309: settings.setVariable("ivy.revision." + resolveId,
310: md.getResolvedModuleRevisionId()
311: .getRevision());
312: getProject().setProperty(
313: "ivy.deps.changed." + resolveId,
314: String.valueOf(hasChanged));
315: settings.setVariable("ivy.deps.changed."
316: + resolveId, String.valueOf(hasChanged));
317: if (conf.trim().equals("*")) {
318: getProject()
319: .setProperty(
320: "ivy.resolved.configurations."
321: + resolveId,
322: mergeConfs(md
323: .getConfigurationsNames()));
324: settings.setVariable(
325: "ivy.resolved.configurations."
326: + resolveId, mergeConfs(md
327: .getConfigurationsNames()));
328: } else {
329: getProject().setProperty(
330: "ivy.resolved.configurations."
331: + resolveId, conf);
332: settings.setVariable(
333: "ivy.resolved.configurations."
334: + resolveId, conf);
335: }
336: getProject().setProperty(
337: "ivy.resolved.file." + resolveId,
338: file.getAbsolutePath());
339: settings.setVariable("ivy.resolved.file."
340: + resolveId, file.getAbsolutePath());
341: }
342: }
343: } catch (MalformedURLException e) {
344: throw new BuildException(
345: "unable to convert given ivy file to url: " + file
346: + ": " + e, e);
347: } catch (ParseException e) {
348: log(e.getMessage(), Project.MSG_ERR);
349: throw new BuildException("syntax errors in ivy file: " + e,
350: e);
351: } catch (ResolveProcessException e) {
352: throw new BuildException(
353: "impossible to resolve dependencies:\n\t"
354: + e.getMessage(), e);
355: } catch (Exception e) {
356: throw new BuildException(
357: "impossible to resolve dependencies:\n\t" + e, e);
358: }
359: }
360:
361: protected Collection/*<String>*/getAllowedLogOptions() {
362: return Arrays.asList(new String[] { LogOptions.LOG_DEFAULT,
363: LogOptions.LOG_DOWNLOAD_ONLY, LogOptions.LOG_QUIET });
364: }
365:
366: private ResolveOptions getResolveOptions(Ivy ivy, String[] confs,
367: IvySettings settings) {
368: if (useOrigin) {
369: settings.useDeprecatedUseOrigin();
370: }
371: return ((ResolveOptions) new ResolveOptions().setLog(log))
372: .setConfs(confs).setValidate(doValidate(settings))
373: .setArtifactFilter(
374: FilterHelper.getArtifactTypeFilter(type))
375: .setRevision(revision).setDate(
376: getPubDate(pubdate, null)).setUseCacheOnly(
377: useCacheOnly).setRefresh(refresh)
378: .setTransitive(transitive).setResolveId(resolveId);
379: }
380:
381: public String getModule() {
382: return module;
383: }
384:
385: public void setModule(String module) {
386: this .module = module;
387: }
388:
389: public String getOrganisation() {
390: return organisation;
391: }
392:
393: public void setOrganisation(String organisation) {
394: this .organisation = organisation;
395: }
396:
397: public boolean isTransitive() {
398: return transitive;
399: }
400:
401: public void setTransitive(boolean transitive) {
402: this .transitive = transitive;
403: }
404:
405: public boolean isChanging() {
406: return changing;
407: }
408:
409: public void setChanging(boolean changing) {
410: this .changing = changing;
411: }
412:
413: public boolean isKeep() {
414: return keep == null ? organisation == null : keep
415: .booleanValue();
416: }
417:
418: public void setKeep(boolean keep) {
419: this .keep = Boolean.valueOf(keep);
420: }
421:
422: public boolean isInline() {
423: return inline;
424: }
425:
426: public void setInline(boolean inline) {
427: this .inline = inline;
428: }
429:
430: public String getResolveId() {
431: return resolveId;
432: }
433:
434: public void setResolveId(String resolveId) {
435: this.resolveId = resolveId;
436: }
437: }
|