You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Javac.java 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import org.apache.tools.ant.BuildException;
  56. import org.apache.tools.ant.DirectoryScanner;
  57. import org.apache.tools.ant.Project;
  58. import org.apache.tools.ant.types.*;
  59. import org.apache.tools.ant.util.*;
  60. import org.apache.tools.ant.taskdefs.compilers.*;
  61. import java.io.File;
  62. /**
  63. * Task to compile Java source files. This task can take the following
  64. * arguments:
  65. * <ul>
  66. * <li>sourcedir
  67. * <li>destdir
  68. * <li>deprecation
  69. * <li>classpath
  70. * <li>bootclasspath
  71. * <li>extdirs
  72. * <li>optimize
  73. * <li>debug
  74. * <li>encoding
  75. * <li>target
  76. * <li>depend
  77. * <li>vebose
  78. * <li>failonerror
  79. * <li>includeantruntime
  80. * <li>includejavaruntime
  81. * </ul>
  82. * Of these arguments, the <b>sourcedir</b> and <b>destdir</b> are required.
  83. * <p>
  84. * When this task executes, it will recursively scan the sourcedir and
  85. * destdir looking for Java source files to compile. This task makes its
  86. * compile decision based on timestamp.
  87. *
  88. * @author James Davidson <a href="mailto:duncan@x180.com">duncan@x180.com</a>
  89. * @author Robin Green <a href="mailto:greenrd@hotmail.com">greenrd@hotmail.com</a>
  90. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  91. * @author <a href="mailto:jayglanville@home.com">J D Glanville</a>
  92. */
  93. public class Javac extends MatchingTask {
  94. private static final String FAIL_MSG
  95. = "Compile failed, messages should have been provided.";
  96. private Path src;
  97. private File destDir;
  98. private Path compileClasspath;
  99. private String encoding;
  100. private boolean debug = false;
  101. private boolean optimize = false;
  102. private boolean deprecation = false;
  103. private boolean depend = false;
  104. private boolean verbose = false;
  105. private String target;
  106. private Path bootclasspath;
  107. private Path extdirs;
  108. private boolean includeAntRuntime = true;
  109. private boolean includeJavaRuntime = false;
  110. private boolean fork = false;
  111. protected boolean failOnError = true;
  112. protected File[] compileList = new File[0];
  113. /**
  114. * Create a nested <src ...> element for multiple source path
  115. * support.
  116. *
  117. * @return a nexted src element.
  118. */
  119. public Path createSrc() {
  120. if (src == null) {
  121. src = new Path(project);
  122. }
  123. return src.createPath();
  124. }
  125. /**
  126. * Set the source dirs to find the source Java files.
  127. */
  128. public void setSrcdir(Path srcDir) {
  129. if (src == null) {
  130. src = srcDir;
  131. } else {
  132. src.append(srcDir);
  133. }
  134. }
  135. /** Gets the source dirs to find the source java files. */
  136. public Path getSrcdir() {
  137. return src;
  138. }
  139. /**
  140. * Set the destination directory into which the Java source
  141. * files should be compiled.
  142. */
  143. public void setDestdir(File destDir) {
  144. this.destDir = destDir;
  145. }
  146. /**
  147. * Gets the destination directory into which the java source files
  148. * should be compiled.
  149. */
  150. public File getDestdir() {
  151. return destDir;
  152. }
  153. /**
  154. * Set the classpath to be used for this compilation.
  155. */
  156. public void setClasspath(Path classpath) {
  157. if (compileClasspath == null) {
  158. compileClasspath = classpath;
  159. } else {
  160. compileClasspath.append(classpath);
  161. }
  162. }
  163. /** Gets the classpath to be used for this compilation. */
  164. public Path getClasspath() {
  165. return compileClasspath;
  166. }
  167. /**
  168. * Maybe creates a nested classpath element.
  169. */
  170. public Path createClasspath() {
  171. if (compileClasspath == null) {
  172. compileClasspath = new Path(project);
  173. }
  174. return compileClasspath.createPath();
  175. }
  176. /**
  177. * Adds a reference to a CLASSPATH defined elsewhere.
  178. */
  179. public void setClasspathRef(Reference r) {
  180. createClasspath().setRefid(r);
  181. }
  182. /**
  183. * Sets the bootclasspath that will be used to compile the classes
  184. * against.
  185. */
  186. public void setBootclasspath(Path bootclasspath) {
  187. if (this.bootclasspath == null) {
  188. this.bootclasspath = bootclasspath;
  189. } else {
  190. this.bootclasspath.append(bootclasspath);
  191. }
  192. }
  193. /**
  194. * Gets the bootclasspath that will be used to compile the classes
  195. * against.
  196. */
  197. public Path getBootclasspath() {
  198. return bootclasspath;
  199. }
  200. /**
  201. * Maybe creates a nested classpath element.
  202. */
  203. public Path createBootclasspath() {
  204. if (bootclasspath == null) {
  205. bootclasspath = new Path(project);
  206. }
  207. return bootclasspath.createPath();
  208. }
  209. /**
  210. * Adds a reference to a CLASSPATH defined elsewhere.
  211. */
  212. public void setBootClasspathRef(Reference r) {
  213. createBootclasspath().setRefid(r);
  214. }
  215. /**
  216. * Sets the extension directories that will be used during the
  217. * compilation.
  218. */
  219. public void setExtdirs(Path extdirs) {
  220. if (this.extdirs == null) {
  221. this.extdirs = extdirs;
  222. } else {
  223. this.extdirs.append(extdirs);
  224. }
  225. }
  226. /**
  227. * Gets the extension directories that will be used during the
  228. * compilation.
  229. */
  230. public Path getExtdirs() {
  231. return extdirs;
  232. }
  233. /**
  234. * Maybe creates a nested classpath element.
  235. */
  236. public Path createExtdirs() {
  237. if (extdirs == null) {
  238. extdirs = new Path(project);
  239. }
  240. return extdirs.createPath();
  241. }
  242. /**
  243. * Throw a BuildException if compilation fails
  244. */
  245. public void setFailonerror(boolean fail) {
  246. failOnError = fail;
  247. }
  248. /**
  249. * Proceed if compilation fails
  250. */
  251. public void setProceed(boolean proceed) {
  252. failOnError = !proceed;
  253. }
  254. /**
  255. * Gets the failonerror flag.
  256. */
  257. public boolean getFailonerror() {
  258. return failOnError;
  259. }
  260. /**
  261. * Set the deprecation flag.
  262. */
  263. public void setDeprecation(boolean deprecation) {
  264. this.deprecation = deprecation;
  265. }
  266. /** Gets the deprecation flag. */
  267. public boolean getDeprecation() {
  268. return deprecation;
  269. }
  270. /**
  271. * Set the Java source file encoding name.
  272. */
  273. public void setEncoding(String encoding) {
  274. this.encoding = encoding;
  275. }
  276. /** Gets the java source file encoding name. */
  277. public String getEncoding() {
  278. return encoding;
  279. }
  280. /**
  281. * Set the debug flag.
  282. */
  283. public void setDebug(boolean debug) {
  284. this.debug = debug;
  285. }
  286. /** Gets the debug flag. */
  287. public boolean getDebug() {
  288. return debug;
  289. }
  290. /**
  291. * Set the optimize flag.
  292. */
  293. public void setOptimize(boolean optimize) {
  294. this.optimize = optimize;
  295. }
  296. /** Gets the optimize flag. */
  297. public boolean getOptimize() {
  298. return optimize;
  299. }
  300. /**
  301. * Set the depend flag.
  302. */
  303. public void setDepend(boolean depend) {
  304. this.depend = depend;
  305. }
  306. /** Gets the depend flag. */
  307. public boolean getDepend() {
  308. return depend;
  309. }
  310. /**
  311. * Set the verbose flag.
  312. */
  313. public void setVerbose(boolean verbose) {
  314. this.verbose = verbose;
  315. }
  316. /** Gets the verbose flag. */
  317. public boolean getVerbose() {
  318. return verbose;
  319. }
  320. /**
  321. * Sets the target VM that the classes will be compiled for. Valid
  322. * strings are "1.1", "1.2", and "1.3".
  323. */
  324. public void setTarget(String target) {
  325. this.target = target;
  326. }
  327. /** Gets the target VM that the classes will be compiled for. */
  328. public String getTarget() {
  329. return target;
  330. }
  331. /**
  332. * Include ant's own classpath in this task's classpath?
  333. */
  334. public void setIncludeantruntime( boolean include ) {
  335. includeAntRuntime = include;
  336. }
  337. /**
  338. * Gets whether or not the ant classpath is to be included in the
  339. * task's classpath.
  340. */
  341. public boolean getIncludeantruntime() {
  342. return includeAntRuntime;
  343. }
  344. /**
  345. * Sets whether or not to include the java runtime libraries to this
  346. * task's classpath.
  347. */
  348. public void setIncludejavaruntime( boolean include ) {
  349. includeJavaRuntime = include;
  350. }
  351. /**
  352. * Gets whether or not the java runtime should be included in this
  353. * task's classpath.
  354. */
  355. public boolean getIncludejavaruntime() {
  356. return includeJavaRuntime;
  357. }
  358. /**
  359. * Sets whether to fork the javac compiler.
  360. */
  361. public void setFork(boolean fork)
  362. {
  363. this.fork = fork;
  364. }
  365. /**
  366. * Executes the task.
  367. */
  368. public void execute() throws BuildException {
  369. // first off, make sure that we've got a srcdir
  370. if (src == null) {
  371. throw new BuildException("srcdir attribute must be set!", location);
  372. }
  373. String [] list = src.list();
  374. if (list.length == 0) {
  375. throw new BuildException("srcdir attribute must be set!", location);
  376. }
  377. if (destDir != null && !destDir.isDirectory()) {
  378. throw new BuildException("destination directory \"" + destDir + "\" does not exist or is not a directory", location);
  379. }
  380. // scan source directories and dest directory to build up
  381. // compile lists
  382. resetFileLists();
  383. for (int i=0; i<list.length; i++) {
  384. File srcDir = (File)project.resolveFile(list[i]);
  385. if (!srcDir.exists()) {
  386. throw new BuildException("srcdir \"" + srcDir.getPath() + "\" does not exist!", location);
  387. }
  388. DirectoryScanner ds = this.getDirectoryScanner(srcDir);
  389. String[] files = ds.getIncludedFiles();
  390. scanDir(srcDir, destDir != null ? destDir : srcDir, files);
  391. }
  392. // compile the source files
  393. String compiler = project.getProperty("build.compiler");
  394. if (fork) {
  395. if (compiler != null) {
  396. log("Since fork is true, ignoring build.compiler setting.", Project.MSG_WARN);
  397. }
  398. compiler = "extJavac";
  399. }
  400. if (compiler == null) {
  401. if (Project.getJavaVersion() != Project.JAVA_1_1 &&
  402. Project.getJavaVersion() != Project.JAVA_1_2) {
  403. compiler = "modern";
  404. } else {
  405. compiler = "classic";
  406. }
  407. }
  408. if (compileList.length > 0) {
  409. CompilerAdapter adapter = CompilerAdapterFactory.getCompiler(
  410. compiler, this );
  411. log("Compiling " + compileList.length +
  412. " source file"
  413. + (compileList.length == 1 ? "" : "s")
  414. + (destDir != null ? " to " + destDir : ""));
  415. // now we need to populate the compiler adapter
  416. adapter.setJavac( this );
  417. // finally, lets execute the compiler!!
  418. if (!adapter.execute()) {
  419. if (failOnError) {
  420. throw new BuildException(FAIL_MSG, location);
  421. }
  422. else {
  423. log(FAIL_MSG, Project.MSG_ERR);
  424. }
  425. }
  426. }
  427. }
  428. /**
  429. * Clear the list of files to be compiled and copied..
  430. */
  431. protected void resetFileLists() {
  432. compileList = new File[0];
  433. }
  434. /**
  435. * Scans the directory looking for source files to be compiled.
  436. * The results are returned in the class variable compileList
  437. */
  438. protected void scanDir(File srcDir, File destDir, String files[]) {
  439. GlobPatternMapper m = new GlobPatternMapper();
  440. m.setFrom("*.java");
  441. m.setTo("*.class");
  442. SourceFileScanner sfs = new SourceFileScanner(this);
  443. File[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m);
  444. if (newFiles.length > 0) {
  445. File[] newCompileList = new File[compileList.length +
  446. newFiles.length];
  447. System.arraycopy(compileList, 0, newCompileList, 0,
  448. compileList.length);
  449. System.arraycopy(newFiles, 0, newCompileList,
  450. compileList.length, newFiles.length);
  451. compileList = newCompileList;
  452. }
  453. }
  454. /** Gets the list of files to be compiled. */
  455. public File[] getFileList() {
  456. return compileList;
  457. }
  458. }