|
| 1 | +abstract class BuildScopedGreetingService @Inject constructor( |
| 2 | + execOperations: ExecOperations, |
| 3 | +) : BuildService<BuildScopedGreetingService.Params> { |
| 4 | + |
| 5 | + companion object { |
| 6 | + const val SERVICE_NAME = "buildScopedGreetingService" |
| 7 | + } |
| 8 | + |
| 9 | + interface Params : BuildServiceParameters { |
| 10 | + val greeting: Property<String> |
| 11 | + } |
| 12 | + |
| 13 | + val exitValue: Int = |
| 14 | + execOperations.exec { |
| 15 | + commandLine("echo", "Hello, ${parameters.greeting.get()}") |
| 16 | + standardOutput = System.out |
| 17 | + }.exitValue |
| 18 | +} |
| 19 | + |
| 20 | +abstract class ProjectScopedGreetingTask @Inject constructor( |
| 21 | + private val execOperations: ExecOperations, |
| 22 | +) : DefaultTask() { |
| 23 | + |
| 24 | + init { |
| 25 | + group = "demo" |
| 26 | + description = "A project-scoped exec task that depends on a " + |
| 27 | + "build-scoped exec from a shared build service." |
| 28 | + } |
| 29 | + |
| 30 | + @Suppress("UnstableApiUsage") |
| 31 | + @get:ServiceReference(BuildScopedGreetingService.SERVICE_NAME) |
| 32 | + abstract val buildScopedGreetingService: Property<BuildScopedGreetingService> |
| 33 | + |
| 34 | + @TaskAction |
| 35 | + fun doTaskAction() { |
| 36 | + |
| 37 | + val buildScopedGreetingServiceInstance: BuildScopedGreetingService = |
| 38 | + buildScopedGreetingService.get() |
| 39 | + |
| 40 | + val exitValue = buildScopedGreetingServiceInstance.exitValue |
| 41 | + |
| 42 | + if (exitValue != 0) |
| 43 | + throw GradleException( |
| 44 | + "Build service exec resulted in a non-zero exit value $exitValue!" |
| 45 | + ) |
| 46 | + |
| 47 | + val greeting = |
| 48 | + buildScopedGreetingServiceInstance.parameters.greeting.get() |
| 49 | + |
| 50 | + execOperations.exec { |
| 51 | + commandLine("echo", greeting) |
| 52 | + standardOutput = System.out |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +gradle.sharedServices.registerIfAbsent( |
| 58 | + BuildScopedGreetingService.SERVICE_NAME, |
| 59 | + BuildScopedGreetingService::class.java |
| 60 | +) { |
| 61 | + parameters.greeting.set("Doctor"); |
| 62 | +} |
| 63 | + |
| 64 | +tasks.register<ProjectScopedGreetingTask>("greetings") |
0 commit comments