Search in sources :

Example 1 with ScalaCompile

use of org.gradle.api.tasks.scala.ScalaCompile in project gradle by gradle.

the class ScalaBasePlugin method configureScalaCompile.

private static void configureScalaCompile(final Project project, final SourceSet sourceSet) {
    String taskName = sourceSet.getCompileTaskName("scala");
    final ScalaCompile scalaCompile = project.getTasks().create(taskName, ScalaCompile.class);
    scalaCompile.dependsOn(sourceSet.getCompileJavaTaskName());
    Convention scalaConvention = (Convention) InvokerHelper.getProperty(sourceSet, "convention");
    ScalaSourceSet scalaSourceSet = scalaConvention.findPlugin(ScalaSourceSet.class);
    SourceSetUtil.configureForSourceSet(sourceSet, scalaSourceSet.getScala(), scalaCompile, scalaCompile.getOptions(), project);
    scalaCompile.setDescription("Compiles the " + scalaSourceSet.getScala() + ".");
    scalaCompile.setSource(scalaSourceSet.getScala());
    project.getTasks().getByName(sourceSet.getClassesTaskName()).dependsOn(taskName);
    // cannot use convention mapping because the resulting object won't be serializable
    // cannot compute at task execution time because we need association with source set
    project.getGradle().addBuildListener(new BuildAdapter() {

        @Override
        public void projectsEvaluated(Gradle gradle) {
            IncrementalCompileOptions incrementalOptions = scalaCompile.getScalaCompileOptions().getIncrementalOptions();
            if (incrementalOptions.getAnalysisFile() == null) {
                String analysisFilePath = project.getBuildDir().getPath() + "/tmp/scala/compilerAnalysis/" + scalaCompile.getName() + ".analysis";
                incrementalOptions.setAnalysisFile(new File(analysisFilePath));
            }
            if (incrementalOptions.getPublishedCode() == null) {
                Jar jarTask = (Jar) project.getTasks().findByName(sourceSet.getJarTaskName());
                incrementalOptions.setPublishedCode(jarTask == null ? null : jarTask.getArchivePath());
            }
        }
    });
}
Also used : IncrementalCompileOptions(org.gradle.api.tasks.scala.IncrementalCompileOptions) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) Convention(org.gradle.api.plugins.Convention) ScalaCompile(org.gradle.api.tasks.scala.ScalaCompile) ScalaSourceSet(org.gradle.api.tasks.ScalaSourceSet) DefaultScalaSourceSet(org.gradle.api.internal.tasks.DefaultScalaSourceSet) BuildAdapter(org.gradle.BuildAdapter) Jar(org.gradle.jvm.tasks.Jar) Gradle(org.gradle.api.invocation.Gradle) File(java.io.File)

Example 2 with ScalaCompile

use of org.gradle.api.tasks.scala.ScalaCompile in project gradle by gradle.

the class ScalaBasePlugin method configureScalaCompile.

private static void configureScalaCompile(final Project project, JavaBasePlugin javaPlugin, final SourceSet sourceSet) {
    String taskName = sourceSet.getCompileTaskName("scala");
    final ScalaCompile scalaCompile = project.getTasks().create(taskName, ScalaCompile.class);
    scalaCompile.dependsOn(sourceSet.getCompileJavaTaskName());
    javaPlugin.configureForSourceSet(sourceSet, scalaCompile);
    Convention scalaConvention = (Convention) InvokerHelper.getProperty(sourceSet, "convention");
    ScalaSourceSet scalaSourceSet = scalaConvention.findPlugin(ScalaSourceSet.class);
    scalaCompile.setDescription("Compiles the " + scalaSourceSet.getScala() + ".");
    scalaCompile.setSource(scalaSourceSet.getScala());
    project.getTasks().getByName(sourceSet.getClassesTaskName()).dependsOn(taskName);
    // cannot use convention mapping because the resulting object won't be serializable
    // cannot compute at task execution time because we need association with source set
    project.getGradle().addBuildListener(new BuildAdapter() {

        @Override
        public void projectsEvaluated(Gradle gradle) {
            IncrementalCompileOptions incrementalOptions = scalaCompile.getScalaCompileOptions().getIncrementalOptions();
            if (incrementalOptions.getAnalysisFile() == null) {
                String analysisFilePath = project.getBuildDir().getPath() + "/tmp/scala/compilerAnalysis/" + scalaCompile.getName() + ".analysis";
                incrementalOptions.setAnalysisFile(new File(analysisFilePath));
            }
            if (incrementalOptions.getPublishedCode() == null) {
                Jar jarTask = (Jar) project.getTasks().findByName(sourceSet.getJarTaskName());
                incrementalOptions.setPublishedCode(jarTask == null ? null : jarTask.getArchivePath());
            }
        }
    });
}
Also used : IncrementalCompileOptions(org.gradle.api.tasks.scala.IncrementalCompileOptions) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) Convention(org.gradle.api.plugins.Convention) ScalaCompile(org.gradle.api.tasks.scala.ScalaCompile) ScalaSourceSet(org.gradle.api.tasks.ScalaSourceSet) DefaultScalaSourceSet(org.gradle.api.internal.tasks.DefaultScalaSourceSet) BuildAdapter(org.gradle.BuildAdapter) Jar(org.gradle.jvm.tasks.Jar) Gradle(org.gradle.api.invocation.Gradle) File(java.io.File)

Example 3 with ScalaCompile

use of org.gradle.api.tasks.scala.ScalaCompile in project gradle by gradle.

the class ScalaBasePlugin method configureScalaCompile.

@SuppressWarnings("deprecation")
private void configureScalaCompile(final Project project, final SourceSet sourceSet, final Configuration incrementalAnalysis, final Usage incrementalAnalysisUsage, final ScalaRuntime scalaRuntime) {
    final ScalaSourceDirectorySet scalaSourceSet = sourceSet.getExtensions().getByType(ScalaSourceDirectorySet.class);
    final TaskProvider<ScalaCompile> scalaCompileTask = project.getTasks().register(sourceSet.getCompileTaskName("scala"), ScalaCompile.class, scalaCompile -> {
        JvmPluginsHelper.configureForSourceSet(sourceSet, scalaSourceSet, scalaCompile, scalaCompile.getOptions(), project);
        scalaCompile.setDescription("Compiles the " + scalaSourceSet + ".");
        scalaCompile.setSource(scalaSourceSet);
        scalaCompile.getJavaLauncher().convention(getToolchainTool(project, JavaToolchainService::launcherFor));
        scalaCompile.getAnalysisMappingFile().set(project.getLayout().getBuildDirectory().file("tmp/scala/compilerAnalysis/" + scalaCompile.getName() + ".mapping"));
        // cannot compute at task execution time because we need association with source set
        IncrementalCompileOptions incrementalOptions = scalaCompile.getScalaCompileOptions().getIncrementalOptions();
        incrementalOptions.getAnalysisFile().set(project.getLayout().getBuildDirectory().file("tmp/scala/compilerAnalysis/" + scalaCompile.getName() + ".analysis"));
        incrementalOptions.getClassfileBackupDir().set(project.getLayout().getBuildDirectory().file("tmp/scala/classfileBackup/" + scalaCompile.getName() + ".bak"));
        final Jar jarTask = (Jar) project.getTasks().findByName(sourceSet.getJarTaskName());
        if (jarTask != null) {
            incrementalOptions.getPublishedCode().set(jarTask.getArchiveFile());
        }
        scalaCompile.getAnalysisFiles().from(incrementalAnalysis.getIncoming().artifactView(new Action<ArtifactView.ViewConfiguration>() {

            @Override
            public void execute(ArtifactView.ViewConfiguration viewConfiguration) {
                viewConfiguration.lenient(true);
                viewConfiguration.componentFilter(new IsProjectComponent());
            }
        }).getFiles());
        // See https://github.com/gradle/gradle/issues/14434.  We do this so that the incrementalScalaAnalysisForXXX configuration
        // is resolved during task graph calculation.  It is not an input, but if we leave it to be resolved during task execution,
        // it can potentially block trying to resolve project dependencies.
        scalaCompile.dependsOn(scalaCompile.getAnalysisFiles());
    });
    JvmPluginsHelper.configureOutputDirectoryForSourceSet(sourceSet, scalaSourceSet, project, scalaCompileTask, scalaCompileTask.map(new Transformer<CompileOptions, ScalaCompile>() {

        @Override
        public CompileOptions transform(ScalaCompile scalaCompile) {
            return scalaCompile.getOptions();
        }
    }));
    project.getTasks().named(sourceSet.getClassesTaskName(), new Action<Task>() {

        @Override
        public void execute(Task task) {
            task.dependsOn(scalaCompileTask);
        }
    });
}
Also used : IncrementalCompileOptions(org.gradle.api.tasks.scala.IncrementalCompileOptions) Task(org.gradle.api.Task) ScalaCompile(org.gradle.api.tasks.scala.ScalaCompile) Transformer(org.gradle.api.Transformer) ArtifactView(org.gradle.api.artifacts.ArtifactView) ScalaSourceDirectorySet(org.gradle.api.tasks.ScalaSourceDirectorySet) Jar(org.gradle.jvm.tasks.Jar)

Aggregations

IncrementalCompileOptions (org.gradle.api.tasks.scala.IncrementalCompileOptions)3 ScalaCompile (org.gradle.api.tasks.scala.ScalaCompile)3 Jar (org.gradle.jvm.tasks.Jar)3 File (java.io.File)2 BuildAdapter (org.gradle.BuildAdapter)2 DefaultScalaSourceSet (org.gradle.api.internal.tasks.DefaultScalaSourceSet)2 Gradle (org.gradle.api.invocation.Gradle)2 Convention (org.gradle.api.plugins.Convention)2 JavaPluginConvention (org.gradle.api.plugins.JavaPluginConvention)2 ScalaSourceSet (org.gradle.api.tasks.ScalaSourceSet)2 Task (org.gradle.api.Task)1 Transformer (org.gradle.api.Transformer)1 ArtifactView (org.gradle.api.artifacts.ArtifactView)1 ScalaSourceDirectorySet (org.gradle.api.tasks.ScalaSourceDirectorySet)1