Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always set JVM 8 target in Scala compiler #1524

Merged
merged 3 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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