Skip to content

Commit 29b368d

Browse files
committed
Fixes #422 Avoid NPE when non-local parent POM is present
1 parent a6432f9 commit 29b368d

4 files changed

Lines changed: 93 additions & 2 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:display-plugin-updates
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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>org.apache</groupId>
7+
<artifactId>apache</artifactId>
8+
<version>23</version>
9+
<relativePath/>
10+
</parent>
11+
12+
<groupId>localhost</groupId>
13+
<artifactId>it-101</artifactId>
14+
<version>1.0</version>
15+
<packaging>pom</packaging>
16+
<name>display-plugin-updates</name>
17+
18+
<description>Make sure display-plugin-updates works if a non-local parent is referenced.</description>
19+
20+
<prerequisites>
21+
<maven>2.0.6</maven>
22+
</prerequisites>
23+
24+
<build>
25+
<pluginManagement>
26+
<plugins>
27+
<plugin>
28+
<groupId>localhost</groupId>
29+
<artifactId>dummy-maven-plugin</artifactId>
30+
<version>1.0</version>
31+
</plugin>
32+
<plugin>
33+
<artifactId>maven-clean-plugin</artifactId>
34+
<version>2.2</version>
35+
</plugin>
36+
<plugin>
37+
<artifactId>maven-deploy-plugin</artifactId>
38+
<version>2.3</version>
39+
</plugin>
40+
<plugin>
41+
<artifactId>maven-install-plugin</artifactId>
42+
<version>2.2</version>
43+
</plugin>
44+
<plugin>
45+
<artifactId>maven-site-plugin</artifactId>
46+
<version>2.0</version>
47+
</plugin>
48+
</plugins>
49+
</pluginManagement>
50+
</build>
51+
52+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.io.*;
2+
import org.codehaus.plexus.util.FileUtils;
3+
import java.util.regex.*;
4+
5+
try
6+
{
7+
File file = new File( basedir, "build.log" );
8+
String buf = FileUtils.fileRead( file );
9+
10+
Pattern p = Pattern.compile( "\\Qlocalhost:dummy-maven-plugin\\E\\s*\\.*\\s*1\\.0\\s+->\\s+3\\.1" );
11+
Matcher m = p.matcher( buf.toString() );
12+
if ( !m.find() )
13+
{
14+
System.out.println( "Did not suggest updating dummy-maven-plugin to version 3.1" );
15+
return false;
16+
}
17+
System.out.println( m.group( 0 ) );
18+
}
19+
catch( Throwable t )
20+
{
21+
t.printStackTrace();
22+
return false;
23+
}
24+
25+
return true;

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070

7171
import javax.xml.stream.XMLStreamException;
7272
import javax.xml.stream.events.XMLEvent;
73+
74+
import java.io.File;
7375
import java.io.IOException;
7476
import java.io.Reader;
7577
import java.io.StringWriter;
@@ -769,7 +771,7 @@ private Map<String, String> getParentsPlugins( List<MavenProject> parents )
769771
try
770772
{
771773
Set<String> withVersionSpecified =
772-
findPluginsWithVersionsSpecified( new StringBuilder( writer.toString() ), parentProject.getFile().getAbsolutePath() );
774+
findPluginsWithVersionsSpecified( new StringBuilder( writer.toString() ), getSafeProjectPathInfo(parentProject) );
773775

774776
Map<String, String> map = getPluginManagement( interpolatedModel );
775777
map.keySet().retainAll( withVersionSpecified );
@@ -797,6 +799,17 @@ private Map<String, String> getParentsPlugins( List<MavenProject> parents )
797799
}
798800
return parentPlugins;
799801
}
802+
803+
private String getSafeProjectPathInfo(MavenProject project) {
804+
File file = project.getFile();
805+
if (file != null) {
806+
return file.getAbsolutePath();
807+
}
808+
else {
809+
// path is used only as information in error message, we can fallback to project artifact info here
810+
return project.toString();
811+
}
812+
}
800813

801814
private boolean isMavenPluginProject()
802815
{
@@ -850,7 +863,7 @@ public String toString()
850863
private Set<String> findPluginsWithVersionsSpecified( MavenProject project )
851864
throws IOException, XMLStreamException
852865
{
853-
return findPluginsWithVersionsSpecified( PomHelper.readXmlFile( project.getFile() ), project.getFile().getAbsolutePath() );
866+
return findPluginsWithVersionsSpecified( PomHelper.readXmlFile( project.getFile() ), getSafeProjectPathInfo(project) );
854867
}
855868

856869
/**

0 commit comments

Comments
 (0)