4

如何让 Gradle @Input 工作List<CustomClass>?我已经尝试添加该toString()方法,这有帮助,但它仍然以奇怪的方式失败。使其可序列化的正确方法是什么?这是 Gradle 的 2.4 版。

失败:List<CustomClass>

@Input
List<CustomClass> getMyInput() {
    List<CustomClass> simpleList = new ArrayList<>()
    simpleList.add(new CustomClass())
    return simpleList
}

static class CustomClass {
    String str
}

这失败并显示以下消息:

"Unable to store task input properties. Property 'myInput' with value '[null]' cannot be serialized."

成功:List<String>

@Input
List<String> getMyInput() {
    List<String> simpleList = new ArrayList<>()
    simpleList.add(new String('ignore'))
    return simpleList
}

异常创建位置的 Gradle 源代码参考: https ://github.com/gradle/gradle/blob/master/subprojects/core/src/main/groovy/org/gradle/api/internal/changedetection/state/InputPropertiesSerializer .java#L42

4

1 回答 1

2

该类需要实现“Serializable”接口。这是为了允许 Gradle 完全序列化对象,以便可以在构建之间比较二进制格式以查看是否有任何更改。一旦 CustomClass 是可序列化的,那么List<CustomClass>只要它使用标准实现,例如ArrayList.

这是一个例子:

@EqualsAndHashCode
static class CustomClass implements Serializable {
    // Increment this when the serialization output changes
    private static final long serialVersionUID = 1L;

    String projectName

    @SuppressWarnings('unused')
    private static void writeObject(ObjectOutputStream s) throws IOException {
        s.defaultWriteObject();
    }

    // Gradle only needs to serialize objects, so this isn't strictly needed
    @SuppressWarnings('unused')
    private static void readObject(ObjectInputStream s) throws IOException {
        s.defaultReadObject();
    }
}
于 2015-10-22T16:14:04.973 回答