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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import java.io.File;
  20. import java.util.ArrayList;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import org.apache.tools.ant.BuildException;
  24. import org.apache.tools.ant.DirectoryScanner;
  25. import org.apache.tools.ant.MagicNames;
  26. import org.apache.tools.ant.Project;
  27. import org.apache.tools.ant.taskdefs.compilers.CompilerAdapter;
  28. import org.apache.tools.ant.taskdefs.compilers.CompilerAdapterFactory;
  29. import org.apache.tools.ant.types.Path;
  30. import org.apache.tools.ant.types.Reference;
  31. import org.apache.tools.ant.util.FileUtils;
  32. import org.apache.tools.ant.util.GlobPatternMapper;
  33. import org.apache.tools.ant.util.JavaEnvUtils;
  34. import org.apache.tools.ant.util.SourceFileScanner;
  35. import org.apache.tools.ant.util.facade.FacadeTaskHelper;
  36. /**
  37. * Compiles Java source files. This task can take the following
  38. * arguments:
  39. * <ul>
  40. * <li>sourcedir
  41. * <li>destdir
  42. * <li>deprecation
  43. * <li>classpath
  44. * <li>bootclasspath
  45. * <li>extdirs
  46. * <li>optimize
  47. * <li>debug
  48. * <li>encoding
  49. * <li>target
  50. * <li>depend
  51. * <li>verbose
  52. * <li>failonerror
  53. * <li>includeantruntime
  54. * <li>includejavaruntime
  55. * <li>source
  56. * <li>compiler
  57. * </ul>
  58. * Of these arguments, the <b>sourcedir</b> and <b>destdir</b> are required.
  59. * <p>
  60. * When this task executes, it will recursively scan the sourcedir and
  61. * destdir looking for Java source files to compile. This task makes its
  62. * compile decision based on timestamp.
  63. *
  64. *
  65. * @since Ant 1.1
  66. *
  67. * @ant.task category="java"
  68. */
  69. public class Javac extends MatchingTask {
  70. private static final String FAIL_MSG
  71. = "Compile failed; see the compiler error output for details.";
  72. private static final String JAVAC16 = "javac1.6";
  73. private static final String JAVAC15 = "javac1.5";
  74. private static final String JAVAC14 = "javac1.4";
  75. private static final String JAVAC13 = "javac1.3";
  76. private static final String JAVAC12 = "javac1.2";
  77. private static final String JAVAC11 = "javac1.1";
  78. private static final String MODERN = "modern";
  79. private static final String CLASSIC = "classic";
  80. private static final String EXTJAVAC = "extJavac";
  81. private static final String PACKAGE_INFO_JAVA = "package-info.java";
  82. private static final String PACKAGE_INFO_CLASS = "package-info.class";
  83. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  84. private Path src;
  85. private File destDir;
  86. private Path compileClasspath;
  87. private Path compileSourcepath;
  88. private String encoding;
  89. private boolean debug = false;
  90. private boolean optimize = false;
  91. private boolean deprecation = false;
  92. private boolean depend = false;
  93. private boolean verbose = false;
  94. private String targetAttribute;
  95. private Path bootclasspath;
  96. private Path extdirs;
  97. private boolean includeAntRuntime = true;
  98. private boolean includeJavaRuntime = false;
  99. private boolean fork = false;
  100. private String forkedExecutable = null;
  101. private boolean nowarn = false;
  102. private String memoryInitialSize;
  103. private String memoryMaximumSize;
  104. private FacadeTaskHelper facade = null;
  105. // CheckStyle:VisibilityModifier OFF - bc
  106. protected boolean failOnError = true;
  107. protected boolean listFiles = false;
  108. protected File[] compileList = new File[0];
  109. // CheckStyle:VisibilityModifier ON
  110. private String source;
  111. private String debugLevel;
  112. private File tmpDir;
  113. private String updatedProperty;
  114. private String errorProperty;
  115. private boolean taskSuccess = true; // assume the best
  116. private boolean includeDestClasses = true;
  117. private List updateDirList = new ArrayList();
  118. /**
  119. * Javac task for compilation of Java files.
  120. */
  121. public Javac() {
  122. facade = new FacadeTaskHelper(assumedJavaVersion());
  123. }
  124. private String assumedJavaVersion() {
  125. if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2)) {
  126. return JAVAC12;
  127. } else if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) {
  128. return JAVAC13;
  129. } else if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_4)) {
  130. return JAVAC14;
  131. } else if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_5)) {
  132. return JAVAC15;
  133. } else if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_6)) {
  134. return JAVAC16;
  135. } else {
  136. return CLASSIC;
  137. }
  138. }
  139. /**
  140. * Get the value of debugLevel.
  141. * @return value of debugLevel.
  142. */
  143. public String getDebugLevel() {
  144. return debugLevel;
  145. }
  146. /**
  147. * Keyword list to be appended to the -g command-line switch.
  148. *
  149. * This will be ignored by all implementations except modern
  150. * and classic(ver >= 1.2). Legal values are none or a
  151. * comma-separated list of the following keywords: lines, vars,
  152. * and source. If debuglevel is not specified, by default, :none
  153. * will be appended to -g. If debug is not turned on, this attribute
  154. * will be ignored.
  155. *
  156. * @param v Value to assign to debugLevel.
  157. */
  158. public void setDebugLevel(String v) {
  159. this.debugLevel = v;
  160. }
  161. /**
  162. * Get the value of source.
  163. * @return value of source.
  164. */
  165. public String getSource() {
  166. return source != null
  167. ? source : getProject().getProperty(MagicNames.BUILD_JAVAC_SOURCE);
  168. }
  169. /**
  170. * Value of the -source command-line switch; will be ignored by
  171. * all implementations except modern, jikes and gcj (gcj uses
  172. * -fsource).
  173. *
  174. * <p>If you use this attribute together with jikes or gcj, you
  175. * must make sure that your version of jikes supports the -source
  176. * switch.</p>
  177. *
  178. * <p>Legal values are 1.3, 1.4, 1.5, and 5 - by default, no
  179. * -source argument will be used at all.</p>
  180. *
  181. * @param v Value to assign to source.
  182. */
  183. public void setSource(String v) {
  184. this.source = v;
  185. }
  186. /**
  187. * Adds a path for source compilation.
  188. *
  189. * @return a nested src element.
  190. */
  191. public Path createSrc() {
  192. if (src == null) {
  193. src = new Path(getProject());
  194. }
  195. return src.createPath();
  196. }
  197. /**
  198. * Recreate src.
  199. *
  200. * @return a nested src element.
  201. */
  202. protected Path recreateSrc() {
  203. src = null;
  204. return createSrc();
  205. }
  206. /**
  207. * Set the source directories to find the source Java files.
  208. * @param srcDir the source directories as a path
  209. */
  210. public void setSrcdir(Path srcDir) {
  211. if (src == null) {
  212. src = srcDir;
  213. } else {
  214. src.append(srcDir);
  215. }
  216. }
  217. /**
  218. * Gets the source dirs to find the source java files.
  219. * @return the source directories as a path
  220. */
  221. public Path getSrcdir() {
  222. return src;
  223. }
  224. /**
  225. * Set the destination directory into which the Java source
  226. * files should be compiled.
  227. * @param destDir the destination director
  228. */
  229. public void setDestdir(File destDir) {
  230. this.destDir = destDir;
  231. }
  232. /**
  233. * Gets the destination directory into which the java source files
  234. * should be compiled.
  235. * @return the destination directory
  236. */
  237. public File getDestdir() {
  238. return destDir;
  239. }
  240. /**
  241. * Set the sourcepath to be used for this compilation.
  242. * @param sourcepath the source path
  243. */
  244. public void setSourcepath(Path sourcepath) {
  245. if (compileSourcepath == null) {
  246. compileSourcepath = sourcepath;
  247. } else {
  248. compileSourcepath.append(sourcepath);
  249. }
  250. }
  251. /**
  252. * Gets the sourcepath to be used for this compilation.
  253. * @return the source path
  254. */
  255. public Path getSourcepath() {
  256. return compileSourcepath;
  257. }
  258. /**
  259. * Adds a path to sourcepath.
  260. * @return a sourcepath to be configured
  261. */
  262. public Path createSourcepath() {
  263. if (compileSourcepath == null) {
  264. compileSourcepath = new Path(getProject());
  265. }
  266. return compileSourcepath.createPath();
  267. }
  268. /**
  269. * Adds a reference to a source path defined elsewhere.
  270. * @param r a reference to a source path
  271. */
  272. public void setSourcepathRef(Reference r) {
  273. createSourcepath().setRefid(r);
  274. }
  275. /**
  276. * Set the classpath to be used for this compilation.
  277. *
  278. * @param classpath an Ant Path object containing the compilation classpath.
  279. */
  280. public void setClasspath(Path classpath) {
  281. if (compileClasspath == null) {
  282. compileClasspath = classpath;
  283. } else {
  284. compileClasspath.append(classpath);
  285. }
  286. }
  287. /**
  288. * Gets the classpath to be used for this compilation.
  289. * @return the class path
  290. */
  291. public Path getClasspath() {
  292. return compileClasspath;
  293. }
  294. /**
  295. * Adds a path to the classpath.
  296. * @return a class path to be configured
  297. */
  298. public Path createClasspath() {
  299. if (compileClasspath == null) {
  300. compileClasspath = new Path(getProject());
  301. }
  302. return compileClasspath.createPath();
  303. }
  304. /**
  305. * Adds a reference to a classpath defined elsewhere.
  306. * @param r a reference to a classpath
  307. */
  308. public void setClasspathRef(Reference r) {
  309. createClasspath().setRefid(r);
  310. }
  311. /**
  312. * Sets the bootclasspath that will be used to compile the classes
  313. * against.
  314. * @param bootclasspath a path to use as a boot class path (may be more
  315. * than one)
  316. */
  317. public void setBootclasspath(Path bootclasspath) {
  318. if (this.bootclasspath == null) {
  319. this.bootclasspath = bootclasspath;
  320. } else {
  321. this.bootclasspath.append(bootclasspath);
  322. }
  323. }
  324. /**
  325. * Gets the bootclasspath that will be used to compile the classes
  326. * against.
  327. * @return the boot path
  328. */
  329. public Path getBootclasspath() {
  330. return bootclasspath;
  331. }
  332. /**
  333. * Adds a path to the bootclasspath.
  334. * @return a path to be configured
  335. */
  336. public Path createBootclasspath() {
  337. if (bootclasspath == null) {
  338. bootclasspath = new Path(getProject());
  339. }
  340. return bootclasspath.createPath();
  341. }
  342. /**
  343. * Adds a reference to a classpath defined elsewhere.
  344. * @param r a reference to a classpath
  345. */
  346. public void setBootClasspathRef(Reference r) {
  347. createBootclasspath().setRefid(r);
  348. }
  349. /**
  350. * Sets the extension directories that will be used during the
  351. * compilation.
  352. * @param extdirs a path
  353. */
  354. public void setExtdirs(Path extdirs) {
  355. if (this.extdirs == null) {
  356. this.extdirs = extdirs;
  357. } else {
  358. this.extdirs.append(extdirs);
  359. }
  360. }
  361. /**
  362. * Gets the extension directories that will be used during the
  363. * compilation.
  364. * @return the extension directories as a path
  365. */
  366. public Path getExtdirs() {
  367. return extdirs;
  368. }
  369. /**
  370. * Adds a path to extdirs.
  371. * @return a path to be configured
  372. */
  373. public Path createExtdirs() {
  374. if (extdirs == null) {
  375. extdirs = new Path(getProject());
  376. }
  377. return extdirs.createPath();
  378. }
  379. /**
  380. * If true, list the source files being handed off to the compiler.
  381. * @param list if true list the source files
  382. */
  383. public void setListfiles(boolean list) {
  384. listFiles = list;
  385. }
  386. /**
  387. * Get the listfiles flag.
  388. * @return the listfiles flag
  389. */
  390. public boolean getListfiles() {
  391. return listFiles;
  392. }
  393. /**
  394. * Indicates whether the build will continue
  395. * even if there are compilation errors; defaults to true.
  396. * @param fail if true halt the build on failure
  397. */
  398. public void setFailonerror(boolean fail) {
  399. failOnError = fail;
  400. }
  401. /**
  402. * @ant.attribute ignore="true"
  403. * @param proceed inverse of failoferror
  404. */
  405. public void setProceed(boolean proceed) {
  406. failOnError = !proceed;
  407. }
  408. /**
  409. * Gets the failonerror flag.
  410. * @return the failonerror flag
  411. */
  412. public boolean getFailonerror() {
  413. return failOnError;
  414. }
  415. /**
  416. * Indicates whether source should be
  417. * compiled with deprecation information; defaults to off.
  418. * @param deprecation if true turn on deprecation information
  419. */
  420. public void setDeprecation(boolean deprecation) {
  421. this.deprecation = deprecation;
  422. }
  423. /**
  424. * Gets the deprecation flag.
  425. * @return the deprecation flag
  426. */
  427. public boolean getDeprecation() {
  428. return deprecation;
  429. }
  430. /**
  431. * The initial size of the memory for the underlying VM
  432. * if javac is run externally; ignored otherwise.
  433. * Defaults to the standard VM memory setting.
  434. * (Examples: 83886080, 81920k, or 80m)
  435. * @param memoryInitialSize string to pass to VM
  436. */
  437. public void setMemoryInitialSize(String memoryInitialSize) {
  438. this.memoryInitialSize = memoryInitialSize;
  439. }
  440. /**
  441. * Gets the memoryInitialSize flag.
  442. * @return the memoryInitialSize flag
  443. */
  444. public String getMemoryInitialSize() {
  445. return memoryInitialSize;
  446. }
  447. /**
  448. * The maximum size of the memory for the underlying VM
  449. * if javac is run externally; ignored otherwise.
  450. * Defaults to the standard VM memory setting.
  451. * (Examples: 83886080, 81920k, or 80m)
  452. * @param memoryMaximumSize string to pass to VM
  453. */
  454. public void setMemoryMaximumSize(String memoryMaximumSize) {
  455. this.memoryMaximumSize = memoryMaximumSize;
  456. }
  457. /**
  458. * Gets the memoryMaximumSize flag.
  459. * @return the memoryMaximumSize flag
  460. */
  461. public String getMemoryMaximumSize() {
  462. return memoryMaximumSize;
  463. }
  464. /**
  465. * Set the Java source file encoding name.
  466. * @param encoding the source file encoding
  467. */
  468. public void setEncoding(String encoding) {
  469. this.encoding = encoding;
  470. }
  471. /**
  472. * Gets the java source file encoding name.
  473. * @return the source file encoding name
  474. */
  475. public String getEncoding() {
  476. return encoding;
  477. }
  478. /**
  479. * Indicates whether source should be compiled
  480. * with debug information; defaults to off.
  481. * @param debug if true compile with debug information
  482. */
  483. public void setDebug(boolean debug) {
  484. this.debug = debug;
  485. }
  486. /**
  487. * Gets the debug flag.
  488. * @return the debug flag
  489. */
  490. public boolean getDebug() {
  491. return debug;
  492. }
  493. /**
  494. * If true, compiles with optimization enabled.
  495. * @param optimize if true compile with optimization enabled
  496. */
  497. public void setOptimize(boolean optimize) {
  498. this.optimize = optimize;
  499. }
  500. /**
  501. * Gets the optimize flag.
  502. * @return the optimize flag
  503. */
  504. public boolean getOptimize() {
  505. return optimize;
  506. }
  507. /**
  508. * Enables dependency-tracking for compilers
  509. * that support this (jikes and classic).
  510. * @param depend if true enable dependency-tracking
  511. */
  512. public void setDepend(boolean depend) {
  513. this.depend = depend;
  514. }
  515. /**
  516. * Gets the depend flag.
  517. * @return the depend flag
  518. */
  519. public boolean getDepend() {
  520. return depend;
  521. }
  522. /**
  523. * If true, asks the compiler for verbose output.
  524. * @param verbose if true, asks the compiler for verbose output
  525. */
  526. public void setVerbose(boolean verbose) {
  527. this.verbose = verbose;
  528. }
  529. /**
  530. * Gets the verbose flag.
  531. * @return the verbose flag
  532. */
  533. public boolean getVerbose() {
  534. return verbose;
  535. }
  536. /**
  537. * Sets the target VM that the classes will be compiled for. Valid
  538. * values depend on the compiler, for jdk 1.4 the valid values are
  539. * "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "5" and "6".
  540. * @param target the target VM
  541. */
  542. public void setTarget(String target) {
  543. this.targetAttribute = target;
  544. }
  545. /**
  546. * Gets the target VM that the classes will be compiled for.
  547. * @return the target VM
  548. */
  549. public String getTarget() {
  550. return targetAttribute != null
  551. ? targetAttribute
  552. : getProject().getProperty(MagicNames.BUILD_JAVAC_TARGET);
  553. }
  554. /**
  555. * If true, includes Ant's own classpath in the classpath.
  556. * @param include if true, includes Ant's own classpath in the classpath
  557. */
  558. public void setIncludeantruntime(boolean include) {
  559. includeAntRuntime = include;
  560. }
  561. /**
  562. * Gets whether or not the ant classpath is to be included in the classpath.
  563. * @return whether or not the ant classpath is to be included in the classpath
  564. */
  565. public boolean getIncludeantruntime() {
  566. return includeAntRuntime;
  567. }
  568. /**
  569. * If true, includes the Java runtime libraries in the classpath.
  570. * @param include if true, includes the Java runtime libraries in the classpath
  571. */
  572. public void setIncludejavaruntime(boolean include) {
  573. includeJavaRuntime = include;
  574. }
  575. /**
  576. * Gets whether or not the java runtime should be included in this
  577. * task's classpath.
  578. * @return the includejavaruntime attribute
  579. */
  580. public boolean getIncludejavaruntime() {
  581. return includeJavaRuntime;
  582. }
  583. /**
  584. * If true, forks the javac compiler.
  585. *
  586. * @param f "true|false|on|off|yes|no"
  587. */
  588. public void setFork(boolean f) {
  589. fork = f;
  590. }
  591. /**
  592. * Sets the name of the javac executable.
  593. *
  594. * <p>Ignored unless fork is true or extJavac has been specified
  595. * as the compiler.</p>
  596. * @param forkExec the name of the executable
  597. */
  598. public void setExecutable(String forkExec) {
  599. forkedExecutable = forkExec;
  600. }
  601. /**
  602. * The value of the executable attribute, if any.
  603. *
  604. * @since Ant 1.6
  605. * @return the name of the java executable
  606. */
  607. public String getExecutable() {
  608. return forkedExecutable;
  609. }
  610. /**
  611. * Is this a forked invocation of JDK's javac?
  612. * @return true if this is a forked invocation
  613. */
  614. public boolean isForkedJavac() {
  615. return fork || "extJavac".equals(getCompiler());
  616. }
  617. /**
  618. * The name of the javac executable to use in fork-mode.
  619. *
  620. * <p>This is either the name specified with the executable
  621. * attribute or the full path of the javac compiler of the VM Ant
  622. * is currently running in - guessed by Ant.</p>
  623. *
  624. * <p>You should <strong>not</strong> invoke this method if you
  625. * want to get the value of the executable command - use {@link
  626. * #getExecutable getExecutable} for this.</p>
  627. * @return the name of the javac executable
  628. */
  629. public String getJavacExecutable() {
  630. if (forkedExecutable == null && isForkedJavac()) {
  631. forkedExecutable = getSystemJavac();
  632. } else if (forkedExecutable != null && !isForkedJavac()) {
  633. forkedExecutable = null;
  634. }
  635. return forkedExecutable;
  636. }
  637. /**
  638. * If true, enables the -nowarn option.
  639. * @param flag if true, enable the -nowarn option
  640. */
  641. public void setNowarn(boolean flag) {
  642. this.nowarn = flag;
  643. }
  644. /**
  645. * Should the -nowarn option be used.
  646. * @return true if the -nowarn option should be used
  647. */
  648. public boolean getNowarn() {
  649. return nowarn;
  650. }
  651. /**
  652. * Adds an implementation specific command-line argument.
  653. * @return a ImplementationSpecificArgument to be configured
  654. */
  655. public ImplementationSpecificArgument createCompilerArg() {
  656. ImplementationSpecificArgument arg =
  657. new ImplementationSpecificArgument();
  658. facade.addImplementationArgument(arg);
  659. return arg;
  660. }
  661. /**
  662. * Get the additional implementation specific command line arguments.
  663. * @return array of command line arguments, guaranteed to be non-null.
  664. */
  665. public String[] getCurrentCompilerArgs() {
  666. String chosen = facade.getExplicitChoice();
  667. try {
  668. // make sure facade knows about magic properties and fork setting
  669. String appliedCompiler = getCompiler();
  670. facade.setImplementation(appliedCompiler);
  671. String[] result = facade.getArgs();
  672. String altCompilerName = getAltCompilerName(facade.getImplementation());
  673. if (result.length == 0 && altCompilerName != null) {
  674. facade.setImplementation(altCompilerName);
  675. result = facade.getArgs();
  676. }
  677. return result;
  678. } finally {
  679. facade.setImplementation(chosen);
  680. }
  681. }
  682. private String getAltCompilerName(String anImplementation) {
  683. if (JAVAC16.equalsIgnoreCase(anImplementation)
  684. || JAVAC15.equalsIgnoreCase(anImplementation)
  685. || JAVAC14.equalsIgnoreCase(anImplementation)
  686. || JAVAC13.equalsIgnoreCase(anImplementation)) {
  687. return MODERN;
  688. }
  689. if (JAVAC12.equalsIgnoreCase(anImplementation)
  690. || JAVAC11.equalsIgnoreCase(anImplementation)) {
  691. return CLASSIC;
  692. }
  693. if (MODERN.equalsIgnoreCase(anImplementation)) {
  694. String nextSelected = assumedJavaVersion();
  695. if (JAVAC16.equalsIgnoreCase(nextSelected)
  696. || JAVAC15.equalsIgnoreCase(nextSelected)
  697. || JAVAC14.equalsIgnoreCase(nextSelected)
  698. || JAVAC13.equalsIgnoreCase(nextSelected)) {
  699. return nextSelected;
  700. }
  701. }
  702. if (CLASSIC.equals(anImplementation)) {
  703. return assumedJavaVersion();
  704. }
  705. if (EXTJAVAC.equalsIgnoreCase(anImplementation)) {
  706. return assumedJavaVersion();
  707. }
  708. return null;
  709. }
  710. /**
  711. * Where Ant should place temporary files.
  712. *
  713. * @since Ant 1.6
  714. * @param tmpDir the temporary directory
  715. */
  716. public void setTempdir(File tmpDir) {
  717. this.tmpDir = tmpDir;
  718. }
  719. /**
  720. * Where Ant should place temporary files.
  721. *
  722. * @since Ant 1.6
  723. * @return the temporary directory
  724. */
  725. public File getTempdir() {
  726. return tmpDir;
  727. }
  728. /**
  729. * The property to set on compliation success.
  730. * This property will not be set if the compilation
  731. * fails, or if there are no files to compile.
  732. * @param updatedProperty the property name to use.
  733. * @since Ant 1.7.1.
  734. */
  735. public void setUpdatedProperty(String updatedProperty) {
  736. this.updatedProperty = updatedProperty;
  737. }
  738. /**
  739. * The property to set on compliation failure.
  740. * This property will be set if the compilation
  741. * fails.
  742. * @param errorProperty the property name to use.
  743. * @since Ant 1.7.1.
  744. */
  745. public void setErrorProperty(String errorProperty) {
  746. this.errorProperty = errorProperty;
  747. }
  748. /**
  749. * This property controls whether to include the
  750. * destination classes directory in the classpath
  751. * given to the compiler.
  752. * The default value is "true".
  753. * @param includeDestClasses the value to use.
  754. */
  755. public void setIncludeDestClasses(boolean includeDestClasses) {
  756. this.includeDestClasses = includeDestClasses;
  757. }
  758. /**
  759. * Get the value of the includeDestClasses property.
  760. * @return the value.
  761. */
  762. public boolean isIncludeDestClasses() {
  763. return includeDestClasses;
  764. }
  765. /**
  766. * Get the result of the javac task (success or failure).
  767. * @return true if compilation succeeded, or
  768. * was not neccessary, false if the compilation failed.
  769. */
  770. public boolean getTaskSuccess() {
  771. return taskSuccess;
  772. }
  773. /**
  774. * Executes the task.
  775. * @exception BuildException if an error occurs
  776. */
  777. public void execute() throws BuildException {
  778. checkParameters();
  779. resetFileLists();
  780. // scan source directories and dest directory to build up
  781. // compile lists
  782. String[] list = src.list();
  783. for (int i = 0; i < list.length; i++) {
  784. File srcDir = getProject().resolveFile(list[i]);
  785. if (!srcDir.exists()) {
  786. throw new BuildException("srcdir \""
  787. + srcDir.getPath()
  788. + "\" does not exist!", getLocation());
  789. }
  790. DirectoryScanner ds = this.getDirectoryScanner(srcDir);
  791. String[] files = ds.getIncludedFiles();
  792. scanDir(srcDir, destDir != null ? destDir : srcDir, files);
  793. }
  794. compile();
  795. if (updatedProperty != null
  796. && taskSuccess
  797. && compileList.length != 0) {
  798. getProject().setNewProperty(updatedProperty, "true");
  799. }
  800. }
  801. /**
  802. * Clear the list of files to be compiled and copied..
  803. */
  804. protected void resetFileLists() {
  805. compileList = new File[0];
  806. }
  807. /**
  808. * Scans the directory looking for source files to be compiled.
  809. * The results are returned in the class variable compileList
  810. *
  811. * @param srcDir The source directory
  812. * @param destDir The destination directory
  813. * @param files An array of filenames
  814. */
  815. protected void scanDir(File srcDir, File destDir, String[] files) {
  816. GlobPatternMapper m = new GlobPatternMapper();
  817. m.setFrom("*.java");
  818. m.setTo("*.class");
  819. SourceFileScanner sfs = new SourceFileScanner(this);
  820. File[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m);
  821. newFiles = removePackageInfoFiles(newFiles, srcDir, destDir);
  822. if (newFiles.length > 0) {
  823. File[] newCompileList
  824. = new File[compileList.length + newFiles.length];
  825. System.arraycopy(compileList, 0, newCompileList, 0,
  826. compileList.length);
  827. System.arraycopy(newFiles, 0, newCompileList,
  828. compileList.length, newFiles.length);
  829. compileList = newCompileList;
  830. }
  831. }
  832. /**
  833. * Gets the list of files to be compiled.
  834. * @return the list of files as an array
  835. */
  836. public File[] getFileList() {
  837. return compileList;
  838. }
  839. /**
  840. * Is the compiler implementation a jdk compiler
  841. *
  842. * @param compilerImpl the name of the compiler implementation
  843. * @return true if compilerImpl is "modern", "classic",
  844. * "javac1.1", "javac1.2", "javac1.3", "javac1.4", "javac1.5" or
  845. * "javac1.6".
  846. */
  847. protected boolean isJdkCompiler(String compilerImpl) {
  848. return MODERN.equals(compilerImpl)
  849. || CLASSIC.equals(compilerImpl)
  850. || JAVAC16.equals(compilerImpl)
  851. || JAVAC15.equals(compilerImpl)
  852. || JAVAC14.equals(compilerImpl)
  853. || JAVAC13.equals(compilerImpl)
  854. || JAVAC12.equals(compilerImpl)
  855. || JAVAC11.equals(compilerImpl);
  856. }
  857. /**
  858. * @return the executable name of the java compiler
  859. */
  860. protected String getSystemJavac() {
  861. return JavaEnvUtils.getJdkExecutable("javac");
  862. }
  863. /**
  864. * Choose the implementation for this particular task.
  865. * @param compiler the name of the compiler
  866. * @since Ant 1.5
  867. */
  868. public void setCompiler(String compiler) {
  869. facade.setImplementation(compiler);
  870. }
  871. /**
  872. * The implementation for this particular task.
  873. *
  874. * <p>Defaults to the build.compiler property but can be overridden
  875. * via the compiler and fork attributes.</p>
  876. *
  877. * <p>If fork has been set to true, the result will be extJavac
  878. * and not classic or java1.2 - no matter what the compiler
  879. * attribute looks like.</p>
  880. *
  881. * @see #getCompilerVersion
  882. * @return the compiler.
  883. * @since Ant 1.5
  884. */
  885. public String getCompiler() {
  886. String compilerImpl = getCompilerVersion();
  887. if (fork) {
  888. if (isJdkCompiler(compilerImpl)) {
  889. compilerImpl = "extJavac";
  890. } else {
  891. log("Since compiler setting isn't classic or modern, "
  892. + "ignoring fork setting.", Project.MSG_WARN);
  893. }
  894. }
  895. return compilerImpl;
  896. }
  897. /**
  898. * The implementation for this particular task.
  899. *
  900. * <p>Defaults to the build.compiler property but can be overridden
  901. * via the compiler attribute.</p>
  902. *
  903. * <p>This method does not take the fork attribute into
  904. * account.</p>
  905. *
  906. * @see #getCompiler
  907. * @return the compiler.
  908. *
  909. * @since Ant 1.5
  910. */
  911. public String getCompilerVersion() {
  912. facade.setMagicValue(getProject().getProperty("build.compiler"));
  913. return facade.getImplementation();
  914. }
  915. /**
  916. * Check that all required attributes have been set and nothing
  917. * silly has been entered.
  918. *
  919. * @since Ant 1.5
  920. * @exception BuildException if an error occurs
  921. */
  922. protected void checkParameters() throws BuildException {
  923. if (src == null) {
  924. throw new BuildException("srcdir attribute must be set!",
  925. getLocation());
  926. }
  927. if (src.size() == 0) {
  928. throw new BuildException("srcdir attribute must be set!",
  929. getLocation());
  930. }
  931. if (destDir != null && !destDir.isDirectory()) {
  932. throw new BuildException("destination directory \""
  933. + destDir
  934. + "\" does not exist "
  935. + "or is not a directory", getLocation());
  936. }
  937. }
  938. /**
  939. * Perform the compilation.
  940. *
  941. * @since Ant 1.5
  942. */
  943. protected void compile() {
  944. String compilerImpl = getCompiler();
  945. if (compileList.length > 0) {
  946. log("Compiling " + compileList.length + " source file"
  947. + (compileList.length == 1 ? "" : "s")
  948. + (destDir != null ? " to " + destDir : ""));
  949. if (listFiles) {
  950. for (int i = 0; i < compileList.length; i++) {
  951. String filename = compileList[i].getAbsolutePath();
  952. log(filename);
  953. }
  954. }
  955. CompilerAdapter adapter =
  956. CompilerAdapterFactory.getCompiler(compilerImpl, this);
  957. // now we need to populate the compiler adapter
  958. adapter.setJavac(this);
  959. // finally, lets execute the compiler!!
  960. if (adapter.execute()) {
  961. // Success - check
  962. for (Iterator i = updateDirList.iterator(); i.hasNext();) {
  963. File file = (File) i.next();
  964. file.setLastModified(System.currentTimeMillis());
  965. }
  966. } else {
  967. // Fail path
  968. this.taskSuccess = false;
  969. if (errorProperty != null) {
  970. getProject().setNewProperty(
  971. errorProperty, "true");
  972. }
  973. if (failOnError) {
  974. throw new BuildException(FAIL_MSG, getLocation());
  975. } else {
  976. log(FAIL_MSG, Project.MSG_ERR);
  977. }
  978. }
  979. }
  980. }
  981. /**
  982. * Adds an "compiler" attribute to Commandline$Attribute used to
  983. * filter command line attributes based on the current
  984. * implementation.
  985. */
  986. public class ImplementationSpecificArgument extends
  987. org.apache.tools.ant.util.facade.ImplementationSpecificArgument {
  988. /**
  989. * @param impl the name of the compiler
  990. */
  991. public void setCompiler(String impl) {
  992. super.setImplementation(impl);
  993. }
  994. }
  995. // ----------------------------------------------------------------
  996. // Code to remove package-info.java files from compilation
  997. // Since Ant 1.7.1.
  998. //
  999. // package-info.java are files that contain package level
  1000. // annotations. They may or may not have corresponding .class
  1001. // files.
  1002. //
  1003. // The following code uses the algorithm:
  1004. // * on entry we have the files that need to be compiled
  1005. // * if the filename is not package-info.java compile it
  1006. // * if a corresponding .class file exists compile it
  1007. // * if the corresponding class directory does not exist compile it
  1008. // * if the corresponding directory lastmodifed time is
  1009. // older than the java file, compile the java file and
  1010. // touch the corresponding class directory (on successful
  1011. // compilation).
  1012. //
  1013. // ----------------------------------------------------------------
  1014. private File[] removePackageInfoFiles(
  1015. File[] newFiles, File srcDir, File destDir) {
  1016. if (!hasPackageInfo(newFiles)) {
  1017. return newFiles;
  1018. }
  1019. List ret = new ArrayList();
  1020. for (int i = 0; i < newFiles.length; ++i) {
  1021. if (needsCompilePackageFile(newFiles[i], srcDir, destDir)) {
  1022. ret.add(newFiles[i]);
  1023. }
  1024. }
  1025. return (File[]) ret.toArray(new File[0]);
  1026. }
  1027. private boolean hasPackageInfo(File[] newFiles) {
  1028. for (int i = 0; i < newFiles.length; ++i) {
  1029. if (newFiles[i].getName().equals(PACKAGE_INFO_JAVA)) {
  1030. return true;
  1031. }
  1032. }
  1033. return false;
  1034. }
  1035. private boolean needsCompilePackageFile(
  1036. File file, File srcDir, File destDir) {
  1037. if (!file.getName().equals(PACKAGE_INFO_JAVA)) {
  1038. return true;
  1039. }
  1040. // return true if destDir contains the file
  1041. String rel = FILE_UTILS.removeLeadingPath(srcDir, file);
  1042. File destFile = new File(destDir, rel);
  1043. File parent = destFile.getParentFile();
  1044. destFile = new File(parent, PACKAGE_INFO_CLASS);
  1045. File sourceFile = new File(srcDir, rel);
  1046. if (destFile.exists()) {
  1047. return true;
  1048. }
  1049. // Dest file does not exist
  1050. // Compile Source file if sourceFile is newer that destDir
  1051. // TODO - use fs
  1052. if (sourceFile.lastModified()
  1053. > destFile.getParentFile().lastModified()) {
  1054. updateDirList.add(destFile.getParentFile());
  1055. return true;
  1056. }
  1057. return false;
  1058. }
  1059. }