From 98d28673994ac2882140660b6f9729811b4d5f87 Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Sat, 27 Aug 2022 16:24:11 +0200 Subject: [PATCH] Fixing #654: NPE in SetMojo when a dependency version is null --- pom.xml | 3 ++ .../org/codehaus/mojo/versions/SetMojo.java | 10 +++-- .../codehaus/mojo/versions/api/PomHelper.java | 31 ++++++--------- .../mojo/versions/utils/RegexUtils.java | 18 +++++---- .../codehaus/mojo/versions/SetMojoTest.java | 10 ++++- .../codehaus/mojo/set/versionless-01/pom.xml | 38 +++++++++++++++++++ 6 files changed, 77 insertions(+), 33 deletions(-) create mode 100644 src/test/resources/org/codehaus/mojo/set/versionless-01/pom.xml diff --git a/pom.xml b/pom.xml index 27792efd9..620ed09ab 100644 --- a/pom.xml +++ b/pom.xml @@ -81,6 +81,9 @@ antoon.johansson@gmail.com +1 + + Andrzej Jarmoniuk + diff --git a/src/main/java/org/codehaus/mojo/versions/SetMojo.java b/src/main/java/org/codehaus/mojo/versions/SetMojo.java index 421ffa6ea..9aae5bd90 100644 --- a/src/main/java/org/codehaus/mojo/versions/SetMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/SetMojo.java @@ -433,10 +433,12 @@ private void applyChange( MavenProject project, SortedMap reactor addChange( groupId, artifactId, oldVersion, newVersion ); // now fake out the triggering change - final Map.Entry current = PomHelper.getModelEntry( reactor, groupId, artifactId ); - current.getValue().setVersion( newVersion ); - - addFile( files, project, current.getKey() ); + Map.Entry current = PomHelper.getModelEntry( reactor, groupId, artifactId ); + if ( current != null ) + { + current.getValue().setVersion( newVersion ); + addFile( files, project, current.getKey() ); + } for ( Map.Entry sourceEntry : reactor.entrySet() ) { diff --git a/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java b/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java index e855bf173..e3d625d45 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java +++ b/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java @@ -816,7 +816,7 @@ else if ( inMatchScope && matchTargetRegex.matcher( path ).matches() ) { if ( "groupId".equals( elementName ) ) { - haveGroupId = groupId.equals( pom.getElementText().trim() ); + haveGroupId = pom.getElementText().trim().equals( groupId ); path = stack.pop(); } else if ( "artifactId".equals( elementName ) ) @@ -1539,12 +1539,14 @@ public static Map getChildModels( Map reactor, Str */ public static Model getModel( Map reactor, String groupId, String artifactId ) { - Map.Entry entry = getModelEntry( reactor, groupId, artifactId ); - return entry == null ? null : entry.getValue(); + return reactor.values().stream().filter( + model -> ( groupId == null || groupId.equals( getGroupId( model ) ) ) && artifactId.equals( + getArtifactId( model ) ) ).findAny().orElse( null ); } /** - * Returns the model that has the specified groupId and artifactId or null if no such model exists. + * Returns the model that has the specified groupId (if specified) + * and artifactId or null if no such model exists. * * @param reactor The map of models keyed by path. * @param groupId The groupId to match. @@ -1554,15 +1556,9 @@ public static Model getModel( Map reactor, String groupId, String public static Map.Entry getModelEntry( Map reactor, String groupId, String artifactId ) { - for ( Map.Entry entry : reactor.entrySet() ) - { - Model model = entry.getValue(); - if ( groupId.equals( getGroupId( model ) ) && artifactId.equals( getArtifactId( model ) ) ) - { - return entry; - } - } - return null; + return reactor.entrySet().stream().filter( + e -> ( groupId == null || groupId.equals( PomHelper.getGroupId( e.getValue() ) ) ) && artifactId.equals( + PomHelper.getArtifactId( e.getValue() ) ) ).findAny().orElse( null ); } /** @@ -1578,15 +1574,12 @@ public static int getReactorParentCount( Map reactor, Model model { return 0; } - else + Model parentModel = getModel( reactor, model.getParent().getGroupId(), model.getParent().getArtifactId() ); + if ( parentModel == null ) { - Model parentModel = getModel( reactor, model.getParent().getGroupId(), model.getParent().getArtifactId() ); - if ( parentModel != null ) - { - return getReactorParentCount( reactor, parentModel ) + 1; - } return 0; } + return getReactorParentCount( reactor, parentModel ) + 1; } /** diff --git a/src/main/java/org/codehaus/mojo/versions/utils/RegexUtils.java b/src/main/java/org/codehaus/mojo/versions/utils/RegexUtils.java index 7a7496767..a9da7cfb4 100644 --- a/src/main/java/org/codehaus/mojo/versions/utils/RegexUtils.java +++ b/src/main/java/org/codehaus/mojo/versions/utils/RegexUtils.java @@ -97,16 +97,18 @@ public static String quote( String s ) public static int getWildcardScore( String wildcardRule ) { int score = 0; - for ( int i = 0; i < wildcardRule.length(); i++ ) + if ( wildcardRule != null ) { - char c = wildcardRule.charAt( i ); - if ( c == '?' ) - { - score++; - } - else if ( c == '*' ) + for ( char c : wildcardRule.toCharArray() ) { - score += 1000; + if ( c == '?' ) + { + score++; + } + else if ( c == '*' ) + { + score += 1000; + } } } return score; diff --git a/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java b/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java index b078dbecc..fb674768a 100644 --- a/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java @@ -4,14 +4,14 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.project.MavenProject; +import org.codehaus.mojo.versions.utils.BaseMojoTestCase; import org.junit.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.fail; -public class SetMojoTest +public class SetMojoTest extends BaseMojoTestCase { @Test public void testGetIncrementedVersion() throws MojoExecutionException @@ -92,4 +92,10 @@ public void testNextSnapshotIndexWithoutNextSnapshot() throws MojoFailureExcepti } } + @Test + public void testVersionlessDependency() throws Exception + { + SetMojo myMojo = createMojo( "set", "src/test/resources/org/codehaus/mojo/set/versionless-01/pom.xml" ); + myMojo.execute(); + } } diff --git a/src/test/resources/org/codehaus/mojo/set/versionless-01/pom.xml b/src/test/resources/org/codehaus/mojo/set/versionless-01/pom.xml new file mode 100644 index 000000000..22956c517 --- /dev/null +++ b/src/test/resources/org/codehaus/mojo/set/versionless-01/pom.xml @@ -0,0 +1,38 @@ + + 4.0.0 + default-group + default-artifact + 1.0 + pom + + + + + localhost + dummy-api + 1.1 + + + + + + + localhost + dummy-api + + + + + + + versions-maven-plugin + + onchange + dummy-api + 2.0 + + + + + \ No newline at end of file