Skip to content

Commit

Permalink
Always set JVM 8 target in Scala compiler (#1524)
Browse files Browse the repository at this point in the history
Always set target version for Scala compiler to JVM 8.
  • Loading branch information
rshkv authored Oct 29, 2020
1 parent d128517 commit 9c9b4cb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1524.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@
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;
project.getPluginManager().withPlugin("scala", plugin -> {
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(
Expand All @@ -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<ScalaStyleTask> scalaStyleTasks = project.getTasks().withType(ScalaStyleTask.class);
scalaStyleTasks.configureEach(scalaStyleTask -> {
Expand All @@ -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
Expand All @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
}

Expand Down

0 comments on commit 9c9b4cb

Please sign in to comment.