diff --git a/changelog/@unreleased/pr-1524.yml b/changelog/@unreleased/pr-1524.yml new file mode 100644 index 000000000..d6ba05c3e --- /dev/null +++ b/changelog/@unreleased/pr-1524.yml @@ -0,0 +1,5 @@ +type: fix +fix: + description: Always set target version for Scala compiler to JVM 8. + links: + - https://github.com/palantir/gradle-baseline/pull/1524 diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineScalastyle.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineScalastyle.java index 44c30880c..c96d7adb3 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineScalastyle.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineScalastyle.java @@ -34,6 +34,8 @@ import org.gradle.plugins.ide.idea.model.IdeaModel; public final class BaselineScalastyle extends AbstractBaselinePlugin { + private static final String SCALA_TARGET_VERSION = "jvm-8"; + @Override public void apply(Project project) { this.project = project; @@ -41,8 +43,7 @@ public void apply(Project project) { JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class); project.getTasks().withType(ScalaCompile.class).configureEach(scalaCompile -> scalaCompile .getScalaCompileOptions() - .setAdditionalParameters(ImmutableList.of("-target:jvm-" - + javaConvention.getTargetCompatibility().toString()))); + .setAdditionalParameters(ImmutableList.of("-target:" + SCALA_TARGET_VERSION))); project.getRootProject().getPluginManager().withPlugin("idea", ideaPlugin -> project.getRootProject() .getExtensions() .configure( @@ -52,8 +53,7 @@ public void apply(Project project) { javaConvention .getSourceSets() .named(SourceSet.MAIN_SOURCE_SET_NAME) - .get(), - javaConvention.getTargetCompatibility().toString()))); + .get()))); project.getPluginManager().apply(ScalaStylePlugin.class); TaskCollection scalaStyleTasks = project.getTasks().withType(ScalaStyleTask.class); scalaStyleTasks.configureEach(scalaStyleTask -> { @@ -72,7 +72,7 @@ public void apply(Project project) { }); } - private void configureIdeaPlugin(IdeaModel ideaModel, SourceSet mainSourceSet, String javaVersion) { + private void configureIdeaPlugin(IdeaModel ideaModel, SourceSet mainSourceSet) { Convention scalaConvention = (Convention) InvokerHelper.getProperty(mainSourceSet, "convention"); ScalaSourceSet scalaSourceSet = scalaConvention.getPlugin(ScalaSourceSet.class); // If scala source directory doesn't contain java files use "JavaThenScala" compilation mode @@ -84,7 +84,7 @@ private void configureIdeaPlugin(IdeaModel ideaModel, SourceSet mainSourceSet, S : "Mixed"; ideaModel.getProject().getIpr().withXml(xmlProvider -> { // configure target jvm mode - String targetJvmVersion = "-target:jvm-" + javaVersion; + String targetJvmVersion = "-target:" + SCALA_TARGET_VERSION; Node rootNode = xmlProvider.asNode(); Node scalaCompilerConf = (Node) rootNode.getAt(new QName("component")).stream() .filter(o -> ((Node) o).attributes().get("name").equals("ScalaCompilerConfiguration")) diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineScalastyleTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineScalastyleTest.groovy index cbf5c5b80..8477babbf 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineScalastyleTest.groovy +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineScalastyleTest.groovy @@ -21,7 +21,6 @@ import org.github.ngbinh.scalastyle.ScalaStylePlugin import org.github.ngbinh.scalastyle.ScalaStyleTask import org.gradle.api.Project import org.gradle.api.XmlProvider -import org.gradle.api.plugins.JavaPluginConvention import org.gradle.api.tasks.scala.ScalaCompile import org.gradle.plugins.ide.idea.model.IdeaModel import org.gradle.testfixtures.ProjectBuilder @@ -71,15 +70,10 @@ final class BaselineScalastyleTest extends Specification { } def configuresTargetJvmVersion() { - String targetVersion = project.getConvention() - .getPlugin(JavaPluginConvention.class) - .getTargetCompatibility() - .toString() - expect: def tasks = project.tasks.withType(ScalaCompile.class) for (ScalaCompile task : tasks) { - assert task.getScalaCompileOptions().getAdditionalParameters().contains("-target:jvm-" + targetVersion) + assert task.getScalaCompileOptions().getAdditionalParameters().contains("-target:jvm-8") } }