Skip to content

Commit 7dad7a9

Browse files
committed
Fixing mojohaus#654: NPE in SetMojo when a dependency version is null
1 parent 44431b8 commit 7dad7a9

File tree

6 files changed

+76
-38
lines changed

6 files changed

+76
-38
lines changed

pom.xml

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@
8181
<email>[email protected]</email>
8282
<timezone>+1</timezone>
8383
</contributor>
84+
<contributor>
85+
<name>Andrzej Jarmoniuk</name>
86+
</contributor>
8487
</contributors>
8588

8689
<prerequisites>

src/main/java/org/codehaus/mojo/versions/SetMojo.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,12 @@ private void applyChange( MavenProject project, SortedMap<String, Model> reactor
433433
addChange( groupId, artifactId, oldVersion, newVersion );
434434
// now fake out the triggering change
435435

436-
final Map.Entry<String, Model> current = PomHelper.getModelEntry( reactor, groupId, artifactId );
437-
current.getValue().setVersion( newVersion );
438-
439-
addFile( files, project, current.getKey() );
436+
Map.Entry<String, Model> current = PomHelper.getModelEntry( reactor, groupId, artifactId );
437+
if ( current != null )
438+
{
439+
current.getValue().setVersion( newVersion );
440+
addFile( files, project, current.getKey() );
441+
}
440442

441443
for ( Map.Entry<String, Model> sourceEntry : reactor.entrySet() )
442444
{

src/main/java/org/codehaus/mojo/versions/api/PomHelper.java

+12-19
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ else if ( inMatchScope && matchTargetRegex.matcher( path ).matches() )
816816
{
817817
if ( "groupId".equals( elementName ) )
818818
{
819-
haveGroupId = groupId.equals( pom.getElementText().trim() );
819+
haveGroupId = pom.getElementText().trim().equals( groupId );
820820
path = stack.pop();
821821
}
822822
else if ( "artifactId".equals( elementName ) )
@@ -1539,12 +1539,14 @@ public static Map<String, Model> getChildModels( Map<String, Model> reactor, Str
15391539
*/
15401540
public static Model getModel( Map<String, Model> reactor, String groupId, String artifactId )
15411541
{
1542-
Map.Entry<String, Model> entry = getModelEntry( reactor, groupId, artifactId );
1543-
return entry == null ? null : entry.getValue();
1542+
return reactor.values().stream().filter(
1543+
model -> ( groupId == null || groupId.equals( getGroupId( model ) ) ) && artifactId.equals(
1544+
getArtifactId( model ) ) ).findAny().orElse( null );
15441545
}
15451546

15461547
/**
1547-
* Returns the model that has the specified groupId and artifactId or <code>null</code> if no such model exists.
1548+
* Returns the model that has the specified groupId (if specified)
1549+
* and artifactId or <code>null</code> if no such model exists.
15481550
*
15491551
* @param reactor The map of models keyed by path.
15501552
* @param groupId The groupId to match.
@@ -1554,15 +1556,9 @@ public static Model getModel( Map<String, Model> reactor, String groupId, String
15541556
public static Map.Entry<String, Model> getModelEntry( Map<String, Model> reactor, String groupId,
15551557
String artifactId )
15561558
{
1557-
for ( Map.Entry<String, Model> entry : reactor.entrySet() )
1558-
{
1559-
Model model = entry.getValue();
1560-
if ( groupId.equals( getGroupId( model ) ) && artifactId.equals( getArtifactId( model ) ) )
1561-
{
1562-
return entry;
1563-
}
1564-
}
1565-
return null;
1559+
return reactor.entrySet().stream().filter(
1560+
e -> ( groupId == null || groupId.equals( PomHelper.getGroupId( e.getValue() ) ) ) && artifactId.equals(
1561+
PomHelper.getArtifactId( e.getValue() ) ) ).findAny().orElse( null );
15661562
}
15671563

15681564
/**
@@ -1578,15 +1574,12 @@ public static int getReactorParentCount( Map<String, Model> reactor, Model model
15781574
{
15791575
return 0;
15801576
}
1581-
else
1577+
Model parentModel = getModel( reactor, model.getParent().getGroupId(), model.getParent().getArtifactId() );
1578+
if ( parentModel == null )
15821579
{
1583-
Model parentModel = getModel( reactor, model.getParent().getGroupId(), model.getParent().getArtifactId() );
1584-
if ( parentModel != null )
1585-
{
1586-
return getReactorParentCount( reactor, parentModel ) + 1;
1587-
}
15881580
return 0;
15891581
}
1582+
return getReactorParentCount( reactor, parentModel ) + 1;
15901583
}
15911584

15921585
/**

src/main/java/org/codehaus/mojo/versions/utils/RegexUtils.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,18 @@ public static String quote( String s )
9797
public static int getWildcardScore( String wildcardRule )
9898
{
9999
int score = 0;
100-
for ( int i = 0; i < wildcardRule.length(); i++ )
100+
if ( wildcardRule != null )
101101
{
102-
char c = wildcardRule.charAt( i );
103-
if ( c == '?' )
104-
{
105-
score++;
106-
}
107-
else if ( c == '*' )
102+
for ( char c : wildcardRule.toCharArray() )
108103
{
109-
score += 1000;
104+
if ( c == '?' )
105+
{
106+
score++;
107+
}
108+
else if ( c == '*' )
109+
{
110+
score += 1000;
111+
}
110112
}
111113
}
112114
return score;

src/test/java/org/codehaus/mojo/versions/SetMojoTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44
import org.apache.maven.plugin.MojoExecutionException;
55
import org.apache.maven.plugin.MojoFailureException;
66
import org.apache.maven.project.MavenProject;
7-
import org.junit.Test;
7+
import org.codehaus.mojo.versions.utils.BaseMojoTestCase;
88

99
import static org.hamcrest.MatcherAssert.assertThat;
1010
import static org.hamcrest.Matchers.containsString;
1111
import static org.hamcrest.Matchers.is;
12-
import static org.junit.Assert.fail;
1312

14-
public class SetMojoTest
13+
public class SetMojoTest extends BaseMojoTestCase
1514
{
16-
@Test
1715
public void testGetIncrementedVersion() throws MojoExecutionException
1816
{
1917
new SetMojo()
@@ -28,7 +26,6 @@ public void testGetIncrementedVersion() throws MojoExecutionException
2826
};
2927
}
3028

31-
@Test
3229
public void testNextSnapshotIndexLowerBound()
3330
{
3431
new SetMojo()
@@ -48,7 +45,6 @@ public void testNextSnapshotIndexLowerBound()
4845
};
4946
}
5047

51-
@Test
5248
public void testNextSnapshotIndexUpperBound()
5349
{
5450
new SetMojo()
@@ -68,7 +64,6 @@ public void testNextSnapshotIndexUpperBound()
6864
};
6965
}
7066

71-
@Test
7267
public void testNextSnapshotIndexWithoutNextSnapshot() throws MojoFailureException
7368
{
7469
try
@@ -92,4 +87,9 @@ public void testNextSnapshotIndexWithoutNextSnapshot() throws MojoFailureExcepti
9287
}
9388
}
9489

90+
public void testVersionlessDependency() throws Exception
91+
{
92+
SetMojo myMojo = createMojo( "set", "src/test/resources/org/codehaus/mojo/set/versionless-01/pom.xml" );
93+
myMojo.execute();
94+
}
9595
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
<groupId>default-group</groupId>
5+
<artifactId>default-artifact</artifactId>
6+
<version>1.0</version>
7+
<packaging>pom</packaging>
8+
9+
<dependencyManagement>
10+
<dependencies>
11+
<dependency>
12+
<groupId>localhost</groupId>
13+
<artifactId>dummy-api</artifactId>
14+
<version>1.1</version>
15+
</dependency>
16+
</dependencies>
17+
</dependencyManagement>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>localhost</groupId>
22+
<artifactId>dummy-api</artifactId>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<artifactId>versions-maven-plugin</artifactId>
30+
<configuration>
31+
<updateBuildOutputTimestampPolicy>onchange</updateBuildOutputTimestampPolicy>
32+
<artifactId>dummy-api</artifactId>
33+
<newVersion>2.0</newVersion>
34+
</configuration>
35+
</plugin>
36+
</plugins>
37+
</build>
38+
</project>

0 commit comments

Comments
 (0)