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:
022: import org.apache.ivy.core.IvyPatternHelper;
023: import org.apache.ivy.core.cache.ResolutionCacheManager;
024: import org.apache.ivy.core.module.descriptor.Artifact;
025: import org.apache.ivy.core.resolve.ResolveOptions;
026: import org.apache.ivy.plugins.report.XmlReportParser;
027: import org.apache.tools.ant.BuildException;
028:
029: /**
030: * Set a set of ant properties according to the last artifact resolved
031: */
032: public class IvyArtifactProperty extends IvyPostResolveTask {
033: private String name;
034:
035: private String value;
036: private boolean overwrite = false;
037:
038: public String getName() {
039: return this .name;
040: }
041:
042: public void setName(String name) {
043: this .name = name;
044: }
045:
046: public String getValue() {
047: return this .value;
048: }
049:
050: public void setValue(String value) {
051: this .value = value;
052: }
053:
054: public void setOverwrite(boolean overwrite) {
055: this .overwrite = overwrite;
056:
057: }
058:
059: public void doExecute() throws BuildException {
060: prepareAndCheck();
061:
062: try {
063: ResolutionCacheManager cacheMgr = getIvyInstance()
064: .getResolutionCacheManager();
065: String[] confs = splitConfs(getConf());
066: String resolveId = getResolveId();
067: if (resolveId == null) {
068: resolveId = ResolveOptions
069: .getDefaultResolveId(getResolvedModuleId());
070: }
071: XmlReportParser parser = new XmlReportParser();
072: for (int i = 0; i < confs.length; i++) {
073: File report = cacheMgr
074: .getConfigurationResolveReportInCache(
075: resolveId, confs[i]);
076: parser.parse(report);
077:
078: Artifact[] artifacts = parser.getArtifacts();
079: for (int j = 0; j < artifacts.length; j++) {
080: Artifact artifact = artifacts[j];
081: String name = IvyPatternHelper.substitute(
082: getSettings().substitute(getName()),
083: artifact, confs[i]);
084: String value = IvyPatternHelper.substitute(
085: getSettings().substitute(getValue()),
086: artifact, confs[i]);
087: setProperty(name, value);
088: }
089: }
090: } catch (Exception ex) {
091: throw new BuildException(
092: "impossible to add artifact properties: " + ex, ex);
093: }
094: }
095:
096: private void setProperty(String name, String value) {
097: if (overwrite) {
098: getProject().setProperty(name, value);
099: } else {
100: getProject().setNewProperty(name, value);
101: }
102: }
103: }
|