1 | /*
|
---|
2 | * Copyright 2000,2002,2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 | import java.io.BufferedReader;
|
---|
21 | import java.io.File;
|
---|
22 | import java.io.FileWriter;
|
---|
23 | import java.io.IOException;
|
---|
24 | import java.io.InputStream;
|
---|
25 | import java.io.InputStreamReader;
|
---|
26 | import java.io.PrintWriter;
|
---|
27 | import org.apache.tools.ant.BuildException;
|
---|
28 | import org.apache.tools.ant.Project;
|
---|
29 | import org.apache.tools.ant.Task;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Executes a given command if the os platform is appropriate.
|
---|
33 | *
|
---|
34 | * <p><strong>As of Ant 1.2, this class is no longer the
|
---|
35 | * implementation of Ant's <exec> task - it is considered to be
|
---|
36 | * dead code by the Ant developers and is unmaintained. Don't use
|
---|
37 | * it.</strong></p>
|
---|
38 |
|
---|
39 | *
|
---|
40 | * @deprecated delegate to {@link org.apache.tools.ant.taskdefs.Execute Execute}
|
---|
41 | * instead.
|
---|
42 | */
|
---|
43 | public class Exec extends Task {
|
---|
44 | private String os;
|
---|
45 | private String out;
|
---|
46 | private File dir;
|
---|
47 | private String command;
|
---|
48 | protected PrintWriter fos = null;
|
---|
49 | private boolean failOnError = false;
|
---|
50 |
|
---|
51 | public Exec() {
|
---|
52 | System.err.println("As of Ant 1.2 released in October 2000, "
|
---|
53 | + "the Exec class");
|
---|
54 | System.err.println("is considered to be dead code by the Ant "
|
---|
55 | + "developers and is unmaintained.");
|
---|
56 | System.err.println("Don\'t use it!");
|
---|
57 | }
|
---|
58 |
|
---|
59 | public void execute() throws BuildException {
|
---|
60 | run(command);
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected int run(String command) throws BuildException {
|
---|
64 |
|
---|
65 | int err = -1; // assume the worst
|
---|
66 |
|
---|
67 | // test if os match
|
---|
68 | String myos = System.getProperty("os.name");
|
---|
69 | log("Myos = " + myos, Project.MSG_VERBOSE);
|
---|
70 | if ((os != null) && (os.indexOf(myos) < 0)) {
|
---|
71 | // this command will be executed only on the specified OS
|
---|
72 | log("Not found in " + os, Project.MSG_VERBOSE);
|
---|
73 | return 0;
|
---|
74 | }
|
---|
75 |
|
---|
76 | // default directory to the project's base directory
|
---|
77 | if (dir == null) {
|
---|
78 | dir = getProject().getBaseDir();
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (myos.toLowerCase().indexOf("windows") >= 0) {
|
---|
82 | if (!dir.equals(getProject().resolveFile("."))) {
|
---|
83 | if (myos.toLowerCase().indexOf("nt") >= 0) {
|
---|
84 | command = "cmd /c cd " + dir + " && " + command;
|
---|
85 | } else {
|
---|
86 | String ant = getProject().getProperty("ant.home");
|
---|
87 | if (ant == null) {
|
---|
88 | throw new BuildException("Property 'ant.home' not "
|
---|
89 | + "found", getLocation());
|
---|
90 | }
|
---|
91 |
|
---|
92 | String antRun = getProject().resolveFile(ant + "/bin/antRun.bat").toString();
|
---|
93 | command = antRun + " " + dir + " " + command;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | } else {
|
---|
97 | String ant = getProject().getProperty("ant.home");
|
---|
98 | if (ant == null) {
|
---|
99 | throw new BuildException("Property 'ant.home' not found",
|
---|
100 | getLocation());
|
---|
101 | }
|
---|
102 | String antRun = getProject().resolveFile(ant + "/bin/antRun").toString();
|
---|
103 |
|
---|
104 | command = antRun + " " + dir + " " + command;
|
---|
105 | }
|
---|
106 |
|
---|
107 | try {
|
---|
108 | // show the command
|
---|
109 | log(command, Project.MSG_VERBOSE);
|
---|
110 |
|
---|
111 | // exec command on system runtime
|
---|
112 | Process proc = Runtime.getRuntime().exec(command);
|
---|
113 |
|
---|
114 | if (out != null) {
|
---|
115 | fos = new PrintWriter(new FileWriter(out));
|
---|
116 | log("Output redirected to " + out, Project.MSG_VERBOSE);
|
---|
117 | }
|
---|
118 |
|
---|
119 | // copy input and error to the output stream
|
---|
120 | StreamPumper inputPumper =
|
---|
121 | new StreamPumper(proc.getInputStream(), Project.MSG_INFO);
|
---|
122 | StreamPumper errorPumper =
|
---|
123 | new StreamPumper(proc.getErrorStream(), Project.MSG_WARN);
|
---|
124 |
|
---|
125 | // starts pumping away the generated output/error
|
---|
126 | inputPumper.start();
|
---|
127 | errorPumper.start();
|
---|
128 |
|
---|
129 | // Wait for everything to finish
|
---|
130 | proc.waitFor();
|
---|
131 | inputPumper.join();
|
---|
132 | errorPumper.join();
|
---|
133 | proc.destroy();
|
---|
134 |
|
---|
135 | // close the output file if required
|
---|
136 | logFlush();
|
---|
137 |
|
---|
138 | // check its exit value
|
---|
139 | err = proc.exitValue();
|
---|
140 | if (err != 0) {
|
---|
141 | if (failOnError) {
|
---|
142 | throw new BuildException("Exec returned: " + err, getLocation());
|
---|
143 | } else {
|
---|
144 | log("Result: " + err, Project.MSG_ERR);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | } catch (IOException ioe) {
|
---|
148 | throw new BuildException("Error exec: " + command, ioe, getLocation());
|
---|
149 | } catch (InterruptedException ex) {
|
---|
150 | //ignore
|
---|
151 | }
|
---|
152 |
|
---|
153 | return err;
|
---|
154 | }
|
---|
155 |
|
---|
156 | public void setDir(String d) {
|
---|
157 | this.dir = getProject().resolveFile(d);
|
---|
158 | }
|
---|
159 |
|
---|
160 | public void setOs(String os) {
|
---|
161 | this.os = os;
|
---|
162 | }
|
---|
163 |
|
---|
164 | public void setCommand(String command) {
|
---|
165 | this.command = command;
|
---|
166 | }
|
---|
167 |
|
---|
168 | public void setOutput(String out) {
|
---|
169 | this.out = out;
|
---|
170 | }
|
---|
171 |
|
---|
172 | public void setFailonerror(boolean fail) {
|
---|
173 | failOnError = fail;
|
---|
174 | }
|
---|
175 |
|
---|
176 | protected void outputLog(String line, int messageLevel) {
|
---|
177 | if (fos == null) {
|
---|
178 | log(line, messageLevel);
|
---|
179 | } else {
|
---|
180 | fos.println(line);
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | protected void logFlush() {
|
---|
185 | if (fos != null) {
|
---|
186 | fos.close();
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | // Inner class for continually pumping the input stream during
|
---|
191 | // Process's runtime.
|
---|
192 | class StreamPumper extends Thread {
|
---|
193 | private BufferedReader din;
|
---|
194 | private int messageLevel;
|
---|
195 | private boolean endOfStream = false;
|
---|
196 | private int SLEEP_TIME = 5;
|
---|
197 |
|
---|
198 | public StreamPumper(InputStream is, int messageLevel) {
|
---|
199 | this.din = new BufferedReader(new InputStreamReader(is));
|
---|
200 | this.messageLevel = messageLevel;
|
---|
201 | }
|
---|
202 |
|
---|
203 | public void pumpStream() throws IOException {
|
---|
204 | if (!endOfStream) {
|
---|
205 | String line = din.readLine();
|
---|
206 |
|
---|
207 | if (line != null) {
|
---|
208 | outputLog(line, messageLevel);
|
---|
209 | } else {
|
---|
210 | endOfStream = true;
|
---|
211 | }
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | public void run() {
|
---|
216 | try {
|
---|
217 | try {
|
---|
218 | while (!endOfStream) {
|
---|
219 | pumpStream();
|
---|
220 | sleep(SLEEP_TIME);
|
---|
221 | }
|
---|
222 | } catch (InterruptedException ie) {
|
---|
223 | //ignore
|
---|
224 | }
|
---|
225 | din.close();
|
---|
226 | } catch (IOException ioe) {
|
---|
227 | // ignore
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|