Skip to content

Commit f12e5ca

Browse files
eleftheriassjohnr
authored andcommitted
Add gradle task for updating to next development version
Issue gh-10461
1 parent 946e24e commit f12e5ca

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

buildSrc/src/main/java/org/springframework/security/convention/versions/UpdateProjectVersionPlugin.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,14 @@ public void execute(UpdateProjectVersionTask updateProjectVersionTask) {
3434
updateProjectVersionTask.setCommit("true".equals(project.findProperty("commit")));
3535
}
3636
});
37+
project.getTasks().register("updateToSnapshotVersion", UpdateToSnapshotVersionTask.class, new Action<UpdateToSnapshotVersionTask>() {
38+
@Override
39+
public void execute(UpdateToSnapshotVersionTask updateToSnapshotVersionTask) {
40+
updateToSnapshotVersionTask.setGroup("Release");
41+
updateToSnapshotVersionTask.setDescription(
42+
"Updates the project version to the next snapshot in gradle.properties and optionally commits the changes");
43+
updateToSnapshotVersionTask.setCommit("true".equals(project.findProperty("commit")));
44+
}
45+
});
3746
}
3847
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2019-2022 the original author or authors.
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+
17+
package org.springframework.security.convention.versions;
18+
19+
import org.gradle.api.DefaultTask;
20+
import org.gradle.api.Project;
21+
import org.gradle.api.tasks.Input;
22+
import org.gradle.api.tasks.Optional;
23+
import org.gradle.api.tasks.TaskAction;
24+
25+
import java.io.File;
26+
import java.io.FileNotFoundException;
27+
import java.util.regex.Matcher;
28+
import java.util.regex.Pattern;
29+
30+
public abstract class UpdateToSnapshotVersionTask extends DefaultTask {
31+
32+
private static final String RELEASE_VERSION_PATTERN = "^([0-9]+)\\.([0-9]+)\\.([0-9]+)(-M\\d+|-RC\\d+)?$";
33+
34+
@Input
35+
@Optional
36+
private Boolean commit;
37+
38+
@TaskAction
39+
public void updateToSnapshotVersion() {
40+
String currentVersion = getProject().getVersion().toString();
41+
File gradlePropertiesFile = getProject().getRootProject().file(Project.GRADLE_PROPERTIES);
42+
if (!gradlePropertiesFile.exists()) {
43+
return;
44+
}
45+
String nextVersion = calculateNextSnapshotVersion(currentVersion);
46+
System.out.println("Updating the project version in " + Project.GRADLE_PROPERTIES + " from " + currentVersion
47+
+ " to " + nextVersion);
48+
FileUtils.replaceFileText(gradlePropertiesFile, (gradlePropertiesText) -> {
49+
gradlePropertiesText = gradlePropertiesText.replace("version=" + currentVersion, "version=" + nextVersion);
50+
return gradlePropertiesText;
51+
});
52+
if (this.commit) {
53+
System.out.println("Committing the version update");
54+
File rootDir = getProject().getRootDir();
55+
String commitMessage = "Next development version";
56+
CommandLineUtils.runCommand(rootDir, "git", "commit", "-am", commitMessage);
57+
}
58+
}
59+
60+
private String calculateNextSnapshotVersion(String currentVersion) {
61+
Pattern releaseVersionPattern = Pattern.compile(RELEASE_VERSION_PATTERN);
62+
Matcher releaseVersion = releaseVersionPattern.matcher(currentVersion);
63+
64+
if (releaseVersion.find()) {
65+
String majorSegment = releaseVersion.group(1);
66+
String minorSegment = releaseVersion.group(2);
67+
String patchSegment = releaseVersion.group(3);
68+
String modifier = releaseVersion.group(4);
69+
if (modifier == null) {
70+
patchSegment = String.valueOf(Integer.parseInt(patchSegment) + 1);
71+
}
72+
System.out.println("modifier = " + modifier);
73+
return String.format("%s.%s.%s-SNAPSHOT", majorSegment, minorSegment, patchSegment);
74+
}
75+
else {
76+
throw new IllegalStateException(
77+
"Cannot calculate next snapshot version because the current project version does not conform to the expected format");
78+
}
79+
}
80+
81+
public Boolean getCommit() {
82+
return commit;
83+
}
84+
85+
public void setCommit(Boolean commit) {
86+
this.commit = commit;
87+
}
88+
89+
}

0 commit comments

Comments
 (0)