diff --git a/src/main/java/io/spring/ge/conventions/gradle/GradleEnterpriseConventionsPlugin.java b/src/main/java/io/spring/ge/conventions/gradle/GradleEnterpriseConventionsPlugin.java index ca9df06..9a3b88d 100644 --- a/src/main/java/io/spring/ge/conventions/gradle/GradleEnterpriseConventionsPlugin.java +++ b/src/main/java/io/spring/ge/conventions/gradle/GradleEnterpriseConventionsPlugin.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2023 the original author or authors. + * Copyright 2020-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -76,32 +76,42 @@ private void apply(Project project) { private void configureBuildScanConventions(BuildScanExtension buildScan, StartParameter startParameter, File rootDir) { - if (!startParameter.isNoBuildScan()) { - ProcessOperationsProcessRunner processRunner = new ProcessOperationsProcessRunner( - new WorkingDirectoryProcessOperations(this.processOperations, rootDir)); - if (startParameter.isBuildScan()) { - new AnonymousPublicationBuildScanConventions(processRunner) { - - @Override - protected String getJdkVersion() { - String toolchainVersion = startParameter.getProjectProperties().get("toolchainVersion"); - return (toolchainVersion != null) ? toolchainVersion : super.getJdkVersion(); - } - - }.execute(buildScan); - } - else { - new BuildScanConventions(processRunner) { + if (startParameter.isNoBuildScan() || containsPropertiesTask(startParameter)) { + return; + } + ProcessOperationsProcessRunner processRunner = new ProcessOperationsProcessRunner( + new WorkingDirectoryProcessOperations(this.processOperations, rootDir)); + if (startParameter.isBuildScan()) { + new AnonymousPublicationBuildScanConventions(processRunner) { + + @Override + protected String getJdkVersion() { + String toolchainVersion = startParameter.getProjectProperties().get("toolchainVersion"); + return (toolchainVersion != null) ? toolchainVersion : super.getJdkVersion(); + } + + }.execute(buildScan); + } + else { + new BuildScanConventions(processRunner) { - @Override - protected String getJdkVersion() { - String toolchainVersion = startParameter.getProjectProperties().get("toolchainVersion"); - return (toolchainVersion != null) ? toolchainVersion : super.getJdkVersion(); - } + @Override + protected String getJdkVersion() { + String toolchainVersion = startParameter.getProjectProperties().get("toolchainVersion"); + return (toolchainVersion != null) ? toolchainVersion : super.getJdkVersion(); + } + + }.execute(buildScan); + } + } - }.execute(buildScan); + private boolean containsPropertiesTask(StartParameter startParameter) { + for (String taskName : startParameter.getTaskNames()) { + if (taskName.equals("properties") || taskName.endsWith(":properties")) { + return true; } } + return false; } } diff --git a/src/test/java/io/spring/ge/conventions/gradle/GradleEnterpriseConventionsPluginIntegrationTests.java b/src/test/java/io/spring/ge/conventions/gradle/GradleEnterpriseConventionsPluginIntegrationTests.java index 7deb06f..9723b94 100644 --- a/src/test/java/io/spring/ge/conventions/gradle/GradleEnterpriseConventionsPluginIntegrationTests.java +++ b/src/test/java/io/spring/ge/conventions/gradle/GradleEnterpriseConventionsPluginIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2023 the original author or authors. + * Copyright 2020-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -66,6 +66,24 @@ void givenGradle6WhenThePluginIsAppliedAndBuildScansAreDisabledThenBuildScanConv assertThat(result.getOutput()).contains("Capture task input files: false"); } + @Test + void givenGradle6WhenThePluginIsAppliedAndPropertiesTaskIsExecutedThenBuildScanConventionsAreNotApplied( + @TempDir File projectDir) { + prepareGradle6Project(projectDir); + BuildResult result = build(projectDir, "6.0.1", "properties", "verifyBuildScanConfig", "--no-scan"); + assertThat(result.getOutput()).contains("Build scan server: null"); + assertThat(result.getOutput()).contains("Capture task input files: false"); + } + + @Test + void givenGradle6MulitProjectBuildWhenThePluginIsAppliedAndPropertiesTaskIsExecutedThenBuildScanConventionsAreNotApplied( + @TempDir File projectDir) { + prepareGradle6MultiProject(projectDir); + BuildResult result = build(projectDir, "6.0.1", "sub:properties", "sub:verifyBuildScanConfig", "--no-scan"); + assertThat(result.getOutput()).contains("Build scan server: null"); + assertThat(result.getOutput()).contains("Capture task input files: false"); + } + @Test void givenGradle6WhenThePluginIsAppliedAndScanIsSpecifiedThenServerIsNotCustomized(@TempDir File projectDir) { prepareGradle6Project(projectDir); @@ -99,6 +117,15 @@ void givenGradle5WhenThePluginIsAppliedAndBuildScansAreDisabledThenBuildScanConv assertThat(result.getOutput()).contains("Capture task input files: false"); } + @Test + void givenGradle5WhenThePluginIsAppliedAndPropertiesTaskIsExecutedThenBuildScanConventionsAreNotApplied( + @TempDir File projectDir) { + prepareGradle5Project(projectDir); + BuildResult result = build(projectDir, "5.6.4", "properties", "verifyBuildScanConfig", "--no-scan"); + assertThat(result.getOutput()).contains("Build scan server: null"); + assertThat(result.getOutput()).contains("Capture task input files: false"); + } + @Test void givenGradle5WhenThePluginIsAppliedAndScanIsSpecifiedThenServerIsNotCustomized(@TempDir File projectDir) { prepareGradle5Project(projectDir); @@ -147,7 +174,34 @@ private void prepareGradle6Project(File projectDir) { }); } + private void prepareGradle6MultiProject(File projectDir) { + write(new File(projectDir, "gradle.properties"), (writer) -> writer.println("org.gradle.caching=true")); + write(new File(projectDir, "settings.gradle"), (writer) -> { + writer.println("plugins {"); + writer.println(" id 'com.gradle.enterprise'"); + writer.println(" id 'io.spring.ge.conventions' version '" + version() + "'"); + writer.println("}"); + writer.println("include 'sub'"); + writer.println("gradle.afterProject { project -> project.ext['settings'] = settings }"); + }); + write(new File(new File(projectDir, "sub"), "build.gradle"), (writer) -> { + writer.println("task verifyBuildScanConfig {"); + writer.println(" doFirst {"); + writer.println(" println \"Build scan server: ${buildScan.server}\""); + writer.println(" println \"Capture task input files: ${buildScan.captureTaskInputFiles}\""); + writer.println(" }"); + writer.println("}"); + writer.println("task verifyBuildCacheConfig {"); + writer.println(" doFirst {"); + writer + .println(" println \"Build cache remote: ${project.ext['settings'].buildCache?.remote?.url}\""); + writer.println(" }"); + writer.println("}"); + }); + } + private void write(File file, Consumer consumer) { + file.getParentFile().mkdirs(); try (PrintWriter writer = new PrintWriter(new FileWriter(file))) { consumer.accept(writer); }