From 022ed20a99a57093f9fc6335aff8234d407c826e Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 15 Feb 2002 13:42:53 +0000 Subject: [PATCH] Add compiler attribute to that allows users to override build.compiler on a task by task basis. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271362 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 4 ++ docs/manual/CoreTasks/javac.html | 8 ++- .../org/apache/tools/ant/taskdefs/Javac.java | 72 +++++++++++++------ .../apache/tools/ant/taskdefs/JavacTest.java | 42 ++++++++++- 4 files changed, 102 insertions(+), 24 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 8e8d9d0b6..76722cac1 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -163,6 +163,10 @@ Other changes: * you can now specify the -sourcepath for explicitly. +* The compiler implementation for javac can now be chosen on a task by + task basis. Use the new compiler attribute of to override + the build.compiler property. + Changes from Ant 1.4 to Ant 1.4.1 =========================================== diff --git a/docs/manual/CoreTasks/javac.html b/docs/manual/CoreTasks/javac.html index 25cd92266..23dd77587 100644 --- a/docs/manual/CoreTasks/javac.html +++ b/docs/manual/CoreTasks/javac.html @@ -29,7 +29,7 @@ want to use default exclusions or not. See the section on directory based tasks, on how the inclusion/exclusion of files works, and how to write patterns.

It is possible to use different compilers. This can be selected with the -"build.compiler" property. Here are the choices:-

+"build.compiler" property or the compiler attribute. Here are the choices:-

  • classic (the standard compiler of JDK 1.1/1.2) - javac1.1 and javac1.2 can be used as aliases
  • @@ -259,6 +259,12 @@ invoking the compiler.

    No + + compiler + The compiler implementation to use. + Defaults to the value of the build.compiler property. + No +

    Parameters specified as nested elements

    diff --git a/src/main/org/apache/tools/ant/taskdefs/Javac.java b/src/main/org/apache/tools/ant/taskdefs/Javac.java index f7a67669a..ee452995c 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Javac.java +++ b/src/main/org/apache/tools/ant/taskdefs/Javac.java @@ -90,6 +90,7 @@ import java.util.Vector; *
  • includeantruntime *
  • includejavaruntime *
  • source + *
  • compiler *
* Of these arguments, the sourcedir and destdir are required. *

@@ -136,6 +137,15 @@ public class Javac extends MatchingTask { private String source; private String debugLevel; + /** + * The compiler set via the compiler attribute. + * + *

default is null

+ * + * @since 1.84, Ant 1.5 + */ + private String compiler = null; + /** * Get the value of debugLevel. * @return value of debugLevel. @@ -544,8 +554,7 @@ public class Javac extends MatchingTask { * Is this a forked invocation of JDK's javac? */ public boolean isForkedJavac() { - return !"false".equals(fork) || - "extJavac".equals(project.getProperty("build.compiler")); + return !"false".equals(fork) || "extJavac".equals(getCompiler()); } /** @@ -665,13 +674,13 @@ public class Javac extends MatchingTask { return compileList; } - protected boolean isJdkCompiler(String compiler) { - return "modern".equals(compiler) || - "classic".equals(compiler) || - "javac1.1".equals(compiler) || - "javac1.2".equals(compiler) || - "javac1.3".equals(compiler) || - "javac1.4".equals(compiler); + protected boolean isJdkCompiler(String compilerImpl) { + return "modern".equals(compilerImpl) || + "classic".equals(compilerImpl) || + "javac1.1".equals(compilerImpl) || + "javac1.2".equals(compilerImpl) || + "javac1.3".equals(compilerImpl) || + "javac1.4".equals(compilerImpl); } protected String getSystemJavac() { @@ -705,34 +714,53 @@ public class Javac extends MatchingTask { } } - private String determineCompiler() { - String compiler = project.getProperty("build.compiler"); + /** + * Chose the implementation for this particular task. + * + * @since 1.84, Ant 1.5 + */ + public void setCompiler(String compiler) { + this.compiler = compiler; + } + + /** + * The implementation for this particular task. + * + *

Defaults to the build.compiler property but can be overriden + * via the compiler and for attributes.

+ * + * @since 1.84, Ant 1.5 + */ + public String getCompiler() { + String compilerImpl = + this.compiler != null ? this.compiler + : project.getProperty("build.compiler"); if (!"false".equals(fork)) { - if (compiler != null) { - if (isJdkCompiler(compiler)) { + if (compilerImpl != null) { + if (isJdkCompiler(compilerImpl)) { log("Since fork is true, ignoring build.compiler setting.", Project.MSG_WARN); - compiler = "extJavac"; + compilerImpl = "extJavac"; } else { log("Since build.compiler setting isn't classic or modern, ignoring fork setting.", Project.MSG_WARN); } } else { - compiler = "extJavac"; + compilerImpl = "extJavac"; } } - if (compiler == null) { + if (compilerImpl == null) { if (Project.getJavaVersion() != Project.JAVA_1_1 && Project.getJavaVersion() != Project.JAVA_1_2) { - compiler = "modern"; + compilerImpl = "modern"; } else { - compiler = "classic"; + compilerImpl = "classic"; } } - return compiler; + return compilerImpl; } /** @@ -765,7 +793,7 @@ public class Javac extends MatchingTask { * @since 1.82, Ant 1.5 */ protected void compile() { - String compiler = determineCompiler(); + String compilerImpl = getCompiler(); if (compileList.length > 0) { log("Compiling " + compileList.length + @@ -774,7 +802,7 @@ public class Javac extends MatchingTask { + (destDir != null ? " to " + destDir : "")); CompilerAdapter adapter = - CompilerAdapterFactory.getCompiler(compiler, this); + CompilerAdapterFactory.getCompiler(compilerImpl, this); // now we need to populate the compiler adapter adapter.setJavac(this); @@ -805,7 +833,7 @@ public class Javac extends MatchingTask { } public String[] getParts() { - if (impl == null || impl.equals(determineCompiler())) { + if (impl == null || impl.equals(getCompiler())) { return super.getParts(); } else { return new String[0]; diff --git a/src/testcases/org/apache/tools/ant/taskdefs/JavacTest.java b/src/testcases/org/apache/tools/ant/taskdefs/JavacTest.java index c01f5d20a..3736c0c73 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/JavacTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/JavacTest.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2001 The Apache Software Foundation. All rights + * Copyright (c) 2001-2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -167,4 +167,44 @@ public class JavacTest extends TestCase { assertEquals(testArg, args[0]); } + /** + * Test compiler attribute. + */ + public void testCompilerAttribute() { + // check defaults + String compiler = javac.getCompiler(); + assertNotNull(compiler); + assertTrue("default value", + "modern".equals(compiler) || "classic".equals(compiler)); + + javac.setFork("true"); + compiler = javac.getCompiler(); + assertNotNull(compiler); + assertEquals("extJavac", compiler); + + // check build.compiler provides defaults + javac.setFork("false"); + project.setNewProperty("build.compiler", "jikes"); + compiler = javac.getCompiler(); + assertNotNull(compiler); + assertEquals("jikes", compiler); + + javac.setFork("true"); + compiler = javac.getCompiler(); + assertNotNull(compiler); + assertEquals("jikes", compiler); + + // check attribute overrides build.compiler + javac.setFork("false"); + javac.setCompiler("jvc"); + compiler = javac.getCompiler(); + assertNotNull(compiler); + assertEquals("jvc", compiler); + + javac.setFork("true"); + compiler = javac.getCompiler(); + assertNotNull(compiler); + assertEquals("jvc", compiler); + } + }