@@ -122,6 +122,10 @@ Other changes: | |||
if when a SecurityManager is active. | |||
Bugzilla Report 60060 | |||
* support for javac's --release switch introduced with Java9 has been | |||
added. | |||
Bugzilla Report 60172 | |||
Changes from Ant 1.9.6 TO Ant 1.9.7 | |||
=================================== | |||
@@ -522,6 +522,16 @@ invoking the compiler.</p> | |||
<em>Since Ant 1.9.8</em>. | |||
<td align="center" valign="top">No</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">release</td> | |||
<td valign="top"> | |||
Specify the value for the <code>--release</code> switch.Ignored | |||
when running on JDK < 9.<br> | |||
When set and running on JDK >= 9 the source and target | |||
attributes as well as the bootclasspath will be ignored. | |||
<em>Since Ant 1.9.8</em>. | |||
<td align="center" valign="top">No</td> | |||
</tr> | |||
</table> | |||
<h3>Parameters specified as nested elements</h3> | |||
@@ -67,6 +67,7 @@ import org.apache.tools.ant.util.facade.FacadeTaskHelper; | |||
* <li>includejavaruntime | |||
* <li>source | |||
* <li>compiler | |||
* <li>release | |||
* </ul> | |||
* Of these arguments, the <b>sourcedir</b> and <b>destdir</b> are required. | |||
* <p> | |||
@@ -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 | |||
@@ -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(); | |||
@@ -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"); | |||
@@ -387,6 +399,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" | |||