Grouping/inheriting properties for tasks in Gradle

北城余情 提交于 2021-01-28 12:11:04

问题


Is there a way to reuse property groups in Gradle?

Something that would look like:

def propGroup = [
 options.fork = true
 options.forkOptions.executable = ...
]

task compileThis(type:JavaCompile) {
  options.fork = propGroup.options.fork
  options.forkOptions.setExecutable(propGroup.options.forkOptions.executable)
  destinationDir = file(xxx)
}

task compileThat(type:JavaCompile) {
  options.fork = propGroup.options.fork
  options.forkOptions.setExecutable(propGroup.options.forkOptions.executable)
  destinationDir = file(yyy)
}

In Java that would be inheritance but you cannot inherit a task from a task in Gradle


回答1:


It will work if propGroup would be defined as a map:

def propGroup = [
   options: [
      fork: true,
      forkOptions: [
         executable: true
      ]
   ]
]

Then executable could be e.g. referred as:

propGroup.options.forkOptions.executable


来源:https://stackoverflow.com/questions/28458928/grouping-inheriting-properties-for-tasks-in-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!