1 | /*
|
---|
2 | * Copyright 2003-2005 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 | package org.apache.tools.ant.launch;
|
---|
18 |
|
---|
19 | import java.net.URL;
|
---|
20 | import java.net.URLClassLoader;
|
---|
21 | import java.net.MalformedURLException;
|
---|
22 | import java.io.File;
|
---|
23 | import java.util.StringTokenizer;
|
---|
24 | import java.util.List;
|
---|
25 | import java.util.ArrayList;
|
---|
26 | import java.util.Iterator;
|
---|
27 |
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * This is a launcher for Ant.
|
---|
31 | *
|
---|
32 | * @since Ant 1.6
|
---|
33 | */
|
---|
34 | public class Launcher {
|
---|
35 | /** The Ant Home property */
|
---|
36 | public static final String ANTHOME_PROPERTY = "ant.home";
|
---|
37 |
|
---|
38 | /** The Ant Library Directory property */
|
---|
39 | public static final String ANTLIBDIR_PROPERTY = "ant.library.dir";
|
---|
40 |
|
---|
41 | /** The directory name of the per-user ant directory */
|
---|
42 | public static final String ANT_PRIVATEDIR = ".ant";
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * The location of a per-user library directory
|
---|
46 | */
|
---|
47 | public static final String ANT_PRIVATELIB = "lib";
|
---|
48 |
|
---|
49 | /** The location of a per-user library directory */
|
---|
50 | public static final String USER_LIBDIR = ANT_PRIVATEDIR + "/" + ANT_PRIVATELIB;
|
---|
51 |
|
---|
52 | /** The startup class that is to be run */
|
---|
53 | public static final String MAIN_CLASS = "org.apache.tools.ant.Main";
|
---|
54 | /**
|
---|
55 | * system property with user home directory
|
---|
56 | */
|
---|
57 | public static final String USER_HOMEDIR = "user.home";
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Entry point for starting command line Ant
|
---|
61 | *
|
---|
62 | * @param args commandline arguments
|
---|
63 | */
|
---|
64 | public static void main(String[] args) {
|
---|
65 | try {
|
---|
66 | Launcher launcher = new Launcher();
|
---|
67 | launcher.run(args);
|
---|
68 | } catch (LaunchException e) {
|
---|
69 | System.err.println(e.getMessage());
|
---|
70 | } catch (Throwable t) {
|
---|
71 | t.printStackTrace();
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Add a CLASSPATH or -lib to lib path urls.
|
---|
77 | * @param path the classpath or lib path to add to the libPathULRLs
|
---|
78 | * @param getJars if true and a path is a directory, add the jars in
|
---|
79 | * the directory to the path urls
|
---|
80 | * @param libPathURLs the list of paths to add to
|
---|
81 | */
|
---|
82 | private void addPath(String path, boolean getJars, List libPathURLs)
|
---|
83 | throws MalformedURLException {
|
---|
84 | StringTokenizer myTokenizer
|
---|
85 | = new StringTokenizer(path, System.getProperty("path.separator"));
|
---|
86 | while (myTokenizer.hasMoreElements()) {
|
---|
87 | String elementName = myTokenizer.nextToken();
|
---|
88 | File element = new File(elementName);
|
---|
89 | if (elementName.indexOf("%") != -1 && !element.exists()) {
|
---|
90 | continue;
|
---|
91 | }
|
---|
92 | if (getJars && element.isDirectory()) {
|
---|
93 | // add any jars in the directory
|
---|
94 | URL[] dirURLs = Locator.getLocationURLs(element);
|
---|
95 | for (int j = 0; j < dirURLs.length; ++j) {
|
---|
96 | libPathURLs.add(dirURLs[j]);
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | libPathURLs.add(element.toURL());
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Run the launcher to launch Ant
|
---|
106 | *
|
---|
107 | * @param args the command line arguments
|
---|
108 | *
|
---|
109 | * @exception MalformedURLException if the URLs required for the classloader
|
---|
110 | * cannot be created.
|
---|
111 | */
|
---|
112 | private void run(String[] args) throws LaunchException, MalformedURLException {
|
---|
113 | String antHomeProperty = System.getProperty(ANTHOME_PROPERTY);
|
---|
114 | File antHome = null;
|
---|
115 |
|
---|
116 | File sourceJar = Locator.getClassSource(getClass());
|
---|
117 | File jarDir = sourceJar.getParentFile();
|
---|
118 |
|
---|
119 | if (antHomeProperty != null) {
|
---|
120 | antHome = new File(antHomeProperty);
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (antHome == null || !antHome.exists()) {
|
---|
124 | antHome = jarDir.getParentFile();
|
---|
125 | System.setProperty(ANTHOME_PROPERTY, antHome.getAbsolutePath());
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (!antHome.exists()) {
|
---|
129 | throw new LaunchException("Ant home is set incorrectly or "
|
---|
130 | + "ant could not be located");
|
---|
131 | }
|
---|
132 |
|
---|
133 | List libPaths = new ArrayList();
|
---|
134 | String cpString = null;
|
---|
135 | List argList = new ArrayList();
|
---|
136 | String[] newArgs;
|
---|
137 | boolean noUserLib = false;
|
---|
138 | boolean noClassPath = false;
|
---|
139 |
|
---|
140 | for (int i = 0; i < args.length; ++i) {
|
---|
141 | if (args[i].equals("-lib")) {
|
---|
142 | if (i == args.length - 1) {
|
---|
143 | throw new LaunchException("The -lib argument must "
|
---|
144 | + "be followed by a library location");
|
---|
145 | }
|
---|
146 | libPaths.add(args[++i]);
|
---|
147 | } else if (args[i].equals("-cp")) {
|
---|
148 | if (i == args.length - 1) {
|
---|
149 | throw new LaunchException("The -cp argument must "
|
---|
150 | + "be followed by a classpath expression");
|
---|
151 | }
|
---|
152 | if (cpString != null) {
|
---|
153 | throw new LaunchException("The -cp argument must "
|
---|
154 | + "not be repeated");
|
---|
155 | }
|
---|
156 | cpString = args[++i];
|
---|
157 | } else if (args[i].equals("--nouserlib") || args[i].equals("-nouserlib")) {
|
---|
158 | noUserLib = true;
|
---|
159 | } else if (args[i].equals("--noclasspath") || args[i].equals("-noclasspath")) {
|
---|
160 | noClassPath = true;
|
---|
161 | } else {
|
---|
162 | argList.add(args[i]);
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | //decide whether to copy the existing arg set, or
|
---|
167 | //build a new one from the list of all args excluding the special
|
---|
168 | //operations that only we handle
|
---|
169 |
|
---|
170 | if (libPaths.size() == 0 && cpString == null) {
|
---|
171 | newArgs = args;
|
---|
172 | } else {
|
---|
173 | newArgs = (String[]) argList.toArray(new String[0]);
|
---|
174 | }
|
---|
175 |
|
---|
176 | List libPathURLs = new ArrayList();
|
---|
177 |
|
---|
178 | if (cpString != null && !noClassPath) {
|
---|
179 | addPath(cpString, false, libPathURLs);
|
---|
180 | }
|
---|
181 |
|
---|
182 | for (Iterator i = libPaths.iterator(); i.hasNext();) {
|
---|
183 | String libPath = (String) i.next();
|
---|
184 | addPath(libPath, true, libPathURLs);
|
---|
185 | }
|
---|
186 |
|
---|
187 | URL[] libJars = (URL[]) libPathURLs.toArray(new URL[0]);
|
---|
188 |
|
---|
189 | // Now try and find JAVA_HOME
|
---|
190 | File toolsJar = Locator.getToolsJar();
|
---|
191 |
|
---|
192 | // determine ant library directory for system jars: use property
|
---|
193 | // or default using location of ant-launcher.jar
|
---|
194 | File antLibDir = null;
|
---|
195 | String antLibDirProperty = System.getProperty(ANTLIBDIR_PROPERTY);
|
---|
196 | if (antLibDirProperty != null) {
|
---|
197 | antLibDir = new File(antLibDirProperty);
|
---|
198 | }
|
---|
199 | if ((antLibDir == null) || !antLibDir.exists()) {
|
---|
200 | antLibDir = jarDir;
|
---|
201 | System.setProperty(ANTLIBDIR_PROPERTY, antLibDir.getAbsolutePath());
|
---|
202 | }
|
---|
203 | URL[] systemJars = Locator.getLocationURLs(antLibDir);
|
---|
204 |
|
---|
205 | File userLibDir
|
---|
206 | = new File(System.getProperty(USER_HOMEDIR),
|
---|
207 | ANT_PRIVATEDIR + File.separatorChar + ANT_PRIVATELIB);
|
---|
208 |
|
---|
209 | URL[] userJars = noUserLib ? new URL[0] : Locator.getLocationURLs(userLibDir);
|
---|
210 |
|
---|
211 | int numJars = libJars.length + userJars.length + systemJars.length;
|
---|
212 | if (toolsJar != null) {
|
---|
213 | numJars++;
|
---|
214 | }
|
---|
215 | URL[] jars = new URL[numJars];
|
---|
216 | System.arraycopy(libJars, 0, jars, 0, libJars.length);
|
---|
217 | System.arraycopy(userJars, 0, jars, libJars.length, userJars.length);
|
---|
218 | System.arraycopy(systemJars, 0, jars, userJars.length + libJars.length,
|
---|
219 | systemJars.length);
|
---|
220 |
|
---|
221 | if (toolsJar != null) {
|
---|
222 | jars[jars.length - 1] = toolsJar.toURL();
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | // now update the class.path property
|
---|
227 | StringBuffer baseClassPath
|
---|
228 | = new StringBuffer(System.getProperty("java.class.path"));
|
---|
229 | if (baseClassPath.charAt(baseClassPath.length() - 1)
|
---|
230 | == File.pathSeparatorChar) {
|
---|
231 | baseClassPath.setLength(baseClassPath.length() - 1);
|
---|
232 | }
|
---|
233 |
|
---|
234 | for (int i = 0; i < jars.length; ++i) {
|
---|
235 | baseClassPath.append(File.pathSeparatorChar);
|
---|
236 | baseClassPath.append(Locator.fromURI(jars[i].toString()));
|
---|
237 | }
|
---|
238 |
|
---|
239 | System.setProperty("java.class.path", baseClassPath.toString());
|
---|
240 |
|
---|
241 | URLClassLoader loader = new URLClassLoader(jars);
|
---|
242 | Thread.currentThread().setContextClassLoader(loader);
|
---|
243 | try {
|
---|
244 | Class mainClass = loader.loadClass(MAIN_CLASS);
|
---|
245 | AntMain main = (AntMain) mainClass.newInstance();
|
---|
246 | main.startAnt(newArgs, null, null);
|
---|
247 | } catch (Throwable t) {
|
---|
248 | t.printStackTrace();
|
---|
249 | }
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|