diff --git a/src/main/java/scala_maven/ScalaMojoSupport.java b/src/main/java/scala_maven/ScalaMojoSupport.java index 54983ee8..97e39d31 100644 --- a/src/main/java/scala_maven/ScalaMojoSupport.java +++ b/src/main/java/scala_maven/ScalaMojoSupport.java @@ -105,7 +105,7 @@ public abstract class ScalaMojoSupport extends AbstractMojo { /** Scala 's version to use. (property 'maven.scala.version' replaced by 'scala.version') */ @Parameter(property = "scala.version") - private String scalaVersion; + protected String scalaVersion; /** * Organization/group ID of the Scala used in the project. Default value is 'org.scala-lang'. This @@ -497,11 +497,15 @@ private DependencyNodeFilter createScalaDistroDependencyFilter() throws Exceptio final JavaMainCaller getScalaCommand(final boolean forkOverride, final String mainClass) throws Exception { JavaMainCaller cmd = getEmptyScalaCommand(mainClass, forkOverride); + populateArgs(cmd); + return cmd; + } + + final void populateArgs(final JavaMainCaller cmd) throws Exception { for (String option : getScalacOptions()) { cmd.addArgs(option); } cmd.addJvmArgs(jvmArgs); - return cmd; } /** diff --git a/src/test/java/scala_maven/ScalaMojoSupportTest.java b/src/test/java/scala_maven/ScalaMojoSupportTest.java index 6cfffeda..264c84ad 100644 --- a/src/test/java/scala_maven/ScalaMojoSupportTest.java +++ b/src/test/java/scala_maven/ScalaMojoSupportTest.java @@ -5,9 +5,15 @@ package scala_maven; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import java.util.List; import org.junit.Test; +import scala_maven_executions.JavaMainCallerSupport; +import scala_maven_executions.SpawnMonitor; public class ScalaMojoSupportTest { @@ -60,4 +66,87 @@ public void scala2_13_should_generate_non_prefixed_target() { assertEquals("11", ScalaMojoSupport.targetOption("11", new VersionNumber("2.13.8"))); assertEquals("17", ScalaMojoSupport.targetOption("17", new VersionNumber("2.13.8"))); } + + static class ScalaMojoSupportWithRelease extends ScalaMojoSupport { + public ScalaMojoSupportWithRelease() { + this.release = "42"; + this.target = "8"; + } + + public List getScalacOptions() throws Exception { + return super.getScalacOptions(); + } + + public void setScalaVersion(final String v) { + this.scalaVersion = v; + } + + @Override + protected void doExecute() throws Exception {} + } + + static class JavaMainCallerArgs extends JavaMainCallerSupport { + public JavaMainCallerArgs() { + super(null, null, null, null, null); + } + + public List getArgs() { + return this.args; + } + + @Override + public SpawnMonitor spawn(boolean displayCmd) throws Exception { + return null; + } + + @Override + public boolean run(boolean displayCmd, boolean throwFailure) throws Exception { + return false; + } + + @Override + public void redirectToLog() {} + } + + final ScalaMojoSupportWithRelease mojoWithRelease = new ScalaMojoSupportWithRelease(); + + @Test + public void scala2_11_should_skip_release_option() throws Exception { + mojoWithRelease.setScalaVersion("2.11.0"); + List opts = mojoWithRelease.getScalacOptions(); + assertNotNull(opts); + assertFalse(opts.contains("-release")); + assertFalse(opts.contains("42")); + } + + @Test + public void scala2_12_should_skip_release_option() throws Exception { + mojoWithRelease.setScalaVersion("2.12.0"); + List opts = mojoWithRelease.getScalacOptions(); + assertNotNull(opts); + assertTrue(opts.contains("-release")); + assertTrue(opts.contains("42")); + } + + @Test + public void scala2_13_should_keep_release_option() throws Exception { + mojoWithRelease.setScalaVersion("2.13.0"); + List opts = mojoWithRelease.getScalacOptions(); + assertNotNull(opts); + assertTrue(opts.contains("-release")); + assertTrue(opts.contains("42")); + } + + @Test + public void scala2_12_scala_command_contain_target_and_release() throws Exception { + mojoWithRelease.setScalaVersion("2.12.0"); + final JavaMainCallerArgs caller = new JavaMainCallerArgs(); + mojoWithRelease.populateArgs(caller); + + assertNotNull(caller.getArgs()); + assertEquals(3, caller.getArgs().size()); + assertTrue(caller.getArgs().contains("-release")); + assertTrue(caller.getArgs().contains("42")); + assertTrue(caller.getArgs().contains("-target:jvm-1.8")); + } }