diff --git a/WHATSNEW b/WHATSNEW index 5ceb69f67..693a6feb2 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -376,7 +376,11 @@ Other changes: dare not change for fear of breaking complex SQL operations in existing files. -* 's packagenames attribute is now optional and default to "*". +* 's packagenames attribute is now optional and defaults to "*". + +* 's source and target attributes as well as 's source + attribute will read default values from the properties + ant.build.javac.source and ant.build.javac.target. Changes from Ant 1.6.4 to Ant 1.6.5 =================================== diff --git a/docs/manual/CoreTasks/javac.html b/docs/manual/CoreTasks/javac.html index 910cf87c3..f91ffae65 100644 --- a/docs/manual/CoreTasks/javac.html +++ b/docs/manual/CoreTasks/javac.html @@ -241,7 +241,12 @@ invoking the compiler.

particular, if you use JDK 1.4+ the generated classes will not be usable for a 1.1 Java VM unless you explicitly set this attribute to the value 1.1 (which is the default value for JDK 1.1 to - 1.3). We highly recommend to always specify this attribute. + 1.3). We highly recommend to always specify this + attribute.
+ A default value for this attribute can be provided using the magic + ant.build.javac.target + property. No @@ -322,7 +327,11 @@ invoking the compiler.

at all.
Note that the default value depends on the JVM that is running Ant. We highly recommend to always specify this - attribute. + attribute.
+ A default value for this attribute can be provided using the magic + ant.build.javac.source + property. No diff --git a/docs/manual/CoreTasks/javadoc.html b/docs/manual/CoreTasks/javadoc.html index 5976e1dea..da4138f0a 100644 --- a/docs/manual/CoreTasks/javadoc.html +++ b/docs/manual/CoreTasks/javadoc.html @@ -407,7 +407,11 @@ to ensure that this command supports the attributes you wish to use.

Necessary to enable javadoc to handle assertions present in J2SE v 1.4 source code. Set this to "1.4" to documents code that compiles using "javac -source - 1.4". + 1.4".
+ A default value for this attribute can be provided using the magic + ant.build.javac.source + property. 1.4+ No diff --git a/docs/manual/conceptstypeslist.html b/docs/manual/conceptstypeslist.html index 3daa3fee2..c96a786c9 100644 --- a/docs/manual/conceptstypeslist.html +++ b/docs/manual/conceptstypeslist.html @@ -14,6 +14,7 @@

Concepts

ant.build.clonevm
build.sysclasspath
+Ant properties controlling javac
Common Attributes

Core Types

diff --git a/docs/manual/javacprops.html b/docs/manual/javacprops.html new file mode 100644 index 000000000..e52ca36c2 --- /dev/null +++ b/docs/manual/javacprops.html @@ -0,0 +1,39 @@ + + + + + +Properties controlling javac + + + + +

The source and target attributes of <javac> +don't have any default values for historical reasons. Since the +underlying javac compiler's default depends on the JDK you use, you +may encounter build files that don't explicitly set those attributes +and that will no longer compile using a newer JDK. If you cannot +change the build file, Ant provides two properties that help you +setting default values for these attributes. If the attributes have +been set explicitly, the properties listed here will be ignored.

+ +

ant.build.javac.source

+ +

Since Ant 1.7

+ +

Provides a default value for <javac>'s and +<javadoc>'s source attribute.

+ +

ant.build.javac.target

+ +

Since Ant 1.7

+ +

Provides a default value for <javac>'s target +attribute.

+ +
+

Copyright © 2006 The Apache Software Foundation. All rights +Reserved.

+ + + diff --git a/src/main/org/apache/tools/ant/MagicNames.java b/src/main/org/apache/tools/ant/MagicNames.java index c02573c13..64fadd7b1 100644 --- a/src/main/org/apache/tools/ant/MagicNames.java +++ b/src/main/org/apache/tools/ant/MagicNames.java @@ -128,5 +128,21 @@ public final class MagicNames { */ public static final String REGEXP_IMPL = "ant.regexp.regexpimpl"; + /** + * property that provides the default value for javac's and + * javadoc's source attribute. + * @since Ant 1.7 + * Value: {@value} + */ + public static final String BUILD_JAVAC_SOURCE = "ant.build.javac.source"; + + /** + * property that provides the default value for javac's target + * attribute. + * @since Ant 1.7 + * Value: {@value} + */ + public static final String BUILD_JAVAC_TARGET = "ant.build.javac.target"; + } diff --git a/src/main/org/apache/tools/ant/taskdefs/Javac.java b/src/main/org/apache/tools/ant/taskdefs/Javac.java index 6aac19256..9c4e9b2e0 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Javac.java +++ b/src/main/org/apache/tools/ant/taskdefs/Javac.java @@ -20,6 +20,7 @@ package org.apache.tools.ant.taskdefs; import java.io.File; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; +import org.apache.tools.ant.MagicNames; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.compilers.CompilerAdapter; import org.apache.tools.ant.taskdefs.compilers.CompilerAdapterFactory; @@ -161,7 +162,8 @@ public class Javac extends MatchingTask { * @return value of source. */ public String getSource() { - return source; + return source != null + ? source : getProject().getProperty(MagicNames.BUILD_JAVAC_SOURCE); } /** @@ -586,7 +588,9 @@ public class Javac extends MatchingTask { * @return the target VM */ public String getTarget() { - return targetAttribute; + return targetAttribute != null + ? targetAttribute + : getProject().getProperty(MagicNames.BUILD_JAVAC_TARGET); } /** diff --git a/src/main/org/apache/tools/ant/taskdefs/Javadoc.java b/src/main/org/apache/tools/ant/taskdefs/Javadoc.java index 83b204eec..f21e945a9 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Javadoc.java +++ b/src/main/org/apache/tools/ant/taskdefs/Javadoc.java @@ -33,6 +33,7 @@ import java.util.StringTokenizer; import java.util.Vector; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; +import org.apache.tools.ant.MagicNames; import org.apache.tools.ant.Project; import org.apache.tools.ant.ProjectComponent; import org.apache.tools.ant.Task; @@ -1912,9 +1913,11 @@ public class Javadoc extends Task { } } - if (source != null) { + String sourceArg = source != null ? source + : getProject().getProperty(MagicNames.BUILD_JAVAC_SOURCE); + if (sourceArg != null) { toExecute.createArgument().setValue("-source"); - toExecute.createArgument().setValue(source); + toExecute.createArgument().setValue(sourceArg); } if (linksource && doclet == null) { diff --git a/src/testcases/org/apache/tools/ant/taskdefs/JavacTest.java b/src/testcases/org/apache/tools/ant/taskdefs/JavacTest.java index 279873fb9..ae2a83ebf 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/JavacTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/JavacTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2001-2002,2004-2005 The Apache Software Foundation + * Copyright 2001-2002,2004-2006 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -212,4 +212,33 @@ public class JavacTest extends TestCase { assertTrue(adapter instanceof JavacExternal); } + public void testSourceNoDefault() { + assertNull(javac.getSource()); + } + + public void testSourceWithDefault() { + project.setNewProperty("ant.build.javac.source", "1.4"); + assertEquals("1.4", javac.getSource()); + } + + public void testSourceOverridesDefault() { + project.setNewProperty("ant.build.javac.source", "1.4"); + javac.setSource("1.5"); + assertEquals("1.5", javac.getSource()); + } + + public void testTargetNoDefault() { + assertNull(javac.getTarget()); + } + + public void testTargetWithDefault() { + project.setNewProperty("ant.build.javac.target", "1.4"); + assertEquals("1.4", javac.getTarget()); + } + + public void testTargetOverridesDefault() { + project.setNewProperty("ant.build.javac.target", "1.4"); + javac.setTarget("1.5"); + assertEquals("1.5", javac.getTarget()); + } }