Skip to content

Commit 1d03956

Browse files
andrzejj0slawekjaranowski
authored andcommitted
Resolves #978
- added an it testing against a plain (not timestamped) -SNAPSHOT - added a unit test testing against a timestamped snapshot - added another comparison for the base version if it exists
1 parent 15d7a17 commit 1d03956

File tree

5 files changed

+109
-21
lines changed

5 files changed

+109
-21
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:use-releases
2+
invoker.mavenOpts = -DprocessParent=true
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>localhost</groupId>
7+
<artifactId>dummy-parent2</artifactId>
8+
<version>3.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>it-use-releases-issue-978-parent-snapshot</artifactId>
12+
<version>1</version>
13+
<packaging>pom</packaging>
14+
15+
<description>Test that parent is updated if it uses a snapshot version</description>
16+
17+
</project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import groovy.xml.XmlSlurper
2+
3+
def project = new XmlSlurper().parse( new File( basedir, 'pom.xml' ) )
4+
5+
assert project.parent.version == '3.0'

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

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -509,28 +509,49 @@ protected boolean updateDependencyVersion(
509509
boolean updated = false;
510510
if (isProcessingParent()
511511
&& getProject().getParent() != null
512-
&& DependencyComparator.INSTANCE.compare(
513-
dep,
514-
DependencyBuilder.newBuilder()
515-
.withGroupId(
516-
getProject().getParentArtifact().getGroupId())
517-
.withArtifactId(
518-
getProject().getParentArtifact().getArtifactId())
519-
.withVersion(
520-
getProject().getParentArtifact().getVersion())
521-
.build())
522-
== 0
523-
&& PomHelper.setProjectParentVersion(pom, newVersion)) {
524-
if (getLog().isDebugEnabled()) {
525-
getLog().debug("Made parent update from " + dep.getVersion() + " to " + newVersion);
512+
&& (DependencyComparator.INSTANCE.compare(
513+
dep,
514+
DependencyBuilder.newBuilder()
515+
.withGroupId(getProject()
516+
.getParentArtifact()
517+
.getGroupId())
518+
.withArtifactId(getProject()
519+
.getParentArtifact()
520+
.getArtifactId())
521+
.withVersion(getProject()
522+
.getParentArtifact()
523+
.getVersion())
524+
.build())
525+
== 0
526+
|| getProject().getParentArtifact().getBaseVersion() != null
527+
&& DependencyComparator.INSTANCE.compare(
528+
dep,
529+
DependencyBuilder.newBuilder()
530+
.withGroupId(getProject()
531+
.getParentArtifact()
532+
.getGroupId())
533+
.withArtifactId(getProject()
534+
.getParentArtifact()
535+
.getArtifactId())
536+
.withVersion(getProject()
537+
.getParentArtifact()
538+
.getBaseVersion())
539+
.build())
540+
== 0)) {
541+
if (PomHelper.setProjectParentVersion(pom, newVersion)) {
542+
if (getLog().isDebugEnabled()) {
543+
getLog().debug("Made parent update from " + dep.getVersion() + " to " + newVersion);
544+
}
545+
getChangeRecorder()
546+
.recordChange(DefaultDependencyChangeRecord.builder()
547+
.withKind(changeKind)
548+
.withDependency(dep)
549+
.withNewVersion(newVersion)
550+
.build());
551+
updated = true;
552+
} else {
553+
getLog().warn("Could not update parent: " + dep.toString() + " to " + newVersion);
526554
}
527-
getChangeRecorder()
528-
.recordChange(DefaultDependencyChangeRecord.builder()
529-
.withKind(changeKind)
530-
.withDependency(dep)
531-
.withNewVersion(newVersion)
532-
.build());
533-
updated = true;
534555
}
535556

536557
if (PomHelper.setDependencyVersion(

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,49 @@ public void testProcessParent()
105105
"1.0.0-SNAPSHOT", "1.0.0")));
106106
}
107107

108+
@Test
109+
public void testProcessTimestampedParent()
110+
throws MojoExecutionException, XMLStreamException, MojoFailureException, IllegalAccessException,
111+
VersionRetrievalException {
112+
setVariableValueToObject(mojo, "processParent", true);
113+
mojo.getProject().setParent(new MavenProject(new Model() {
114+
{
115+
setGroupId("default-group");
116+
setArtifactId("artifactA");
117+
setVersion("1.0.0-SNAPSHOT");
118+
}
119+
}));
120+
mojo.getProject()
121+
.setParentArtifact(
122+
new DefaultArtifact(
123+
"default-group",
124+
"artifactA",
125+
"1.0.0-20230912.080442-1",
126+
SCOPE_COMPILE,
127+
"pom",
128+
"default",
129+
null) {
130+
{
131+
setBaseVersion("1.0.0-SNAPSHOT");
132+
}
133+
});
134+
135+
try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) {
136+
pomHelper
137+
.when(() -> PomHelper.setProjectParentVersion(any(), anyString()))
138+
.thenReturn(true);
139+
pomHelper
140+
.when(() -> PomHelper.getRawModel(any(MavenProject.class)))
141+
.thenReturn(mojo.getProject().getModel());
142+
mojo.update(null);
143+
}
144+
assertThat(
145+
changeRecorder.getChanges(),
146+
hasItem(new DefaultDependencyVersionChange(
147+
"default-group", "artifactA",
148+
"1.0.0-SNAPSHOT", "1.0.0")));
149+
}
150+
108151
@Test
109152
public void testReplaceSnapshotWithRelease()
110153
throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException {

0 commit comments

Comments
 (0)