From c1bbf2fc0ca870384cdec8f437fb1fb9a4699db9 Mon Sep 17 00:00:00 2001
From: Stefan Bodewig
--release
switch.Ignored
+ when running on JDK < 9.@@ -121,6 +122,7 @@ public class Javac extends MatchingTask { private boolean depend = false; private boolean verbose = false; private String targetAttribute; + private String release; private Path bootclasspath; private Path extdirs; private Boolean includeAntRuntime; @@ -787,6 +789,32 @@ public class Javac extends MatchingTask { : getProject().getProperty(MagicNames.BUILD_JAVAC_TARGET); } + /** + * Sets the version to use for the {@code --release} switch that + * combines {@code source}, {@code target} and setting the + * bootclasspath. + * + * Values depend on the compiler, for jdk 9 the valid values are + * "6", "7", "8", "9". + * @param release the value of the release attribute + * @since Ant 1.9.8 + */ + public void setRelease(final String release) { + this.release = release; + } + + /** + * Gets the version to use for the {@code --release} switch that + * combines {@code source}, {@code target} and setting the + * bootclasspath. + * + * @return the value of the release attribute + * @since Ant 1.9.8 + */ + public String getRelease() { + return release; + } + /** * If true, includes Ant's own classpath in the classpath. * @param include if true, includes Ant's own classpath in the classpath diff --git a/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java b/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java index 827b40206..82373a9b3 100644 --- a/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java +++ b/src/main/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapter.java @@ -70,6 +70,7 @@ public abstract class DefaultCompilerAdapter protected boolean depend = false; protected boolean verbose = false; protected String target; + protected String release; protected Path bootclasspath; protected Path extdirs; protected Path compileClasspath; @@ -111,6 +112,7 @@ public abstract class DefaultCompilerAdapter depend = attributes.getDepend(); verbose = attributes.getVerbose(); target = attributes.getTarget(); + release = attributes.getRelease(); bootclasspath = attributes.getBootclasspath(); extdirs = attributes.getExtdirs(); compileList = attributes.getFileList(); @@ -321,15 +323,17 @@ public abstract class DefaultCompilerAdapter cmd.createArgument().setValue("-sourcepath"); cmd.createArgument().setPath(sourcepath); } - if (target != null) { - cmd.createArgument().setValue("-target"); - cmd.createArgument().setValue(target); - } + if (release == null || !assumeJava19()) { + if (target != null) { + cmd.createArgument().setValue("-target"); + cmd.createArgument().setValue(target); + } - final Path bp = getBootClassPath(); - if (bp.size() > 0) { - cmd.createArgument().setValue("-bootclasspath"); - cmd.createArgument().setPath(bp); + final Path bp = getBootClassPath(); + if (bp.size() > 0) { + cmd.createArgument().setValue("-bootclasspath"); + cmd.createArgument().setPath(bp); + } } if (extdirs != null && extdirs.size() > 0) { @@ -390,13 +394,27 @@ public abstract class DefaultCompilerAdapter setupJavacCommandlineSwitches(cmd, true); if (!assumeJava13()) { // -source added with JDK 1.4 final String t = attributes.getTarget(); - if (attributes.getSource() != null) { - cmd.createArgument().setValue("-source"); - cmd.createArgument() - .setValue(adjustSourceValue(attributes.getSource())); + final String s = attributes.getSource(); + if (release == null || !assumeJava19()) { + if (release != null) { + attributes.log("Support for javac --release has been added" + + " in Java9 ignoring it"); + } + if (s != null) { + cmd.createArgument().setValue("-source"); + cmd.createArgument().setValue(adjustSourceValue(s)); - } else if (t != null && mustSetSourceForTarget(t)) { - setImplicitSourceSwitch(cmd, t, adjustSourceValue(t)); + } else if (t != null && mustSetSourceForTarget(t)) { + setImplicitSourceSwitch(cmd, t, adjustSourceValue(t)); + } + } else { // Java 9+ and release has been set + if (t != null || s != null || getBootClassPath().size() > 0) { + attributes.log("Ignoring source, target and bootclasspath" + + " as release has been set", + Project.MSG_WARN); + } + cmd.createArgument().setValue("--release"); + cmd.createArgument().setValue(release); } } final Path msp = getModulesourcepath(); diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java index 7ed5b3fab..cb274a804 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java @@ -68,6 +68,18 @@ public class DefaultCompilerAdapterTest { } } + private static class SourceTargetHelperNoOverride extends DefaultCompilerAdapter { + + public boolean execute() { return false; } + + /** + * public to avoid classloader issues. + */ + public Commandline setupModernJavacCommandlineSwitches(Commandline cmd) { + return super.setupModernJavacCommandlineSwitches(cmd); + } + } + @Test public void testSourceIsIgnoredForJavac13() { testSource(null, "javac1.3", "", null, "1.1"); @@ -384,6 +396,56 @@ public class DefaultCompilerAdapterTest { } } + @Test + public void releaseIsIgnoredForJava8() { + LogCapturingJavac javac = new LogCapturingJavac(); + Project p = new Project(); + javac.setProject(p); + javac.setCompiler("javac8"); + javac.setSource("6"); + javac.setTarget("6"); + javac.setRelease("6"); + javac.setSourcepath(new Path(p)); + SourceTargetHelperNoOverride sth = new SourceTargetHelperNoOverride(); + sth.setJavac(javac); + Commandline cmd = new Commandline(); + sth.setupModernJavacCommandlineSwitches(cmd); + assertContains("Support for javac --release has been added in " + + "Java9 ignoring it", javac.getLog()); + String[] args = cmd.getCommandline(); + assertEquals(7, args.length); + assertEquals("-classpath", args[0]); + assertEquals("-target", args[2]); + assertEquals("6", args[3]); + assertEquals("-g:none", args[4]); + assertEquals("-source", args[5]); + assertEquals("6", args[6]); + } + + @Test + public void releaseIsUsedForJava9() { + LogCapturingJavac javac = new LogCapturingJavac(); + Project p = new Project(); + javac.setProject(p); + javac.setCompiler("javac9"); + javac.setSource("6"); + javac.setTarget("6"); + javac.setRelease("6"); + javac.setSourcepath(new Path(p)); + SourceTargetHelperNoOverride sth = new SourceTargetHelperNoOverride(); + sth.setJavac(javac); + Commandline cmd = new Commandline(); + sth.setupModernJavacCommandlineSwitches(cmd); + assertContains("Ignoring source, target and bootclasspath as " + + "release has been set", javac.getLog()); + String[] args = cmd.getCommandline(); + assertEquals(5, args.length); + assertEquals("-classpath", args[0]); + assertEquals("-g:none", args[2]); + assertEquals("--release", args[3]); + assertEquals("6", args[4]); + } + private void commonSourceDowngrades(String javaVersion) { testSource("1.3", javaVersion, "If you specify -target 1.1 you now must also specify"