KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > tasks > Ant


1 package hudson.tasks;
2
3 import hudson.CopyOnWrite;
4 import hudson.Launcher;
5 import hudson.Util;
6 import hudson.model.Build;
7 import hudson.model.BuildListener;
8 import hudson.model.Descriptor;
9 import hudson.model.Project;
10 import hudson.util.FormFieldValidator;
11 import hudson.util.ArgumentListBuilder;
12 import org.kohsuke.stapler.StaplerRequest;
13 import org.kohsuke.stapler.StaplerResponse;
14
15 import javax.servlet.ServletException JavaDoc;
16 import java.io.File JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.util.Map JavaDoc;
19
20 /**
21  * @author Kohsuke Kawaguchi
22  */

23 public class Ant extends Builder {
24     /**
25      * The targets, properties, and other Ant options.
26      * Either separated by whitespace or newline.
27      */

28     private final String JavaDoc targets;
29
30     /**
31      * Identifies {@link AntInstallation} to be used.
32      */

33     private final String JavaDoc antName;
34
35     public Ant(String JavaDoc targets,String JavaDoc antName) {
36         this.targets = targets;
37         this.antName = antName;
38     }
39
40     public String JavaDoc getTargets() {
41         return targets;
42     }
43
44     /**
45      * Gets the Ant to invoke,
46      * or null to invoke the default one.
47      */

48     public AntInstallation getAnt() {
49         for( AntInstallation i : DESCRIPTOR.getInstallations() ) {
50             if(antName!=null && i.getName().equals(antName))
51                 return i;
52         }
53         return null;
54     }
55
56     public boolean perform(Build build, Launcher launcher, BuildListener listener) {
57         Project proj = build.getProject();
58
59         ArgumentListBuilder args = new ArgumentListBuilder();
60
61         String JavaDoc execName;
62         if(launcher.isUnix())
63             execName = "ant";
64         else
65             execName = "ant.bat";
66
67         String JavaDoc normalizedTarget = targets.replaceAll("[\t\r\n]+"," ");
68
69         AntInstallation ai = getAnt();
70         if(ai==null) {
71             args.add(execName);
72         } else {
73             File JavaDoc exec = ai.getExecutable();
74             if(!ai.getExists()) {
75                 listener.fatalError(exec+" doesn't exist");
76                 return false;
77             }
78             args.add(exec.getPath());
79         }
80         args.addTokenized(normalizedTarget);
81
82         Map JavaDoc<String JavaDoc,String JavaDoc> env = build.getEnvVars();
83         if(ai!=null)
84             env.put("ANT_HOME",ai.getAntHome());
85
86         if(!launcher.isUnix()) {
87             // on Windows, executing batch file can't return the correct error code,
88
// so we need to wrap it into cmd.exe.
89
// double %% is needed because we want ERRORLEVEL to be expanded after
90
// batch file executed, not before. This alone shows how broken Windows is...
91
args.prepend("cmd.exe","/C");
92             args.add("&&","exit","%%ERRORLEVEL%%");
93         }
94
95         try {
96             int r = launcher.launch(args.toCommandArray(),env,listener.getLogger(),proj.getModuleRoot()).join();
97             return r==0;
98         } catch (IOException JavaDoc e) {
99             Util.displayIOException(e,listener);
100             e.printStackTrace( listener.fatalError("command execution failed") );
101             return false;
102         }
103     }
104
105     public Descriptor<Builder> getDescriptor() {
106         return DESCRIPTOR;
107     }
108
109     public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();
110
111     public static final class DescriptorImpl extends Descriptor<Builder> {
112         @CopyOnWrite
113         private volatile AntInstallation[] installations = new AntInstallation[0];
114
115         private DescriptorImpl() {
116             super(Ant.class);
117             load();
118         }
119
120         protected void convert(Map JavaDoc<String JavaDoc,Object JavaDoc> oldPropertyBag) {
121             if(oldPropertyBag.containsKey("installations"))
122                 installations = (AntInstallation[]) oldPropertyBag.get("installations");
123         }
124
125         public String JavaDoc getHelpFile() {
126             return "/help/project-config/ant.html";
127         }
128
129         public String JavaDoc getDisplayName() {
130             return "Invoke top-level Ant targets";
131         }
132
133         public AntInstallation[] getInstallations() {
134             return installations;
135         }
136
137         public boolean configure(StaplerRequest req) {
138             boolean r = true;
139
140             int i;
141             String JavaDoc[] names = req.getParameterValues("ant_name");
142             String JavaDoc[] homes = req.getParameterValues("ant_home");
143             int len;
144             if(names!=null && homes!=null)
145                 len = Math.min(names.length,homes.length);
146             else
147                 len = 0;
148             AntInstallation[] insts = new AntInstallation[len];
149
150             for( i=0; i<len; i++ ) {
151                 if(names[i].length()==0 || homes[i].length()==0) continue;
152                 insts[i] = new AntInstallation(names[i],homes[i]);
153             }
154
155             this.installations = insts;
156
157             save();
158
159             return r;
160         }
161
162         public Builder newInstance(StaplerRequest req) {
163             return new Ant(req.getParameter("ant_targets"),req.getParameter("ant_version"));
164         }
165
166     //
167
// web methods
168
//
169
/**
170          * Checks if the ANT_HOME is valid.
171          */

172         public void doCheckAntHome( StaplerRequest req, StaplerResponse rsp ) throws IOException JavaDoc, ServletException JavaDoc {
173             // this can be used to check the existence of a file on the server, so needs to be protected
174
new FormFieldValidator(req,rsp,true) {
175                 public void check() throws IOException JavaDoc, ServletException JavaDoc {
176                     File JavaDoc f = getFileParameter("value");
177                     if(!f.isDirectory()) {
178                         error(f+" is not a directory");
179                         return;
180                     }
181
182                     File JavaDoc antJar = new File JavaDoc(f,"lib/ant.jar");
183                     if(!antJar.exists()) {
184                         error(f+" doesn't look like an Ant directory");
185                         return;
186                     }
187
188                     ok();
189                 }
190             }.process();
191         }
192     }
193
194     public static final class AntInstallation {
195         private final String JavaDoc name;
196         private final String JavaDoc antHome;
197
198         public AntInstallation(String JavaDoc name, String JavaDoc antHome) {
199             this.name = name;
200             this.antHome = antHome;
201         }
202
203         /**
204          * install directory.
205          */

206         public String JavaDoc getAntHome() {
207             return antHome;
208         }
209
210         /**
211          * Human readable display name.
212          */

213         public String JavaDoc getName() {
214             return name;
215         }
216
217         public File JavaDoc getExecutable() {
218             String JavaDoc execName;
219             if(File.separatorChar=='\\')
220                 execName = "ant.bat";
221             else
222                 execName = "ant";
223
224             return new File JavaDoc(getAntHome(),"bin/"+execName);
225         }
226
227         /**
228          * Returns true if the executable exists.
229          */

230         public boolean getExists() {
231             return getExecutable().exists();
232         }
233     }
234 }
235
Popular Tags