我想从1.0.0增加项目的版本号。每当通过bash命令进行新构建时自动到1.0.1。我只需要增加路径号和其他人在手动构建期间手动增加。
我想改变
这个:
version=1.0.0
要 这个:
version=1.0.1
使用gradle任务。 任何帮助我怎么能这样做。 有没有办法使用正则表达式或使用子字符串函数来更新它。
答案 0 :(得分:4)
以下是一个示例任务:
version='1.0.0' //version we need to change
task increment<<{
def v=buildFile.getText().find(version) //get this build file's text and extract the version value
String minor=v.substring(v.lastIndexOf('.')+1) //get last digit
int m=minor.toInteger()+1 //increment
String major=v.substring(0,v.length()-1) //get the beginning
//println m
String s=buildFile.getText().replaceFirst("version='$version'","version='"+major+m+"'")
//println s
buildFile.setText(s) //replace the build file's text
}
多次运行此任务,您应该会看到版本更改。
变体:
version='1.0.0'
task incrementVersion<<{
String minor=version.substring(version.lastIndexOf('.')+1)
int m=minor.toInteger()+1
String major=version.substring(0,version.length()-1)
String s=buildFile.getText().replaceFirst("version='$version'","version='"+major+m+"'")
buildFile.setText(s)
}
答案 1 :(得分:1)
以下解决方案不会产生evern最后一个数字超过9-10的问题等等
version='1.0.11.1001'
task incrementrevsion{
def v = version
println v
String minor=v.substring(v.lastIndexOf('.')+1) //get last digit
int m=minor.toInteger()+1 //increment
println m
String major=v.substring(0,v.lastIndexOf(".")); //get the beginning
println major
String s=buildFile.getText().replaceFirst("version='$version'","version='"+major+ "." +m+"'")
//println s
buildFile.setText(s) //replace the build file's text
}
答案 2 :(得分:1)
您还可以使用带有增量矩阵的split,可以根据更改量进行更改:
def version = '1.0.0'
def incstep = '0.0.1'.split(/\./).collect{it.toInteger()}
def indexedVersionList = version.split(/\./).toList().withIndex()
def updatedVersionList = indexedVersionList.collect{num, idx -> num.toInteger()+incstep[idx]}
def updatedVersion = updatedVersionList.join(".")
答案 3 :(得分:0)
def patch = version.substring(version.lastIndexOf('.') + 1)
def p = patch.toInteger() + 1
def major = version.substring(0, version.length() - p.toString().length())
def s = buildFile.getText().replaceFirst("version = '$version'", "version = '" + major + p + "'")
buildFile.setText(s)
与Alexiy的答案唯一不同的是,第3行包含m.toString()。length(),好像次要版本&gt; 10,即1.0.12,您将使用该方法将其更改为1.0.113。我们需要计算次要版本的长度,而不是仅砍掉1个符号。
还有一件事,通常最后一个数字称为补丁,次要是中间数字:)
答案 4 :(得分:0)
对于我来说,请使用此解决方案。 您需要将此代码添加到 build.gradle 文件中:
version='1.0.1'
tasks.register("incrementVersion") {
doLast {
String minor = version.substring(version.lastIndexOf('.') + 1)
int m = minor.toInteger() + 1
String major = version.substring(0,version.length() - 1)
String s = buildFile.getText().replaceFirst("version = '$version'","version = '" + major + m + "'")
buildFile.setText(s)
print version
}
}
答案 5 :(得分:0)
这是Gradle(Android项目)中版本颠簸的自定义任务:
class Version {
private int major
private int minor
private int patch
private int code
Version(int code, String version) {
this.code = code
def (major, minor, patch) = version.tokenize('.')
this.major = major.toInteger()
this.minor = minor.toInteger()
this.patch = patch.toInteger()
}
@SuppressWarnings("unused")
void bumpMajor() {
major += 1
minor = 0
patch = 0
code += 1
}
@SuppressWarnings("unused")
void bumpMinor() {
minor += 1
patch = 0
code += 1
}
@SuppressWarnings("unused")
void bumpPatch() {
patch += 1
code += 1
}
String getName() { "$major.$minor.$patch" }
int getCode() { code }
}
tasks.addRule("Pattern: bump<TYPE>Version") { String taskName ->
if (taskName.matches("bump(Major|Minor|Patch)Version")) {
task(taskName) {
doLast {
String type = (taskName - 'bump' - 'Version')
println "Bumping ${type.toLowerCase()} version…"
int oldVersionCode = android.defaultConfig.versionCode
String oldVersionName = android.defaultConfig.versionName
version = new Version(oldVersionCode, oldVersionName)
version."bump$type"()
String newVersionName = version.getName()
String newVersionCode = version.getCode()
println "$oldVersionName ($oldVersionCode) → $newVersionName ($newVersionCode)"
def updated = buildFile.getText()
updated = updated.replaceFirst("versionName '$oldVersionName'", "versionName '$newVersionName'")
updated = updated.replaceFirst("versionCode $oldVersionCode", "versionCode $newVersionCode")
buildFile.setText(updated)
}
}
}
}
有关完整性,请参见此Kanji learning Android app。
需要以下格式(注意单引号):
android {
defaultConfig {
versionCode 3
versionName '0.3.13'
}
}
$ ./gradlew bumpPatchVersion
> Task :app:bumpPatchVersion
Bumping patch version…
0.3.13 (3) → 0.3.14 (4)
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
$ ./gradlew bumpMinorVersion
> Task :app:bumpMinorVersion
Bumping minor version…
0.3.14 (4) → 0.4.0 (5)
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
$ ./gradlew bumpMajorVersion
> Task :app:bumpMajorVersion
Bumping major version…
0.4.0 (5) → 1.0.0 (6)
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
答案 6 :(得分:0)
我知道我发布此消息的时间很晚,但是上面提到的答案一直有效到'1.0.99'。之后,它开始出现异常。
如果仍然有人感兴趣,我发现了另一种方法。
task increment {
def v = buildFile.getText().find(version)
def (major, minor, patch) = v.tokenize('.')
int newPatch = patch.toInteger() + 1
String newVersion = major + "." + minor + "." + newPatch
String updatedVersion = buildFile.getText().replaceFirst("version='"+v+"'","version='"+newVersion+"'")
buildFile.setText(updatedVersion)
}
答案 7 :(得分:0)
这是一个与 KTS(Kotlin Script)相同想法的例子。
val newVersion: String? by project
tasks.register("bumpVersion") {
this.doFirst {
println("Old version $version")
val newVersion = takeIf { newVersion.isNullOrBlank() }?.let {
val versionArray = version.toString().split(".")
"${versionArray[0]}.${versionArray[1]}.${versionArray.last().toInt().plus(1)}"
} ?: newVersion
buildFile.readText().apply {
println("Bump to $newVersion")
val content = this.replaceFirst("version = \"$version\"", "version = \"$newVersion\"")
buildFile.writeText(content)
}
}
}
自动增加补丁或者可以发送新版本作为属性。
./gradlew bumpVersion -PnewVersion=0.2.0
答案 8 :(得分:0)
这就是我使用 Kotlin DSL 的方式:
/**
* Usage: gradlew incrementVersion [-Pmode=major|minor|patch]
*/
tasks.create("incrementVersion") {
description = "Increments the version to make the app ready for next release."
doLast {
var (major, minor, patch) = project.version.toString().split(".")
val mode = project.properties["mode"]?.toString()?.toLowerCaseAsciiOnly()
if (mode == "major") {
major = (major.toInt() + 1).toString()
minor = "0"
patch = "0"
} else if (mode == "minor") {
minor = (minor.toInt() + 1).toString()
patch = "0"
} else {
patch = (patch.toInt() + 1).toString()
}
val newVersion = "$major.$minor.$patch"
val newContent =
buildFile.readText().replaceFirst(Regex("version = .+"), "version = \"$newVersion\"")
buildFile.writeText(newContent)
}
}
鉴于您的构建脚本中有这样的内容:
version = "1.2.3"
...而版本的补丁部分只是一个数字(如主要和次要部分)。
答案 9 :(得分:-1)
我的解决方案,其中版本将由参数设置。
version = '4.0.0' // I have whitespaces between the equals-sign
task setVersion << {
group = 'Versioning'
description = "Sets the version to the new number specified with -PnewVersion=\'x.x.x\'"
println version
if(project.hasProperty('newVersion')) {
println 'Set Project to new Version '+newVersion
String s=buildFile.getText().replaceFirst("version = '$version'","version = '"+newVersion+"'")
buildFile.setText(s)
}
}
使用以下方法调用Gradle任务:
gradle setVersion -PnewVersion='5.1.1'