Skip to content

Commit

Permalink
Use onlyIf to condition tasks execution
Browse files Browse the repository at this point in the history
  • Loading branch information
alextu committed Mar 6, 2023
1 parent 2522255 commit c07302c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
30 changes: 9 additions & 21 deletions src/main/groovy/org/jenkinsci/gradle/plugins/jpi/JpiPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -597,51 +597,39 @@ class JpiPlugin implements Plugin<Project>, PluginDependencyProvider {
xml.required = true
html.required = false
}
}
project.afterEvaluate {
def jpiExtension = project.extensions.getByType(JpiExtension)
if (!jpiExtension.checkstyleEnabled.get()) {
project.tasks.withType(Checkstyle).each {
it.enabled = false
}
it.onlyIf {
project.extensions.getByType(JpiExtension).checkstyleEnabled.get()
}
}
}

private configureJacoco(Project project) {
def jpiExtension = project.extensions.getByType(JpiExtension)
project.tasks.withType(JacocoReport).configureEach {
it.reports {
xml.required = true
html.required = false
}
}
project.afterEvaluate {
if (jpiExtension.jacocoEnabled.get()) {
project.tasks.withType(Test).each {
it.finalizedBy(project.tasks.named('jacocoTestReport'))
}
it.onlyIf {
project.extensions.getByType(JpiExtension).jacocoEnabled.get()
}
}
project.tasks.withType(Test).configureEach {
it.finalizedBy(project.tasks.named('jacocoTestReport'))
}
}

private configureSpotbugs(Project project) {
if (GradleVersion.current() < GradleVersion.version('7.0')) {
return
}
project.plugins.apply(SpotBugsPlugin)
def jpiExtension = project.extensions.getByType(JpiExtension)
project.tasks.withType(SpotBugsTask).configureEach {
it.reports {
xml.required = true
html.required = false
}
}
project.afterEvaluate {
if (!jpiExtension.spotBugsEnabled.get()) {
project.tasks.withType(SpotBugsTask).each {
it.enabled = false
}
it.onlyIf {
project.extensions.getByType(JpiExtension).spotBugsEnabled.get()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class JacocoPluginSpec extends IntegrationSpec {
.build()

then:
result.task(':jacocoTestReport') == null
result.task(':jacocoTestReport').outcome == TaskOutcome.SKIPPED
}

def "should run jacoco task and generate only xml report"() {
Expand Down

0 comments on commit c07302c

Please sign in to comment.