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.
-*
ant.build.javac.target
+ property.
ant.build.javac.source
+ property.
"javac -source
- 1.4"
.ant.build.javac.source
+ property.
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.
Since Ant 1.7
+ +Provides a default value for <javac>
's and
+<javadoc>
's source attribute.
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()); + } }