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.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Map.Entry;
028:
029: import org.apache.ivy.Ivy;
030: import org.apache.ivy.core.module.descriptor.Configuration;
031: import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
032: import org.apache.ivy.core.module.descriptor.Configuration.Visibility;
033: import org.apache.ivy.core.module.id.ModuleRevisionId;
034: import org.apache.ivy.core.settings.IvySettings;
035: import org.apache.ivy.plugins.parser.ModuleDescriptorParserRegistry;
036: import org.apache.tools.ant.BuildException;
037: import org.apache.tools.ant.Project;
038:
039: /**
040: * Parses information about an ivy file and make them available in ant.
041: */
042: public class IvyInfo extends IvyTask {
043: private File file = null;
044:
045: public File getFile() {
046: return file;
047: }
048:
049: public void setFile(File file) {
050: this .file = file;
051: }
052:
053: public void doExecute() throws BuildException {
054: Ivy ivy = getIvyInstance();
055: IvySettings settings = ivy.getSettings();
056: if (file == null) {
057: file = getProject().resolveFile(
058: getProperty(settings, "ivy.dep.file"));
059: }
060:
061: try {
062: ModuleDescriptor md = ModuleDescriptorParserRegistry
063: .getInstance().parseDescriptor(settings,
064: file.toURL(), doValidate(settings));
065: ModuleRevisionId mrid = md.getModuleRevisionId();
066: getProject().setProperty("ivy.organisation",
067: mrid.getOrganisation());
068: getProject().setProperty("ivy.module", mrid.getName());
069: if (mrid.getBranch() != null) {
070: getProject()
071: .setProperty("ivy.branch", mrid.getBranch());
072: }
073: getProject()
074: .setProperty("ivy.revision", mrid.getRevision());
075: getProject().setProperty("ivy.status", md.getStatus());
076:
077: Map extra = mrid.getExtraAttributes();
078: for (Iterator iter = extra.entrySet().iterator(); iter
079: .hasNext();) {
080: Entry entry = (Entry) iter.next();
081: getProject().setProperty("ivy.extra." + entry.getKey(),
082: (String) entry.getValue());
083: }
084:
085: getProject().setProperty("ivy.configurations",
086: mergeConfs(md.getConfigurationsNames()));
087:
088: // store the public configurations in a separate property
089: Configuration[] configs = md.getConfigurations();
090: List publicConfigsList = new ArrayList();
091: for (int i = 0; i < configs.length; i++) {
092: if (Visibility.PUBLIC
093: .equals(configs[i].getVisibility())) {
094: publicConfigsList.add(configs[i].getName());
095: }
096: }
097: String[] publicConfigs = (String[]) publicConfigsList
098: .toArray(new String[publicConfigsList.size()]);
099: getProject().setProperty("ivy.public.configurations",
100: mergeConfs(publicConfigs));
101: } catch (MalformedURLException e) {
102: throw new BuildException(
103: "unable to convert given ivy file to url: " + file
104: + ": " + e, e);
105: } catch (ParseException e) {
106: log(e.getMessage(), Project.MSG_ERR);
107: throw new BuildException("syntax errors in ivy file: " + e,
108: e);
109: } catch (Exception e) {
110: throw new BuildException(
111: "impossible to resolve dependencies: " + e, e);
112: }
113: }
114: }
|