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