diff --git a/changelog/@unreleased/pr-1584.v2.yml b/changelog/@unreleased/pr-1584.v2.yml new file mode 100644 index 000000000..76ef9fca9 --- /dev/null +++ b/changelog/@unreleased/pr-1584.v2.yml @@ -0,0 +1,6 @@ +type: improvement +improvement: + description: checkExplicitSourceCompatabilityTask ignores projects without java + source + links: + - https://github.com/palantir/gradle-baseline/pull/1584 diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/tasks/CheckExplicitSourceCompatibilityTask.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/tasks/CheckExplicitSourceCompatibilityTask.java index a43424c0c..fa2d110a5 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/tasks/CheckExplicitSourceCompatibilityTask.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/tasks/CheckExplicitSourceCompatibilityTask.java @@ -31,6 +31,7 @@ import org.gradle.api.provider.Property; import org.gradle.api.publish.PublishingExtension; import org.gradle.api.specs.Spec; +import org.gradle.api.tasks.SourceSetContainer; import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.options.Option; import org.gradle.util.GradleVersion; @@ -74,6 +75,14 @@ public boolean isSatisfiedBy(Task element) { return !publishing.getPublications().isEmpty(); } }); + onlyIf(new Spec() { + @Override + public boolean isSatisfiedBy(Task task) { + return getProject().getExtensions().getByType(SourceSetContainer.class).stream() + .anyMatch( + sourceSet -> !sourceSet.getAllJava().getFiles().isEmpty()); + } + }); } @Option(option = "fix", description = "Whether to apply the suggested fix to build.gradle") diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineReproducibilityIntegrationSpec.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineReproducibilityIntegrationSpec.groovy index a7599798c..3988bc896 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineReproducibilityIntegrationSpec.groovy +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/BaselineReproducibilityIntegrationSpec.groovy @@ -69,7 +69,7 @@ class BaselineReproducibilityIntegrationSpec extends IntegrationSpec { writeHelloWorld() then: - runTasksSuccessfully("checkExplicitSourceCompatibility") + def result = runTasksSuccessfully("checkExplicitSourceCompatibility") } def 'no-op if nothing is published'() { @@ -87,4 +87,26 @@ class BaselineReproducibilityIntegrationSpec extends IntegrationSpec { def output = runTasksSuccessfully("check") output.getStandardOutput().contains("> Task :checkExplicitSourceCompatibility SKIPPED") } + + def 'no-op if there is not source'() { + when: + buildFile << """ + ${applyPlugin(BaselineReproducibility.class)} + apply plugin: 'java' + apply plugin: 'maven-publish' + version '1.2.3' + + publishing { + publications { + maven(MavenPublication) { + from components.java + } + } + } + """.stripIndent() + + then: + def output = runTasksSuccessfully("check") + output.getStandardOutput().contains("> Task :checkExplicitSourceCompatibility SKIPPED") + } }