如何在gradlew help --task文档中添加“选项”

时间:2014-09-26 18:58:33

标签: android gradle documentation version task

我在build.gradle文件中进行了以下设置:

// Task designed to bump version numbers. This should be the first task run     
// after a new release branch is created.                                       
task bumpVersion(description: 'Bumps the version number of the current Android release. Should be used as a standalone task, and should only be the first task called after creating a release branch.', group: 'Management') << {
  Properties props = new Properties();                                          
  File propsFile = new File('gradle.properties');                               
  props.load(propsFile.newDataInputStream());                                   
  def currentVersionCode = props.getProperty("CORE_VERSION_CODE") as int;       
  def currentVersionName = props.getProperty("CORE_VERSION_NAME") as String;    
  def intPortionsOfVersionName = currentVersionName.tokenize('.').toArray();    
  def leastSignificantPortion = intPortionsOfVersionName[intPortionsOfVersionName.length - 1] as int;

  def newVersionCode = currentVersionCode + 1;                                  
  def newVersionName = "";                                                      
  if (!project.hasProperty('newVersion')) {                                     
    leastSignificantPortion = leastSignificantPortion + 1;                      
    intPortionsOfVersionName[intPortionsOfVersionName.length - 1] = leastSignificantPortion;
    newVersionName = intPortionsOfVersionName.collect{ it }.join(".");          
  } else {                                                                      
    newVersionName = project.getProperty('newVersion');                         
  }                                                                             

  props.setProperty("CORE_VERSION_NAME", newVersionName as String);             
  props.setProperty("CORE_VERSION_CODE", newVersionCode as String);             

  props.store(propsFile.newWriter(), null);                                     
}

newVersionName = project.getProperty('newVersion')行下,我尝试获取名为“newVersion”的属性(如果存在),如果不可用,则尝试获取最低有效数字。

这很好用,但我想要做的是添加一种在文档中指定此选项的方法(即gradle help --task bumpVersion)。例如,如果我运行gradle help --task help,它会给我:

:help
Detailed task information for help

Path
     :help

Type
     Help (org.gradle.configuration.Help)

Options
     --task     The task, detailed help is requested for.

Description
     Displays a help message

请注意“选项”部分下的“--task”。我想知道如何使用我自己的代码执行此操作。

1 个答案:

答案 0 :(得分:1)

可以使用@Option注释完成此操作。

@Option(option = "version", description = "Version number to use")
public void setVersion(String version) { ... }

注意:这是一个内部API,因此可能会发生变化。

修改:可能忘记提及您必须以custom task class身份执行任务才能利用此功能。