【问题标题】:Gradle: Unable to store task input properties. Property 'myInput' with value '[null]' cannot be serializedGradle:无法存储任务输入属性。无法序列化值为“[null]”的属性“myInput”
【发布时间】:2015-10-22 07:56:58
【问题描述】:

如何让 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&lt;String&gt;

@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

【问题讨论】:

  • 添加了关于 Gradle 2.4 的注释。我基本上错过了 Serializable 的正确实现。请参阅下面的答案。

标签: gradle gradle-plugin


【解决方案1】:

该类需要实现“Serializable”接口。这是为了允许 Gradle 完全序列化对象,以便可以在构建之间比较二进制格式以查看是否有任何更改。一旦 CustomClass 是可序列化的,那么 List&lt;CustomClass&gt; 也将是可序列化的,只要它使用标准实现,例如 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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    相关资源
    最近更新 更多