Skip to content

Commit e99daf3

Browse files
authored
Resolves #1251: nextSnapshotIndexToIncrement should zero versions components right to it (#1262)
1 parent 7a05285 commit e99daf3

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/SetMojo.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
import java.text.DateFormat;
2424
import java.text.SimpleDateFormat;
2525
import java.util.ArrayList;
26-
import java.util.Arrays;
2726
import java.util.Date;
2827
import java.util.LinkedHashSet;
29-
import java.util.LinkedList;
3028
import java.util.List;
3129
import java.util.Map;
3230
import java.util.Objects;
@@ -35,6 +33,8 @@
3533
import java.util.TimeZone;
3634
import java.util.TreeMap;
3735
import java.util.regex.Pattern;
36+
import java.util.stream.Collectors;
37+
import java.util.stream.IntStream;
3838

3939
import org.apache.commons.lang3.StringUtils;
4040
import org.apache.maven.artifact.ArtifactUtils;
@@ -201,9 +201,11 @@ public class SetMojo extends AbstractVersionsUpdaterMojo {
201201
/**
202202
* <p>Specifies the version index to increment when using <code>nextSnapshot</code>.
203203
* Will increment the (1-based, counting from the left, or the most major component) index
204-
* of the snapshot version, e.g. for <code>-DnextSnapshotIndexToIncrement=1</code>
205-
* and the version being <code>1.2.3-SNAPSHOT</code>, the new version will become <code>2.2.3-SNAPSHOT.</code></p>
206-
* <p>Only valid with <code>nextSnapshot</code>.</p>
204+
* of the snapshot version, e.g. for @{code -DnextSnapshotIndexToIncrement=1} whilst
205+
* all lesser components will become 0's.</p>
206+
* <p>For example, the version being @{code 1.2.3-SNAPSHOT}, the new version will
207+
* become {@code 2.0.0-SNAPSHOT}.</p>
208+
* <p>Only valid with {@code nextSnapshot}.</p>
207209
*
208210
* @since 2.12
209211
*/
@@ -434,20 +436,29 @@ protected String getIncrementedVersion(String version, Integer nextSnapshotIndex
434436
throws MojoExecutionException {
435437
String versionWithoutSnapshot =
436438
version.endsWith(SNAPSHOT) ? version.substring(0, version.indexOf(SNAPSHOT)) : version;
437-
List<String> numbers = new LinkedList<>(Arrays.asList(versionWithoutSnapshot.split("\\.")));
439+
String[] versionComponents = versionWithoutSnapshot.split("\\.");
438440

439441
if (nextSnapshotIndexToIncrement == null) {
440-
nextSnapshotIndexToIncrement = numbers.size();
442+
nextSnapshotIndexToIncrement = versionComponents.length;
441443
} else if (nextSnapshotIndexToIncrement < 1) {
442444
throw new MojoExecutionException("nextSnapshotIndexToIncrement cannot be less than 1");
443-
} else if (nextSnapshotIndexToIncrement > numbers.size()) {
445+
} else if (nextSnapshotIndexToIncrement > versionComponents.length) {
444446
throw new MojoExecutionException(
445447
"nextSnapshotIndexToIncrement cannot be greater than the last version index");
446448
}
447-
int snapshotVersionToIncrement = Integer.parseInt(numbers.remove(nextSnapshotIndexToIncrement - 1));
448-
numbers.add(nextSnapshotIndexToIncrement - 1, String.valueOf(snapshotVersionToIncrement + 1));
449449

450-
return StringUtils.join(numbers.toArray(new String[0]), ".") + "-SNAPSHOT";
450+
int finalNextSnapshotIndexToIncrement = nextSnapshotIndexToIncrement;
451+
return IntStream.range(0, versionComponents.length)
452+
.mapToObj(i -> {
453+
if (i + 1 < finalNextSnapshotIndexToIncrement) {
454+
return versionComponents[i];
455+
} else if (i + 1 == finalNextSnapshotIndexToIncrement) {
456+
return String.valueOf(Integer.parseInt(versionComponents[i]) + 1);
457+
}
458+
return "0";
459+
})
460+
.collect(Collectors.joining("."))
461+
+ "-SNAPSHOT";
451462
}
452463

453464
private void applyChange(

versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,16 @@ public void testIssue1137() throws Exception {
265265
testLog.getLoggedMessages().stream().map(Triple::getMiddle).collect(Collectors.joining("\n")),
266266
not(containsString("Processing change of null:child")));
267267
}
268+
269+
@Test
270+
public void testNextSnapshotIndexToIncrement() throws MojoExecutionException {
271+
new SetMojo(artifactFactory, null, null, null, null, null) {
272+
{
273+
nextSnapshot = true;
274+
assertThat(getIncrementedVersion("1.1.1-SNAPSHOT", 1), is("2.0.0-SNAPSHOT"));
275+
assertThat(getIncrementedVersion("1.1.1-SNAPSHOT", 2), is("1.2.0-SNAPSHOT"));
276+
assertThat(getIncrementedVersion("1.1.1-SNAPSHOT", 3), is("1.1.2-SNAPSHOT"));
277+
}
278+
};
279+
}
268280
}

0 commit comments

Comments
 (0)