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 24 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2002 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.Commandline;
  59. import org.apache.tools.ant.types.Path;
  60. import org.apache.tools.ant.types.Reference;
  61. import org.apache.tools.ant.util.GlobPatternMapper;
  62. import org.apache.tools.ant.util.JavaEnvUtils;
  63. import org.apache.tools.ant.util.SourceFileScanner;
  64. import org.apache.tools.ant.taskdefs.compilers.CompilerAdapter;
  65. import org.apache.tools.ant.taskdefs.compilers.CompilerAdapterFactory;
  66. import java.io.File;
  67. import java.util.Enumeration;
  68. import java.util.Vector;
  69. /**
  70. * Task to compile Java source files. This task can take the following
  71. * arguments:
  72. * <ul>
  73. * <li>sourcedir
  74. * <li>destdir
  75. * <li>deprecation
  76. * <li>classpath
  77. * <li>bootclasspath
  78. * <li>extdirs
  79. * <li>optimize
  80. * <li>debug
  81. * <li>encoding
  82. * <li>target
  83. * <li>depend
  84. * <li>vebose
  85. * <li>failonerror
  86. * <li>includeantruntime
  87. * <li>includejavaruntime
  88. * <li>source
  89. * <li>compiler
  90. * </ul>
  91. * Of these arguments, the <b>sourcedir</b> and <b>destdir</b> are required.
  92. * <p>
  93. * When this task executes, it will recursively scan the sourcedir and
  94. * destdir looking for Java source files to compile. This task makes its
  95. * compile decision based on timestamp.
  96. *
  97. * @author James Davidson <a href="mailto:duncan@x180.com">duncan@x180.com</a>
  98. * @author Robin Green <a href="mailto:greenrd@hotmail.com">greenrd@hotmail.com</a>
  99. * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
  100. * @author <a href="mailto:jayglanville@home.com">J D Glanville</a>
  101. *
  102. * @version $Revision$
  103. *
  104. * @since Ant 1.1
  105. *
  106. * @ant.task category="java"
  107. */
  108. public class Javac extends MatchingTask {
  109. private final static String FAIL_MSG
  110. = "Compile failed; see the compiler error output for details.";
  111. private Path src;
  112. private File destDir;
  113. private Path compileClasspath;
  114. private Path compileSourcepath;
  115. private String encoding;
  116. private boolean debug = false;
  117. private boolean optimize = false;
  118. private boolean deprecation = false;
  119. private boolean depend = false;
  120. private boolean verbose = false;
  121. private String target;
  122. private Path bootclasspath;
  123. private Path extdirs;
  124. private boolean includeAntRuntime = true;
  125. private boolean includeJavaRuntime = false;
  126. private boolean fork = false;
  127. private String forkedExecutable = null;
  128. private boolean nowarn = false;
  129. private String memoryInitialSize;
  130. private String memoryMaximumSize;
  131. private Vector implementationSpecificArgs = new Vector();
  132. protected boolean failOnError = true;
  133. protected boolean listFiles = false;
  134. protected File[] compileList = new File[0];
  135. private String source;
  136. private String debugLevel;
  137. /**
  138. * The compiler set via the compiler attribute.
  139. *
  140. * <p>default is null</p>
  141. *
  142. * @since Ant 1.5
  143. */
  144. private String compiler = null;
  145. /**
  146. * Get the value of debugLevel.
  147. * @return value of debugLevel.
  148. */
  149. public String getDebugLevel() {
  150. return debugLevel;
  151. }
  152. /**
  153. * Set the value of debugLevel.
  154. * @param v Value to assign to debugLevel.
  155. */
  156. public void setDebugLevel(String v) {
  157. this.debugLevel = v;
  158. }
  159. /**
  160. * Get the value of source.
  161. * @return value of source.
  162. */
  163. public String getSource() {
  164. return source;
  165. }
  166. /**
  167. * Set the value of source.
  168. * @param v Value to assign to source.
  169. */
  170. public void setSource(String v) {
  171. this.source = v;
  172. }
  173. /**
  174. * Create a nested src element for multiple source path
  175. * support.
  176. *
  177. * @return a nested src element.
  178. */
  179. public Path createSrc() {
  180. if (src == null) {
  181. src = new Path(project);
  182. }
  183. return src.createPath();
  184. }
  185. /**
  186. * Recreate src
  187. *
  188. * @return a nested src element.
  189. */
  190. protected Path recreateSrc() {
  191. src = null;
  192. return createSrc();
  193. }
  194. /**
  195. * Set the source dirs to find the source Java files.
  196. */
  197. public void setSrcdir(Path srcDir) {
  198. if (src == null) {
  199. src = srcDir;
  200. } else {
  201. src.append(srcDir);
  202. }
  203. }
  204. /** Gets the source dirs to find the source java files. */
  205. public Path getSrcdir() {
  206. return src;
  207. }
  208. /**
  209. * Set the destination directory into which the Java source
  210. * files should be compiled.
  211. */
  212. public void setDestdir(File destDir) {
  213. this.destDir = destDir;
  214. }
  215. /**
  216. * Gets the destination directory into which the java source files
  217. * should be compiled.
  218. */
  219. public File getDestdir() {
  220. return destDir;
  221. }
  222. /**
  223. * Set the sourcepath to be used for this compilation.
  224. */
  225. public void setSourcepath(Path sourcepath) {
  226. if (compileSourcepath == null) {
  227. compileSourcepath = sourcepath;
  228. } else {
  229. compileSourcepath.append(sourcepath);
  230. }
  231. }
  232. /** Gets the sourcepath to be used for this compilation. */
  233. public Path getSourcepath() {
  234. return compileSourcepath;
  235. }
  236. /**
  237. * Maybe creates a nested sourcepath element.
  238. */
  239. public Path createSourcepath() {
  240. if (compileSourcepath == null) {
  241. compileSourcepath = new Path(project);
  242. }
  243. return compileSourcepath.createPath();
  244. }
  245. /**
  246. * Adds a reference to a CLASSPATH defined elsewhere.
  247. */
  248. public void setSourcepathRef(Reference r) {
  249. createSourcepath().setRefid(r);
  250. }
  251. /**
  252. * Set the classpath to be used for this compilation.
  253. */
  254. public void setClasspath(Path classpath) {
  255. if (compileClasspath == null) {
  256. compileClasspath = classpath;
  257. } else {
  258. compileClasspath.append(classpath);
  259. }
  260. }
  261. /** Gets the classpath to be used for this compilation. */
  262. public Path getClasspath() {
  263. return compileClasspath;
  264. }
  265. /**
  266. * Maybe creates a nested classpath element.
  267. */
  268. public Path createClasspath() {
  269. if (compileClasspath == null) {
  270. compileClasspath = new Path(project);
  271. }
  272. return compileClasspath.createPath();
  273. }
  274. /**
  275. * Adds a reference to a CLASSPATH defined elsewhere.
  276. */
  277. public void setClasspathRef(Reference r) {
  278. createClasspath().setRefid(r);
  279. }
  280. /**
  281. * Sets the bootclasspath that will be used to compile the classes
  282. * against.
  283. */
  284. public void setBootclasspath(Path bootclasspath) {
  285. if (this.bootclasspath == null) {
  286. this.bootclasspath = bootclasspath;
  287. } else {
  288. this.bootclasspath.append(bootclasspath);
  289. }
  290. }
  291. /**
  292. * Gets the bootclasspath that will be used to compile the classes
  293. * against.
  294. */
  295. public Path getBootclasspath() {
  296. return bootclasspath;
  297. }
  298. /**
  299. * Maybe creates a nested classpath element.
  300. */
  301. public Path createBootclasspath() {
  302. if (bootclasspath == null) {
  303. bootclasspath = new Path(project);
  304. }
  305. return bootclasspath.createPath();
  306. }
  307. /**
  308. * Adds a reference to a CLASSPATH defined elsewhere.
  309. */
  310. public void setBootClasspathRef(Reference r) {
  311. createBootclasspath().setRefid(r);
  312. }
  313. /**
  314. * Sets the extension directories that will be used during the
  315. * compilation.
  316. */
  317. public void setExtdirs(Path extdirs) {
  318. if (this.extdirs == null) {
  319. this.extdirs = extdirs;
  320. } else {
  321. this.extdirs.append(extdirs);
  322. }
  323. }
  324. /**
  325. * Gets the extension directories that will be used during the
  326. * compilation.
  327. */
  328. public Path getExtdirs() {
  329. return extdirs;
  330. }
  331. /**
  332. * Maybe creates a nested classpath element.
  333. */
  334. public Path createExtdirs() {
  335. if (extdirs == null) {
  336. extdirs = new Path(project);
  337. }
  338. return extdirs.createPath();
  339. }
  340. /**
  341. * List the source files being handed off to the compiler
  342. */
  343. public void setListfiles(boolean list) {
  344. listFiles = list;
  345. }
  346. /** Get the listfiles flag. */
  347. public boolean getListfiles() {
  348. return listFiles;
  349. }
  350. /**
  351. * Throw a BuildException if compilation fails
  352. */
  353. public void setFailonerror(boolean fail) {
  354. failOnError = fail;
  355. }
  356. /**
  357. * Proceed if compilation fails
  358. */
  359. public void setProceed(boolean proceed) {
  360. failOnError = !proceed;
  361. }
  362. /**
  363. * Gets the failonerror flag.
  364. */
  365. public boolean getFailonerror() {
  366. return failOnError;
  367. }
  368. /**
  369. * Set the deprecation flag.
  370. */
  371. public void setDeprecation(boolean deprecation) {
  372. this.deprecation = deprecation;
  373. }
  374. /** Gets the deprecation flag. */
  375. public boolean getDeprecation() {
  376. return deprecation;
  377. }
  378. /**
  379. * Set the memoryInitialSize flag.
  380. */
  381. public void setMemoryInitialSize(String memoryInitialSize) {
  382. this.memoryInitialSize = memoryInitialSize;
  383. }
  384. /** Gets the memoryInitialSize flag. */
  385. public String getMemoryInitialSize() {
  386. return memoryInitialSize;
  387. }
  388. /**
  389. * Set the memoryMaximumSize flag.
  390. */
  391. public void setMemoryMaximumSize(String memoryMaximumSize) {
  392. this.memoryMaximumSize = memoryMaximumSize;
  393. }
  394. /** Gets the memoryMaximumSize flag. */
  395. public String getMemoryMaximumSize() {
  396. return memoryMaximumSize;
  397. }
  398. /**
  399. * Set the Java source file encoding name.
  400. */
  401. public void setEncoding(String encoding) {
  402. this.encoding = encoding;
  403. }
  404. /** Gets the java source file encoding name. */
  405. public String getEncoding() {
  406. return encoding;
  407. }
  408. /**
  409. * Set the debug flag.
  410. */
  411. public void setDebug(boolean debug) {
  412. this.debug = debug;
  413. }
  414. /** Gets the debug flag. */
  415. public boolean getDebug() {
  416. return debug;
  417. }
  418. /**
  419. * Set the optimize flag.
  420. */
  421. public void setOptimize(boolean optimize) {
  422. this.optimize = optimize;
  423. }
  424. /** Gets the optimize flag. */
  425. public boolean getOptimize() {
  426. return optimize;
  427. }
  428. /**
  429. * Set the depend flag.
  430. */
  431. public void setDepend(boolean depend) {
  432. this.depend = depend;
  433. }
  434. /** Gets the depend flag. */
  435. public boolean getDepend() {
  436. return depend;
  437. }
  438. /**
  439. * Set the verbose flag.
  440. */
  441. public void setVerbose(boolean verbose) {
  442. this.verbose = verbose;
  443. }
  444. /** Gets the verbose flag. */
  445. public boolean getVerbose() {
  446. return verbose;
  447. }
  448. /**
  449. * Sets the target VM that the classes will be compiled for. Valid
  450. * strings are "1.1", "1.2", and "1.3".
  451. */
  452. public void setTarget(String target) {
  453. this.target = target;
  454. }
  455. /** Gets the target VM that the classes will be compiled for. */
  456. public String getTarget() {
  457. return target;
  458. }
  459. /**
  460. * Include ant's own classpath in this task's classpath?
  461. */
  462. public void setIncludeantruntime(boolean include) {
  463. includeAntRuntime = include;
  464. }
  465. /**
  466. * Gets whether or not the ant classpath is to be included in the
  467. * task's classpath.
  468. */
  469. public boolean getIncludeantruntime() {
  470. return includeAntRuntime;
  471. }
  472. /**
  473. * Sets whether or not to include the java runtime libraries to this
  474. * task's classpath.
  475. */
  476. public void setIncludejavaruntime(boolean include) {
  477. includeJavaRuntime = include;
  478. }
  479. /**
  480. * Gets whether or not the java runtime should be included in this
  481. * task's classpath.
  482. */
  483. public boolean getIncludejavaruntime() {
  484. return includeJavaRuntime;
  485. }
  486. /**
  487. * Sets whether to fork the javac compiler.
  488. *
  489. * @param f "true|false|on|off|yes|no"
  490. */
  491. public void setFork(boolean f) {
  492. fork = f;
  493. }
  494. /**
  495. * Sets the the name of the javac executable.
  496. *
  497. * <p>Ignored unless fork is true or extJavac has been specified
  498. * as the compiler.</p>
  499. */
  500. public void setExecutable(String forkExec) {
  501. forkedExecutable = forkExec;
  502. }
  503. /**
  504. * Is this a forked invocation of JDK's javac?
  505. */
  506. public boolean isForkedJavac() {
  507. return fork || "extJavac".equals(getCompiler());
  508. }
  509. /**
  510. * The name of the javac executable to use in fork-mode.
  511. */
  512. public String getJavacExecutable() {
  513. if (forkedExecutable == null && isForkedJavac()) {
  514. forkedExecutable = getSystemJavac();
  515. } else if (forkedExecutable != null && !isForkedJavac()) {
  516. forkedExecutable = null;
  517. }
  518. return forkedExecutable;
  519. }
  520. /**
  521. * Sets whether the -nowarn option should be used.
  522. */
  523. public void setNowarn(boolean flag) {
  524. this.nowarn = flag;
  525. }
  526. /**
  527. * Should the -nowarn option be used.
  528. */
  529. public boolean getNowarn() {
  530. return nowarn;
  531. }
  532. /**
  533. * Adds an implementation specific command line argument.
  534. */
  535. public ImplementationSpecificArgument createCompilerArg() {
  536. ImplementationSpecificArgument arg =
  537. new ImplementationSpecificArgument();
  538. implementationSpecificArgs.addElement(arg);
  539. return arg;
  540. }
  541. /**
  542. * Get the additional implementation specific command line arguments.
  543. * @return array of command line arguments, guaranteed to be non-null.
  544. */
  545. public String[] getCurrentCompilerArgs() {
  546. Vector args = new Vector();
  547. for (Enumeration enum = implementationSpecificArgs.elements();
  548. enum.hasMoreElements();) {
  549. ImplementationSpecificArgument arg =
  550. ((ImplementationSpecificArgument) enum.nextElement());
  551. String[] curr = arg.getParts();
  552. for (int i = 0; i < curr.length; i++) {
  553. args.addElement(curr[i]);
  554. }
  555. }
  556. String[] res = new String[args.size()];
  557. args.copyInto(res);
  558. return res;
  559. }
  560. /**
  561. * Executes the task.
  562. */
  563. public void execute() throws BuildException {
  564. checkParameters();
  565. resetFileLists();
  566. // scan source directories and dest directory to build up
  567. // compile lists
  568. String[] list = src.list();
  569. for (int i = 0; i < list.length; i++) {
  570. File srcDir = project.resolveFile(list[i]);
  571. if (!srcDir.exists()) {
  572. throw new BuildException("srcdir \""
  573. + srcDir.getPath()
  574. + "\" does not exist!", location);
  575. }
  576. DirectoryScanner ds = this.getDirectoryScanner(srcDir);
  577. String[] files = ds.getIncludedFiles();
  578. scanDir(srcDir, destDir != null ? destDir : srcDir, files);
  579. }
  580. compile();
  581. }
  582. /**
  583. * Clear the list of files to be compiled and copied..
  584. */
  585. protected void resetFileLists() {
  586. compileList = new File[0];
  587. }
  588. /**
  589. * Scans the directory looking for source files to be compiled.
  590. * The results are returned in the class variable compileList
  591. */
  592. protected void scanDir(File srcDir, File destDir, String[] files) {
  593. GlobPatternMapper m = new GlobPatternMapper();
  594. m.setFrom("*.java");
  595. m.setTo("*.class");
  596. SourceFileScanner sfs = new SourceFileScanner(this);
  597. File[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m);
  598. if (newFiles.length > 0) {
  599. File[] newCompileList = new File[compileList.length +
  600. newFiles.length];
  601. System.arraycopy(compileList, 0, newCompileList, 0,
  602. compileList.length);
  603. System.arraycopy(newFiles, 0, newCompileList,
  604. compileList.length, newFiles.length);
  605. compileList = newCompileList;
  606. }
  607. }
  608. /** Gets the list of files to be compiled. */
  609. public File[] getFileList() {
  610. return compileList;
  611. }
  612. protected boolean isJdkCompiler(String compilerImpl) {
  613. return "modern".equals(compilerImpl) ||
  614. "classic".equals(compilerImpl) ||
  615. "javac1.1".equals(compilerImpl) ||
  616. "javac1.2".equals(compilerImpl) ||
  617. "javac1.3".equals(compilerImpl) ||
  618. "javac1.4".equals(compilerImpl);
  619. }
  620. protected String getSystemJavac() {
  621. return JavaEnvUtils.getJdkExecutable("javac");
  622. }
  623. /**
  624. * Choose the implementation for this particular task.
  625. *
  626. * @since Ant 1.5
  627. */
  628. public void setCompiler(String compiler) {
  629. this.compiler = compiler;
  630. }
  631. /**
  632. * The implementation for this particular task.
  633. *
  634. * <p>Defaults to the build.compiler property but can be overriden
  635. * via the compiler and fork attributes.</p>
  636. *
  637. * @since Ant 1.5
  638. */
  639. public String getCompiler() {
  640. String compilerImpl =
  641. this.compiler != null ? this.compiler
  642. : project.getProperty("build.compiler");
  643. if (fork) {
  644. if (compilerImpl != null) {
  645. if (isJdkCompiler(compilerImpl)) {
  646. log("Since fork is true, ignoring compiler setting.",
  647. Project.MSG_WARN);
  648. compilerImpl = "extJavac";
  649. }
  650. else {
  651. log("Since compiler setting isn't classic or modern,"
  652. + "ignoring fork setting.", Project.MSG_WARN);
  653. }
  654. }
  655. else {
  656. compilerImpl = "extJavac";
  657. }
  658. }
  659. if (compilerImpl == null) {
  660. if (JavaEnvUtils.getJavaVersion() != JavaEnvUtils.JAVA_1_1 &&
  661. JavaEnvUtils.getJavaVersion() != JavaEnvUtils.JAVA_1_2) {
  662. compilerImpl = "modern";
  663. } else {
  664. compilerImpl = "classic";
  665. }
  666. }
  667. return compilerImpl;
  668. }
  669. /**
  670. * Check that all required attributes have been set and nothing
  671. * silly has been entered.
  672. *
  673. * @since Ant 1.5
  674. */
  675. protected void checkParameters() throws BuildException {
  676. if (src == null) {
  677. throw new BuildException("srcdir attribute must be set!",
  678. location);
  679. }
  680. if (src.size() == 0) {
  681. throw new BuildException("srcdir attribute must be set!",
  682. location);
  683. }
  684. if (destDir != null && !destDir.isDirectory()) {
  685. throw new BuildException("destination directory \""
  686. + destDir
  687. + "\" does not exist "
  688. + "or is not a directory", location);
  689. }
  690. }
  691. /**
  692. * Perform the compilation.
  693. *
  694. * @since Ant 1.5
  695. */
  696. protected void compile() {
  697. String compilerImpl = getCompiler();
  698. if (compileList.length > 0) {
  699. log("Compiling " + compileList.length +
  700. " source file"
  701. + (compileList.length == 1 ? "" : "s")
  702. + (destDir != null ? " to " + destDir : ""));
  703. if (listFiles) {
  704. for (int i = 0 ; i < compileList.length ; i++)
  705. {
  706. String filename = compileList[i].getAbsolutePath();
  707. log(filename) ;
  708. }
  709. }
  710. CompilerAdapter adapter =
  711. CompilerAdapterFactory.getCompiler(compilerImpl, this);
  712. // now we need to populate the compiler adapter
  713. adapter.setJavac(this);
  714. // finally, lets execute the compiler!!
  715. if (!adapter.execute()) {
  716. if (failOnError) {
  717. throw new BuildException(FAIL_MSG, location);
  718. } else {
  719. log(FAIL_MSG, Project.MSG_ERR);
  720. }
  721. }
  722. }
  723. }
  724. /**
  725. * Adds an "implementation" attribute to Commandline$Attribute
  726. * used to filter command line attributes based on the current
  727. * implementation.
  728. */
  729. public class ImplementationSpecificArgument
  730. extends Commandline.Argument {
  731. private String impl;
  732. public void setCompiler(String impl) {
  733. this.impl = impl;
  734. }
  735. public String[] getParts() {
  736. if (impl == null || impl.equals(getCompiler())) {
  737. return super.getParts();
  738. } else {
  739. return new String[0];
  740. }
  741. }
  742. }
  743. }