Home > Blockchain >  Authoring Gradle task with int[] input property
Authoring Gradle task with int[] input property

Time:02-11

I am authoring a Gradle task that needs to work on an array of integers (dispatching a job for each value).

I don't understand how to properly use Property<T> to accept a Groovy array syntax.

The following does not work

Inside Task.java

/**
 * Element IDs to download
 */
@Input
protected abstract Property<Integer[]> getTaxonomyElementIds();

Inside build.gradle

taxonomyElementIds = [200, 201, 202, 203, 204, 205, 206, 207, 208]

Error

Cannot set the value of task ':downloadMasterDb' property 'taxonomyElementIds' of type [Ljava.lang.Integer; using an instance of type java.util.ArrayList.

The following workaround works, but I prefer a cleaner Groovy syntax

    taxonomyElementIds = [200, 201, 202, 203, 204, 205, 206, 207, 208].toArray(Integer[]::new)

I also tried to change the type of the property to List<Integer> (which essentially doesn't change my task code) but doesn't work.

How can I change my property in order to accept the Groovy array/list syntax?

CodePudding user response:

Use ListProperty<Integer> instead:

plugins {
    id("java")
}

repositories {
    mavenCentral()
}

abstract class MyTask extends DefaultTask {

    @Input
    protected abstract ListProperty<Integer> getTaxonomyElementIds();

    @TaskAction
    void generate() {
        taxonomyElementIds.get().each {
            println it
        }
    }

}

tasks.register("myTask", MyTask) {
    taxonomyElementIds = [200, 201, 202, 203, 204, 205, 206, 207, 208]
}

Output of the above:

> Task :myTask
200
201
202
203
204
205
206
207
208
  • Related