What is the difference between these task definition syntaxes in gradle?

前端 未结 1 1532
长情又很酷
长情又很酷 2020-12-23 21:33

A)

task build << {  
  description = \"Build task.\"  
  ant.echo(\'build\')  
}

B)

task build {  
  description          


        
相关标签:
1条回答
  • 2020-12-23 22:10

    The first syntax defines a task, and provides some code to be executed when the task executes. The second syntax defines a task, and provides some code to be executed straight away to configure the task. For example:

    task build << { println 'this executes when build task is executed' }
    task build { println 'this executes when the build script is executed' }
    

    In fact, the first syntax is equivalent to:

    task build { doLast { println 'this executes when build task is executed' } }
    

    So, in your example above, for syntax A the description does not show up in gradle -t because the code which sets the description is not executed until the task executed, which does not happen when you run gradle -t.

    For syntax B the code that does the ant.echo() is run for every invocation of gradle, including gradle -t

    To provide both an action to execute and a description for the task you can do either of:

    task build(description: 'some description') << { some code }
    task build { description = 'some description'; doLast { some code } }
    
    0 讨论(0)
提交回复
热议问题