我的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/