-
Notifications
You must be signed in to change notification settings - Fork 1
/
bump-version.groovy
executable file
·145 lines (121 loc) · 5.31 KB
/
bump-version.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env groovy
@Grab('com.github.zafarkhaja:java-semver:0.9.0')
@Grab('com.ximpleware:vtd-xml:2.11')
import com.github.zafarkhaja.semver.Version
import com.ximpleware.VTDGen
import com.ximpleware.VTDNav
import com.ximpleware.XMLModifier
import java.nio.file.Path
import java.nio.file.Paths
public class Bumper {
private static String parentPomPath
public static void main(String[] args) {
println "Script to bump semantic versions before branching"
println "Requirements: Maven 3.3+"
if (args == null || args.length < 2) {
println "pass 1) path to root maven pom file to bump all it's sub-modules versions"
println "2) path to parent pom file which contains dependency management to set versions of bumped modules"
println "In parent pom file module versions must be in <artifactId>.version format"
println "e.g if you have my-api artifact then you should name property \"my-api.version\""
}
println "Root pom: " + args[0]
println "parent pom: " + args[1]
parentPomPath = args[1]
processProject(args[0], "")
}
private static void processProject(String pomPath, String parentVersion) {
println "==========================="
Path xmlFilePath = Paths
.get(pomPath)
.toAbsolutePath()
println "Processing POM: " + xmlFilePath
Node root = new XmlParser().parse(xmlFilePath.toFile())
assert "project" == root.name().getLocalPart()
String rootModuleName = root.artifactId[0].text()
println "Module name: " + rootModuleName
String newVersion = bumpVersion(pomPath, parentVersion)
setParentPomDepsVersion(rootModuleName, newVersion)
if (isProjectPomPackaging(root)) {
def modules = root.modules.module
int modulesCount = modules.size()
for (int i = 0; i < modulesCount; i++) {
String moduleName = modules[i].value().text()
String modulePomPath = xmlFilePath.toAbsolutePath().getParent().toAbsolutePath().toString() +
File.separatorChar + moduleName + File.separatorChar + "pom.xml"
processProject(modulePomPath, newVersion)
}
}
}
private static void setParentPomDepsVersion(String rootModuleName, String newVersion) {
println "Set parent pom property version for " + rootModuleName + " with " + newVersion
VTDGen vg = new VTDGen()
XMLModifier xm = new XMLModifier()
if (vg.parseFile(parentPomPath, false))
{
VTDNav vn = vg.getNav()
xm.bind(vn)
if (vn.toElement(VTDNav.FIRST_CHILD, "properties"))
{
if (vn.toElement(VTDNav.FIRST_CHILD, rootModuleName + ".version"))
{
int i = vn.getText()
if (i != -1)
{
xm.updateToken(i, newVersion)
}
}
}
xm.output(new FileOutputStream(parentPomPath))
}
}
private static boolean isProjectPomPackaging(Node root) {
return "pom" == root.packaging.text()
}
private static String getProjectVersion(String pomPath) {
String command = "mvn help:evaluate -f " + pomPath +
" -Dexpression=project.version -o"
println "Executing: " + command
ProcessBuilder builder = new ProcessBuilder("bash", "-c", command)
Process process = builder.start()
InputStream stdout = process.getInputStream ()
BufferedReader reader = new BufferedReader (new InputStreamReader(stdout))
String line
while ((line = reader.readLine ()) != null) {
if (!line.startsWith("[") && !line.startsWith("Download")) {
println "Current ver: " + line
return line
}
}
throw new RuntimeException("Can't determine version")
}
private static String getProjectBumpedVersion(String oldVersion) {
try {
println "Trying to bump version " + oldVersion
Version ver = Version.valueOf(oldVersion)
String preReleaseVersion = ver.getPreReleaseVersion()
Version nextVersion = ver.incrementMajorVersion()
if (preReleaseVersion != null && !preReleaseVersion.isEmpty()) {
nextVersion = nextVersion.setPreReleaseVersion(preReleaseVersion)
}
println "Next ver:" + nextVersion
return nextVersion.toString()
}
catch (Exception e) {
println "Error parsing version " + oldVersion;
e.printStackTrace();
}
}
private static String bumpVersion(String pomPath, String parentVersion) {
String currentVersion = getProjectVersion(pomPath)
if (currentVersion.equals(parentVersion)) {
return parentVersion
}
String nextVersion = getProjectBumpedVersion(currentVersion)
String command = "mvn org.codehaus.mojo:versions-maven-plugin:2.1:set -f " + pomPath +
" -DgenerateBackupPoms=false -DnewVersion=" + nextVersion
println "Executing: " + command
int exitCode = ["bash", "-c", command].execute().waitFor()
// TODO: check exit code
return nextVersion
}
}