1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 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.GlobPatternMapper; 32 import org.apache.tools.ant.util.JavaEnvUtils; 33 import org.apache.tools.ant.util.SourceFileScanner; 34 import org.apache.tools.ant.util.facade.FacadeTaskHelper; 35 36 69 70 public class Javac extends MatchingTask { 71 72 private static final String FAIL_MSG 73 = "Compile failed; see the compiler error output for details."; 74 75 private static final String JAVAC16 = "javac1.6"; 76 private static final String JAVAC15 = "javac1.5"; 77 private static final String JAVAC14 = "javac1.4"; 78 private static final String JAVAC13 = "javac1.3"; 79 private static final String JAVAC12 = "javac1.2"; 80 private static final String JAVAC11 = "javac1.1"; 81 private static final String MODERN = "modern"; 82 private static final String CLASSIC = "classic"; 83 private static final String EXTJAVAC = "extJavac"; 84 85 private Path src; 86 private File destDir; 87 private Path compileClasspath; 88 private Path compileSourcepath; 89 private String encoding; 90 private boolean debug = false; 91 private boolean optimize = false; 92 private boolean deprecation = false; 93 private boolean depend = false; 94 private boolean verbose = false; 95 private String targetAttribute; 96 private Path bootclasspath; 97 private Path extdirs; 98 private boolean includeAntRuntime = true; 99 private boolean includeJavaRuntime = false; 100 private boolean fork = false; 101 private String forkedExecutable = null; 102 private boolean nowarn = false; 103 private String memoryInitialSize; 104 private String memoryMaximumSize; 105 private FacadeTaskHelper facade = null; 106 107 protected boolean failOnError = true; 109 protected boolean listFiles = false; 110 protected File [] compileList = new File [0]; 111 113 private String source; 114 private String debugLevel; 115 private File tmpDir; 116 117 120 public Javac() { 121 facade = new FacadeTaskHelper(assumedJavaVersion()); 122 } 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 144 public String getDebugLevel() { 145 return debugLevel; 146 } 147 148 160 public void setDebugLevel(String v) { 161 this.debugLevel = v; 162 } 163 164 168 public String getSource() { 169 return source != null 170 ? source : getProject().getProperty(MagicNames.BUILD_JAVAC_SOURCE); 171 } 172 173 184 public void setSource(String v) { 185 this.source = v; 186 } 187 188 193 public Path createSrc() { 194 if (src == null) { 195 src = new Path(getProject()); 196 } 197 return src.createPath(); 198 } 199 200 205 protected Path recreateSrc() { 206 src = null; 207 return createSrc(); 208 } 209 210 214 public void setSrcdir(Path srcDir) { 215 if (src == null) { 216 src = srcDir; 217 } else { 218 src.append(srcDir); 219 } 220 } 221 222 226 public Path getSrcdir() { 227 return src; 228 } 229 230 235 public void setDestdir(File destDir) { 236 this.destDir = destDir; 237 } 238 239 244 public File getDestdir() { 245 return destDir; 246 } 247 248 252 public void setSourcepath(Path sourcepath) { 253 if (compileSourcepath == null) { 254 compileSourcepath = sourcepath; 255 } else { 256 compileSourcepath.append(sourcepath); 257 } 258 } 259 260 264 public Path getSourcepath() { 265 return compileSourcepath; 266 } 267 268 272 public Path createSourcepath() { 273 if (compileSourcepath == null) { 274 compileSourcepath = new Path(getProject()); 275 } 276 return compileSourcepath.createPath(); 277 } 278 279 283 public void setSourcepathRef(Reference r) { 284 createSourcepath().setRefid(r); 285 } 286 287 292 public void setClasspath(Path classpath) { 293 if (compileClasspath == null) { 294 compileClasspath = classpath; 295 } else { 296 compileClasspath.append(classpath); 297 } 298 } 299 300 304 public Path getClasspath() { 305 return compileClasspath; 306 } 307 308 312 public Path createClasspath() { 313 if (compileClasspath == null) { 314 compileClasspath = new Path(getProject()); 315 } 316 return compileClasspath.createPath(); 317 } 318 319 323 public void setClasspathRef(Reference r) { 324 createClasspath().setRefid(r); 325 } 326 327 333 public void setBootclasspath(Path bootclasspath) { 334 if (this.bootclasspath == null) { 335 this.bootclasspath = bootclasspath; 336 } else { 337 this.bootclasspath.append(bootclasspath); 338 } 339 } 340 341 346 public Path getBootclasspath() { 347 return bootclasspath; 348 } 349 350 354 public Path createBootclasspath() { 355 if (bootclasspath == null) { 356 bootclasspath = new Path(getProject()); 357 } 358 return bootclasspath.createPath(); 359 } 360 361 365 public void setBootClasspathRef(Reference r) { 366 createBootclasspath().setRefid(r); 367 } 368 369 374 public void setExtdirs(Path extdirs) { 375 if (this.extdirs == null) { 376 this.extdirs = extdirs; 377 } else { 378 this.extdirs.append(extdirs); 379 } 380 } 381 382 387 public Path getExtdirs() { 388 return extdirs; 389 } 390 391 395 public Path createExtdirs() { 396 if (extdirs == null) { 397 extdirs = new Path(getProject()); 398 } 399 return extdirs.createPath(); 400 } 401 402 406 public void setListfiles(boolean list) { 407 listFiles = list; 408 } 409 410 414 public boolean getListfiles() { 415 return listFiles; 416 } 417 418 423 public void setFailonerror(boolean fail) { 424 failOnError = fail; 425 } 426 427 431 public void setProceed(boolean proceed) { 432 failOnError = !proceed; 433 } 434 435 439 public boolean getFailonerror() { 440 return failOnError; 441 } 442 443 448 public void setDeprecation(boolean deprecation) { 449 this.deprecation = deprecation; 450 } 451 452 456 public boolean getDeprecation() { 457 return deprecation; 458 } 459 460 467 public void setMemoryInitialSize(String memoryInitialSize) { 468 this.memoryInitialSize = memoryInitialSize; 469 } 470 471 475 public String getMemoryInitialSize() { 476 return memoryInitialSize; 477 } 478 479 486 public void setMemoryMaximumSize(String memoryMaximumSize) { 487 this.memoryMaximumSize = memoryMaximumSize; 488 } 489 490 494 public String getMemoryMaximumSize() { 495 return memoryMaximumSize; 496 } 497 498 502 public void setEncoding(String encoding) { 503 this.encoding = encoding; 504 } 505 506 510 public String getEncoding() { 511 return encoding; 512 } 513 514 519 public void setDebug(boolean debug) { 520 this.debug = debug; 521 } 522 523 527 public boolean getDebug() { 528 return debug; 529 } 530 531 535 public void setOptimize(boolean optimize) { 536 this.optimize = optimize; 537 } 538 539 543 public boolean getOptimize() { 544 return optimize; 545 } 546 547 552 public void setDepend(boolean depend) { 553 this.depend = depend; 554 } 555 556 560 public boolean getDepend() { 561 return depend; 562 } 563 564 568 public void setVerbose(boolean verbose) { 569 this.verbose = verbose; 570 } 571 572 576 public boolean getVerbose() { 577 return verbose; 578 } 579 580 586 public void setTarget(String target) { 587 this.targetAttribute = target; 588 } 589 590 594 public String getTarget() { 595 return targetAttribute != null 596 ? targetAttribute 597 : getProject().getProperty(MagicNames.BUILD_JAVAC_TARGET); 598 } 599 600 604 public void setIncludeantruntime(boolean include) { 605 includeAntRuntime = include; 606 } 607 608 612 public boolean getIncludeantruntime() { 613 return includeAntRuntime; 614 } 615 616 620 public void setIncludejavaruntime(boolean include) { 621 includeJavaRuntime = include; 622 } 623 624 629 public boolean getIncludejavaruntime() { 630 return includeJavaRuntime; 631 } 632 633 638 public void setFork(boolean f) { 639 fork = f; 640 } 641 642 649 public void setExecutable(String forkExec) { 650 forkedExecutable = forkExec; 651 } 652 653 659 public String getExecutable() { 660 return forkedExecutable; 661 } 662 663 667 public boolean isForkedJavac() { 668 return fork || "extJavac".equals(getCompiler()); 669 } 670 671 683 public String getJavacExecutable() { 684 if (forkedExecutable == null && isForkedJavac()) { 685 forkedExecutable = getSystemJavac(); 686 } else if (forkedExecutable != null && !isForkedJavac()) { 687 forkedExecutable = null; 688 } 689 return forkedExecutable; 690 } 691 692 696 public void setNowarn(boolean flag) { 697 this.nowarn = flag; 698 } 699 700 704 public boolean getNowarn() { 705 return nowarn; 706 } 707 708 712 public ImplementationSpecificArgument createCompilerArg() { 713 ImplementationSpecificArgument arg = 714 new ImplementationSpecificArgument(); 715 facade.addImplementationArgument(arg); 716 return arg; 717 } 718 719 723 public String [] getCurrentCompilerArgs() { 724 String chosen = facade.getExplicitChoice(); 725 try { 726 String appliedCompiler = getCompiler(); 728 facade.setImplementation(appliedCompiler); 729 730 String [] result = facade.getArgs(); 731 732 String altCompilerName = getAltCompilerName(facade.getImplementation()); 733 734 if (result.length == 0 && altCompilerName != null) { 735 facade.setImplementation(altCompilerName); 736 result = facade.getArgs(); 737 } 738 739 return result; 740 741 } finally { 742 facade.setImplementation(chosen); 743 } 744 } 745 746 private String getAltCompilerName(String anImplementation) { 747 if (JAVAC16.equalsIgnoreCase(anImplementation) 748 || JAVAC15.equalsIgnoreCase(anImplementation) 749 || JAVAC14.equalsIgnoreCase(anImplementation) 750 || JAVAC13.equalsIgnoreCase(anImplementation)) { 751 return MODERN; 752 } 753 if (JAVAC12.equalsIgnoreCase(anImplementation) 754 || JAVAC11.equalsIgnoreCase(anImplementation)) { 755 return CLASSIC; 756 } 757 if (MODERN.equalsIgnoreCase(anImplementation)) { 758 String nextSelected = assumedJavaVersion(); 759 if (JAVAC16.equalsIgnoreCase(nextSelected) 760 || JAVAC15.equalsIgnoreCase(nextSelected) 761 || JAVAC14.equalsIgnoreCase(nextSelected) 762 || JAVAC13.equalsIgnoreCase(nextSelected)) { 763 return nextSelected; 764 } 765 } 766 if (CLASSIC.equals(anImplementation)) { 767 return assumedJavaVersion(); 768 } 769 if (EXTJAVAC.equalsIgnoreCase(anImplementation)) { 770 return assumedJavaVersion(); 771 } 772 return null; 773 } 774 775 781 public void setTempdir(File tmpDir) { 782 this.tmpDir = tmpDir; 783 } 784 785 791 public File getTempdir() { 792 return tmpDir; 793 } 794 795 799 public void execute() throws BuildException { 800 checkParameters(); 801 resetFileLists(); 802 803 String [] list = src.list(); 806 for (int i = 0; i < list.length; i++) { 807 File srcDir = getProject().resolveFile(list[i]); 808 if (!srcDir.exists()) { 809 throw new BuildException("srcdir \"" 810 + srcDir.getPath() 811 + "\" does not exist!", getLocation()); 812 } 813 814 DirectoryScanner ds = this.getDirectoryScanner(srcDir); 815 String [] files = ds.getIncludedFiles(); 816 817 scanDir(srcDir, destDir != null ? destDir : srcDir, files); 818 } 819 820 compile(); 821 } 822 823 826 protected void resetFileLists() { 827 compileList = new File [0]; 828 } 829 830 838 protected void scanDir(File srcDir, File destDir, String [] files) { 839 GlobPatternMapper m = new GlobPatternMapper(); 840 m.setFrom("*.java"); 841 m.setTo("*.class"); 842 SourceFileScanner sfs = new SourceFileScanner(this); 843 File [] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m); 844 845 if (newFiles.length > 0) { 846 File [] newCompileList 847 = new File [compileList.length + newFiles.length]; 848 System.arraycopy(compileList, 0, newCompileList, 0, 849 compileList.length); 850 System.arraycopy(newFiles, 0, newCompileList, 851 compileList.length, newFiles.length); 852 compileList = newCompileList; 853 } 854 } 855 856 860 public File [] getFileList() { 861 return compileList; 862 } 863 864 872 protected boolean isJdkCompiler(String compilerImpl) { 873 return MODERN.equals(compilerImpl) 874 || CLASSIC.equals(compilerImpl) 875 || JAVAC16.equals(compilerImpl) 876 || JAVAC15.equals(compilerImpl) 877 || JAVAC14.equals(compilerImpl) 878 || JAVAC13.equals(compilerImpl) 879 || JAVAC12.equals(compilerImpl) 880 || JAVAC11.equals(compilerImpl); 881 } 882 883 886 protected String getSystemJavac() { 887 return JavaEnvUtils.getJdkExecutable("javac"); 888 } 889 890 895 public void setCompiler(String compiler) { 896 facade.setImplementation(compiler); 897 } 898 899 913 public String getCompiler() { 914 String compilerImpl = getCompilerVersion(); 915 if (fork) { 916 if (isJdkCompiler(compilerImpl)) { 917 compilerImpl = "extJavac"; 918 } else { 919 log("Since compiler setting isn't classic or modern," 920 + "ignoring fork setting.", Project.MSG_WARN); 921 } 922 } 923 return compilerImpl; 924 } 925 926 940 public String getCompilerVersion() { 941 facade.setMagicValue(getProject().getProperty("build.compiler")); 942 return facade.getImplementation(); 943 } 944 945 952 protected void checkParameters() throws BuildException { 953 if (src == null) { 954 throw new BuildException("srcdir attribute must be set!", 955 getLocation()); 956 } 957 if (src.size() == 0) { 958 throw new BuildException("srcdir attribute must be set!", 959 getLocation()); 960 } 961 962 if (destDir != null && !destDir.isDirectory()) { 963 throw new BuildException("destination directory \"" 964 + destDir 965 + "\" does not exist " 966 + "or is not a directory", getLocation()); 967 } 968 } 969 970 975 protected void compile() { 976 String compilerImpl = getCompiler(); 977 978 if (compileList.length > 0) { 979 log("Compiling " + compileList.length + " source file" 980 + (compileList.length == 1 ? "" : "s") 981 + (destDir != null ? " to " + destDir : "")); 982 983 if (listFiles) { 984 for (int i = 0; i < compileList.length; i++) { 985 String filename = compileList[i].getAbsolutePath(); 986 log(filename); 987 } 988 } 989 990 CompilerAdapter adapter = 991 CompilerAdapterFactory.getCompiler(compilerImpl, this); 992 993 adapter.setJavac(this); 995 996 if (!adapter.execute()) { 998 if (failOnError) { 999 throw new BuildException(FAIL_MSG, getLocation()); 1000 } else { 1001 log(FAIL_MSG, Project.MSG_ERR); 1002 } 1003 } 1004 } 1005 } 1006 1007 1012 public class ImplementationSpecificArgument extends 1013 org.apache.tools.ant.util.facade.ImplementationSpecificArgument { 1014 1015 1018 public void setCompiler(String impl) { 1019 super.setImplementation(impl); 1020 } 1021 } 1022 1023} 1024 | Popular Tags |