Gradle extra属性在子项目中定义的自定义任务中不可见 [英] Gradle extra properties not visible in a custom task defined in a subproject

查看:883
本文介绍了Gradle extra属性在子项目中定义的自定义任务中不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在多个Gradle任务中重复使用通用逻辑,类似于这个答案中的建议, m无法显示额外的项目属性。



下面是问题所在。假设我有一个根Gradle构建脚本, build.gradle ,它设置了一个额外的项目属性,

  project.ext.myProp ='myValue'

我有一个子项目 settings.gradle

  include'subproject'

,并且子项目定义并使用引用额外项目属性的自定义任务,

  class CustomTask extends DefaultTask {
CustomTask(){
doFirst {
println project.ext.myProp
}



任务自定义(类型:CustomTask){
println'自定义任务'
}

执行此操作会带给我:

  FAILURE:Build例外失败。 
...
*例外是:
org.gradle.api.GradleScriptException:评估项目':subproject'时发生问题。
...
引起:org.gradle.api.tasks.TaskInstantiationException:无法创建类型为'CustomTask'的任务。
...
导致:groovy.lang.MissingPropertyException:由于不存在
...

,因此不能获得属性'myProp'的额外属性扩展BUILD FAILED

请注意,这似乎工作如果:


$ b $如果您使用动态属性而不是额外的属性,那么自定义任务在根项目中与额外属性一起定义
  • 但不推荐使用 / li>

    解决方案

    阅读名为 foo foo project.foo (而不是 ext.foo ),它也将搜索父项目的(额外)属性。编辑:在任务类中,您可以使用 project.foo



    请注意,仅用于构建脚本中的临时脚本;任务类和插件不能使用它们。任务类不应该伸手进入Gradle对象模型;相反,它应该声明允许构建脚本和/或插件为其提供所需信息的属性(以及必要的方法)。这使得理解,重用和记录任务类变得更加容易,并且可以通过 @Input ... 声明输入和输出。 PS:不用在中加入 doFirst ,而是在$ a中使用@Output ... 注解。

    构造函数,任务类通常有一个注解为 @TaskAction 的方法。


    I'm trying to reuse common logic among multiple Gradle tasks, similar to what was suggested in this answer, but I'm having trouble with extra project properties not being visible.

    Boiled down, here's the problem. Say I have a root Gradle build script, build.gradle that sets an extra project property,

    project.ext.myProp = 'myValue'
    

    I have a subproject defined in settings.gradle,

    include 'subproject'
    

    and the subproject defines and uses a custom task that references that extra project property,

    class CustomTask extends DefaultTask {
        CustomTask() {
            doFirst {
                println project.ext.myProp
            }
        }
    }
    
    task custom(type: CustomTask) {
        println 'custom task'
    }
    

    Executing this gives me this:

    FAILURE: Build failed with an exception.
    ...
    * Exception is:
    org.gradle.api.GradleScriptException: A problem occurred evaluating project ':subproject'.
    ...
    Caused by: org.gradle.api.tasks.TaskInstantiationException: Could not create task of type 'CustomTask'.
    ...
    Caused by: groovy.lang.MissingPropertyException: cannot get property 'myProp' on extra properties extension as it does not exist
    ...
    
    BUILD FAILED
    

    Note that this seems to work if:

    • the custom task is defined in the root project alongside the extra property
    • if you use dynamic properties instead of extra properties, but those are deprecated

    解决方案

    The recommended syntax for reading an extra property named foo in a build script is foo or project.foo (rather than ext.foo), which will also search the parent projects' (extra) properties. EDIT: In a task class, you can use project.foo.

    It's important to note that extra properties are only meant for ad-hoc scripting in build scripts; task classes and plugins should not use them. A task class shouldn't reach out into the Gradle object model at all; instead, it should declare properties (and, if necessary, methods) which allow build scripts and/or plugins to supply it with all information that it needs. This makes it easier to understand, reuse, and document the task class, and makes it possible to declare inputs and outputs via @Input... and @Output... annotations.

    PS: Instead of calling doFirst in a constructor, a task class usually has a method annotated with @TaskAction.

    这篇关于Gradle extra属性在子项目中定义的自定义任务中不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆