android - 如何将 'Options'添加到gradlew帮助-任务文档

标签 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”在“选项”部分下的情况。我想知道如何使用我自己的代码执行此操作。

最佳答案

这可以使用@Option批注完成。

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

注意:这是一个内部API,因此可能会更改。

编辑:可能忘记了提及您必须将任务作为custom task class来实现才能利用此功能。

关于android - 如何将 'Options'添加到gradlew帮助-任务文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26066527/

相关文章:

java - Spring Boot Gradle 快速构建,跳过非必要内容

python - 如何使用代码中的注释从 python 生成 swagger 规范文件 (json)?

java - 限制 View 滚动上的 OnBindViewHolder

android - 如何在 Android 上绘制下一个东西之前暂停 5 秒?

android - 是否可以通过页内链接在 iOS 和 Android 中调用 native 共享功能?

spring - 如何在带有接线的 Spring Boot 应用程序中运行特定的类/实用程序?

java - 为游戏随机生成 Sprite ?

spring-boot - Unresolved reference : SpringExtension using @ExtendWith annotation

documentation - SPDH协议(protocol)文档

documentation - 如何使用 aws cdk 记录 rest api