1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.issue.util;
17
18 import org.apache.maven.artifact.versioning.ArtifactVersion;
19 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
20 import org.apache.maven.project.MavenProject;
21
22
23
24
25 public final class VersionHelper
26 {
27
28
29
30
31
32
33
34
35
36
37
38 public static final String MAJOR = "MAJOR";
39
40
41
42
43
44
45
46 public static final String MINOR = "MINOR";
47
48
49
50
51
52
53
54 public static final String MICRO = "MICRO";
55
56
57
58
59
60
61
62 public static final String CURRENT = "CURRENT";
63
64
65
66
67
68
69
70 public static final String SNAPSHOT = "SNAPSHOT";
71
72
73
74
75
76
77 private final MavenProject project;
78
79
80
81
82
83
84
85
86
87
88 public VersionHelper(final MavenProject project)
89 {
90 this.project = project;
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 public String calculateNextVersion(final String version)
111 {
112 final String nextVersion;
113 if (MAJOR.equals(version))
114 {
115 nextVersion = calcMajor();
116 }
117 else if (MINOR.equals(version))
118 {
119 nextVersion = calcMinor();
120 }
121 else if (MICRO.equals(version))
122 {
123 nextVersion = calcMicro();
124 }
125 else if (CURRENT.equals(version))
126 {
127 nextVersion = getVersionStrippedFromSnapshotSuffix(project.getVersion());
128 }
129 else if (SNAPSHOT.equals(version))
130 {
131 nextVersion = project.getVersion();
132 }
133 else
134 {
135 nextVersion = version;
136 }
137
138 return nextVersion;
139 }
140
141
142
143
144
145
146
147
148 public static String getVersionStrippedFromSnapshotSuffix(final String version)
149 {
150 if (version.endsWith("-SNAPSHOT"))
151 {
152 return version.substring(0, version.length() - 9);
153 }
154 return version;
155 }
156
157 private String calcMajor()
158 {
159 final String nextVersion;
160 final ArtifactVersion artifactVersion = createArtifactVersion();
161 if (artifactVersion == null)
162 {
163 return null;
164 }
165 final int next = artifactVersion.getMajorVersion() + 1;
166 nextVersion = next + ".0.0";
167 return nextVersion;
168 }
169
170 private String calcMinor()
171 {
172 final String nextVersion;
173 final ArtifactVersion artifactVersion = createArtifactVersion();
174 if (artifactVersion == null)
175 {
176 return null;
177 }
178 final int next = artifactVersion.getMinorVersion() + 1;
179 nextVersion =
180 String.valueOf(artifactVersion.getMajorVersion()) + '.' + next + ".0";
181 return nextVersion;
182 }
183
184 private String calcMicro()
185 {
186 final String nextVersion;
187 final ArtifactVersion artifactVersion = createArtifactVersion();
188 if (artifactVersion == null)
189 {
190 return null;
191 }
192 final int next = artifactVersion.getIncrementalVersion() + 1;
193 nextVersion =
194 String.valueOf(artifactVersion.getMajorVersion()) + '.'
195 + artifactVersion.getMinorVersion() + '.' + next;
196 return nextVersion;
197 }
198
199 private ArtifactVersion createArtifactVersion()
200 {
201 final String version = project.getVersion();
202 final ArtifactVersion artifactVersion = new DefaultArtifactVersion(version);
203 if (notRecognized(artifactVersion))
204 {
205 return null;
206 }
207 return artifactVersion;
208 }
209
210 private static boolean notRecognized(final ArtifactVersion artifactVersion)
211 {
212 return (artifactVersion.getMajorVersion() == 0
213 && artifactVersion.getMinorVersion() == 0 && artifactVersion
214 .getIncrementalVersion() == 0);
215 }
216
217
218
219 }