KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * This file is subject to the licence found in LICENCE.TXT in the root directory of the project.
3  * Copyright Jayasoft 2005 - All rights reserved
4  *
5  * #SNAPSHOT#
6  */

7 package fr.jayasoft.ivy.ant;
8
9 import java.io.File JavaDoc;
10
11 import org.apache.tools.ant.BuildException;
12
13 import fr.jayasoft.ivy.Artifact;
14 import fr.jayasoft.ivy.Ivy;
15 import fr.jayasoft.ivy.ModuleId;
16 import fr.jayasoft.ivy.util.IvyPatternHelper;
17 import fr.jayasoft.ivy.xml.XmlReportParser;
18
19 /**
20  * Set a set of ant properties according to the last artifact resolved
21  *
22  * @author Xavier Hanin
23  */

24 public class IvyArtifactProperty extends IvyTask {
25     private String JavaDoc _conf;
26     private String JavaDoc _name;
27     private String JavaDoc _value;
28     
29     private String JavaDoc _organisation;
30     private String JavaDoc _module;
31     private boolean _haltOnFailure = true;
32     private File JavaDoc _cache;
33
34     public String JavaDoc getConf() {
35         return _conf;
36     }
37     public void setConf(String JavaDoc conf) {
38         _conf = conf;
39     }
40     public String JavaDoc getName() {
41         return _name;
42     }
43     public void setName(String JavaDoc name) {
44         _name = name;
45     }
46     public String JavaDoc getValue() {
47         return _value;
48     }
49     public void setValue(String JavaDoc value) {
50         _value = value;
51     }
52     public String JavaDoc getModule() {
53         return _module;
54     }
55     public void setModule(String JavaDoc module) {
56         _module = module;
57     }
58     public String JavaDoc getOrganisation() {
59         return _organisation;
60     }
61     public void setOrganisation(String JavaDoc organisation) {
62         _organisation = organisation;
63     }
64     public boolean isHaltonfailure() {
65         return _haltOnFailure;
66     }
67     public void setHaltonfailure(boolean haltOnFailure) {
68         _haltOnFailure = haltOnFailure;
69     }
70     public File JavaDoc getCache() {
71         return _cache;
72     }
73     public void setCache(File JavaDoc cache) {
74         _cache = cache;
75     }
76
77     public void execute() throws BuildException {
78         Ivy ivy = getIvyInstance();
79         
80         _organisation = getProperty(_organisation, ivy, "ivy.organisation");
81         _module = getProperty(_module, ivy, "ivy.module");
82
83         ensureResolved(isHaltonfailure(), false, getOrganisation(), getModule());
84         
85         _conf = getProperty(_conf, ivy, "ivy.resolved.configurations");
86         if ("*".equals(_conf)) {
87             _conf = getProperty(ivy, "ivy.resolved.configurations");
88             if (_conf == null) {
89                 throw new BuildException("bad provided for ivy artifactproperty: * can only be used with a prior call to <resolve/>");
90             }
91         }
92         _organisation = getProperty(_organisation, ivy, "ivy.organisation");
93         _module = getProperty(_module, ivy, "ivy.module");
94         if (_cache == null) {
95             _cache = ivy.getDefaultCache();
96         }
97         
98         if (_organisation == null) {
99             throw new BuildException("no organisation provided for ivy artifactproperty: It can either be set explicitely via the attribute 'organisation' or via 'ivy.organisation' property or a prior call to <resolve/>");
100         }
101         if (_module == null) {
102             throw new BuildException("no module name provided for ivy artifactproperty: It can either be set explicitely via the attribute 'module' or via 'ivy.module' property or a prior call to <resolve/>");
103         }
104         if (_conf == null) {
105             throw new BuildException("no conf provided for ivy artifactproperty: It can either be set explicitely via the attribute 'conf' or via 'ivy.resolved.configurations' property or a prior call to <resolve/>");
106         }
107         try {
108             XmlReportParser parser = new XmlReportParser();
109             String JavaDoc[] confs = splitConfs(_conf);
110             for (int i = 0; i < confs.length; i++) {
111                 Artifact[] artifacts = parser.getArtifacts(new ModuleId(_organisation, _module), confs[i], _cache);
112                 for (int j = 0; j < artifacts.length; j++) {
113                     Artifact artifact = artifacts[j];
114                     String JavaDoc name = IvyPatternHelper.substitute(ivy.substitute(getName()), artifact, confs[i]);
115                     String JavaDoc value = IvyPatternHelper.substitute(ivy.substitute(getValue()), artifact, confs[i]);
116                     getProject().setProperty(name, value);
117                 }
118             }
119         } catch (Exception JavaDoc ex) {
120             throw new BuildException("impossible to add artifact properties: "+ex, ex);
121         }
122     }
123 }
124
Popular Tags