Skip to content

Commit 4ba110c

Browse files
authored
Add task to bump versions in gradle (#497)
1 parent 0170a93 commit 4ba110c

File tree

4 files changed

+62
-31
lines changed

4 files changed

+62
-31
lines changed

build.gradle

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.regex.Pattern
2+
3+
/** Defines a custom task for updating version numbers. */
4+
abstract class UpdateVersionTask extends DefaultTask {
5+
6+
static final def VERSION_REGEX = "([0-9]+)\\.([0-9]+)\\.([0-9]+)(-SNAPSHOT)?"
7+
8+
@Input
9+
String newVersion
10+
11+
@InputFile
12+
File propertiesFile = project.file("${project.projectDir}/gradle.properties")
13+
14+
@TaskAction
15+
def update() {
16+
if (newVersion == null) {
17+
throw new GradleException("Need to specify newVersion")
18+
}
19+
if (propertiesFile == null) {
20+
throw new GradleException("Need to specify gradlePropertiesFile")
21+
}
22+
if (!newVersion.matches(VERSION_REGEX)) {
23+
throw new GradleException("Invalid format for -PnewVersion. Expecting: ${VERSION_REGEX}")
24+
}
25+
def toSet = new Properties()
26+
propertiesFile.withInputStream { toSet.load(it) }
27+
toSet.version = newVersion
28+
propertiesFile.withOutputStream { toSet.store(it, "See LICENSE for license terms") }
29+
}
30+
}
31+
32+
/** Updates the version to version specified with -PnewVersion=foo. */
33+
task updateVersion(type: UpdateVersionTask) {
34+
newVersion = project.properties.newVersion
35+
}
36+
37+
/** Updates the version to the next snapshot. */
38+
task updateVersionToNextSnapshot(type: UpdateVersionTask) {
39+
def current = new Properties()
40+
propertiesFile.withInputStream { current.load(it) }
41+
def matcher = Pattern.compile(VERSION_REGEX).matcher(current.version)
42+
if (!matcher.find()) {
43+
throw new GradleException("Current version cannot be parsed: ${current.version} expected match of: ${VERSION_REGEX}")
44+
}
45+
def toSet = "${matcher.group(1)}.${matcher.group(2)}.${Integer.parseInt(matcher.group(3)) + 1}-SNAPSHOT"
46+
newVersion = toSet
47+
}

buildSrc/src/main/groovy/com.google.api-ads.java-conventions.gradle

-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ repositories {
3535
mavenLocal()
3636
}
3737

38-
group = 'com.google.api-ads'
39-
version = '15.0.0'
4038
java.sourceCompatibility = JavaVersion.VERSION_1_8
4139
java.targetCompatibility = JavaVersion.VERSION_1_8
4240

google-ads/build.gradle

+9-9
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,10 @@ task copyLicenses(type: Copy) {
174174
dependsOn 'generateLicenses'
175175
mustRunAfter 'createThirdPartyDirectory'
176176

177-
def inputFiles = tasks.generateLicenses.outputs.files.asFileTree.files
178-
.findAll { it.path.contains "dependency-license"}
179-
180-
from inputFiles
177+
from new File(tasks.generateLicenses.xmlDestination.getAbsolutePath(), "dependency-license.json"),
178+
new File(tasks.generateLicenses.xmlDestination.getAbsolutePath(), "dependency-license.html"),
179+
new File(tasks.generateLicenses.xmlDestination.getAbsolutePath(), "dependency-license.xml")
181180
into tasks.generateThirdPartyDirectory.thirdPartyDir
182-
// Rename files to LICENSES.*
183181
eachFile {
184182
if (!path.contains("dependency-license")) {
185183
throw new GradleException(
@@ -191,11 +189,13 @@ task copyLicenses(type: Copy) {
191189
// Fails the build if we didn't copy everything. Gradle will fail silently if
192190
// it cannot copy one of the inputs which is dangerous when you're dealing
193191
// with legal obligations.
194-
if (inputFiles.size() != inputs.sourceFiles.size()) {
192+
//
193+
// We're expecting exactly 3 input files, a JSON, XML and HTML version of
194+
// the license report.
195+
if (3 != inputs.sourceFiles.size()) {
195196
throw new GradleException(
196-
"Failed to locate files to copy from. Expected: "
197-
+ inputFiles
198-
+ " but was: " + inputs.sourceFiles.toList())
197+
"Failed to locate files to copy from. Expected: 3 but was: "
198+
+ inputs.sourceFiles.toList())
199199
}
200200
}
201201
}

gradle.properties

+6-20
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
1-
#
2-
# Copyright 2020 Google LLC
3-
#
4-
# Licensed under the Apache License, Version 2.0 (the "License");
5-
# you may not use this file except in compliance with the License.
6-
# You may obtain a copy of the License at
7-
#
8-
# https://www.apache.org/licenses/LICENSE-2.0
9-
#
10-
# Unless required by applicable law or agreed to in writing, software
11-
# distributed under the License is distributed on an "AS IS" BASIS,
12-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
# See the License for the specific language governing permissions and
14-
# limitations under the License.
15-
#
16-
# These are global settings for the gradle build process.
17-
18-
org.gradle.jvmargs=-Xmx5g -XX:MaxMetaspaceSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
1+
#See LICENSE for license terms
2+
#Fri Nov 05 19:01:55 GMT 2021
193
org.gradle.caching=true
20-
org.gradle.console=rich
214
org.gradle.parallel=true
22-
5+
org.gradle.jvmargs=-Xmx5g -XX\:MaxMetaspaceSize\=2048m -XX\:+HeapDumpOnOutOfMemoryError -Dfile.encoding\=UTF-8
6+
org.gradle.console=rich
7+
version=15.0.1-SNAPSHOT
8+
group=com.google.api-ads

0 commit comments

Comments
 (0)