1. /*
  2. * Copyright 2003-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. package org.apache.tools.ant.launch;
  18. import java.net.URL;
  19. import java.net.URLClassLoader;
  20. import java.net.MalformedURLException;
  21. import java.io.File;
  22. import java.util.StringTokenizer;
  23. import java.util.List;
  24. import java.util.ArrayList;
  25. import java.util.Iterator;
  26. /**
  27. * This is a launcher for Ant.
  28. *
  29. * @since Ant 1.6
  30. */
  31. public class Launcher {
  32. /** The Ant Home property */
  33. public static final String ANTHOME_PROPERTY = "ant.home";
  34. /** The Ant Library Directory property */
  35. public static final String ANTLIBDIR_PROPERTY = "ant.library.dir";
  36. /** The location of a per-user library directory */
  37. public static final String USER_LIBDIR = ".ant/lib";
  38. /** The startup class that is to be run */
  39. public static final String MAIN_CLASS = "org.apache.tools.ant.Main";
  40. /**
  41. * Entry point for starting command line Ant
  42. *
  43. * @param args commandline arguments
  44. */
  45. public static void main(String[] args) {
  46. try {
  47. Launcher launcher = new Launcher();
  48. launcher.run(args);
  49. } catch (LaunchException e) {
  50. System.err.println(e.getMessage());
  51. } catch (Throwable t) {
  52. t.printStackTrace();
  53. }
  54. }
  55. /**
  56. * Run the launcher to launch Ant
  57. *
  58. * @param args the command line arguments
  59. *
  60. * @exception MalformedURLException if the URLs required for the classloader
  61. * cannot be created.
  62. */
  63. private void run(String[] args) throws LaunchException, MalformedURLException {
  64. String antHomeProperty = System.getProperty(ANTHOME_PROPERTY);
  65. File antHome = null;
  66. File sourceJar = Locator.getClassSource(getClass());
  67. File jarDir = sourceJar.getParentFile();
  68. if (antHomeProperty != null) {
  69. antHome = new File(antHomeProperty);
  70. }
  71. if (antHome == null || !antHome.exists()) {
  72. antHome = jarDir.getParentFile();
  73. System.setProperty(ANTHOME_PROPERTY, antHome.getAbsolutePath());
  74. }
  75. if (!antHome.exists()) {
  76. throw new LaunchException("Ant home is set incorrectly or "
  77. + "ant could not be located");
  78. }
  79. List libPaths = new ArrayList();
  80. List argList = new ArrayList();
  81. String[] newArgs;
  82. for (int i = 0; i < args.length; ++i) {
  83. if (args[i].equals("-lib")) {
  84. if (i == args.length - 1) {
  85. throw new LaunchException("The -lib argument must "
  86. + "be followed by a library location");
  87. }
  88. libPaths.add(args[++i]);
  89. } else {
  90. argList.add(args[i]);
  91. }
  92. }
  93. if (libPaths.size() == 0) {
  94. newArgs = args;
  95. } else {
  96. newArgs = (String[]) argList.toArray(new String[0]);
  97. }
  98. List libPathURLs = new ArrayList();
  99. for (Iterator i = libPaths.iterator(); i.hasNext();) {
  100. String libPath = (String) i.next();
  101. StringTokenizer myTokenizer
  102. = new StringTokenizer(libPath, System.getProperty("path.separator"));
  103. while (myTokenizer.hasMoreElements()) {
  104. String elementName = myTokenizer.nextToken();
  105. File element = new File(elementName);
  106. if (elementName.indexOf("%") != -1 && !element.exists()) {
  107. continue;
  108. }
  109. if (element.isDirectory()) {
  110. // add any jars in the directory
  111. URL[] dirURLs = Locator.getLocationURLs(element);
  112. for (int j = 0; j < dirURLs.length; ++j) {
  113. libPathURLs.add(dirURLs[j]);
  114. }
  115. }
  116. libPathURLs.add(element.toURL());
  117. }
  118. }
  119. URL[] libJars = (URL[]) libPathURLs.toArray(new URL[0]);
  120. // Now try and find JAVA_HOME
  121. File toolsJar = Locator.getToolsJar();
  122. // determine ant library directory for system jars: use property
  123. // or default using location of ant-launcher.jar
  124. File antLibDir = null;
  125. String antLibDirProperty = System.getProperty(ANTLIBDIR_PROPERTY);
  126. if (antLibDirProperty != null) {
  127. antLibDir = new File(antLibDirProperty);
  128. }
  129. if ((antLibDir == null) || !antLibDir.exists()) {
  130. antLibDir = jarDir;
  131. System.setProperty(ANTLIBDIR_PROPERTY, antLibDir.getAbsolutePath());
  132. }
  133. URL[] systemJars = Locator.getLocationURLs(antLibDir);
  134. File userLibDir
  135. = new File(System.getProperty("user.home"), USER_LIBDIR);
  136. URL[] userJars = Locator.getLocationURLs(userLibDir);
  137. int numJars = libJars.length + userJars.length + systemJars.length;
  138. if (toolsJar != null) {
  139. numJars++;
  140. }
  141. URL[] jars = new URL[numJars];
  142. System.arraycopy(libJars, 0, jars, 0, libJars.length);
  143. System.arraycopy(userJars, 0, jars, libJars.length, userJars.length);
  144. System.arraycopy(systemJars, 0, jars, userJars.length + libJars.length,
  145. systemJars.length);
  146. if (toolsJar != null) {
  147. jars[jars.length - 1] = toolsJar.toURL();
  148. }
  149. // now update the class.path property
  150. StringBuffer baseClassPath
  151. = new StringBuffer(System.getProperty("java.class.path"));
  152. if (baseClassPath.charAt(baseClassPath.length() - 1)
  153. == File.pathSeparatorChar) {
  154. baseClassPath.setLength(baseClassPath.length() - 1);
  155. }
  156. for (int i = 0; i < jars.length; ++i) {
  157. baseClassPath.append(File.pathSeparatorChar);
  158. baseClassPath.append(Locator.fromURI(jars[i].toString()));
  159. }
  160. System.setProperty("java.class.path", baseClassPath.toString());
  161. URLClassLoader loader = new URLClassLoader(jars);
  162. Thread.currentThread().setContextClassLoader(loader);
  163. try {
  164. Class mainClass = loader.loadClass(MAIN_CLASS);
  165. AntMain main = (AntMain) mainClass.newInstance();
  166. main.startAnt(newArgs, null, null);
  167. } catch (Throwable t) {
  168. t.printStackTrace();
  169. }
  170. }
  171. }