Skip to content

Commit

Permalink
Fix detection of plugin updates requiring newer Maven for building th…
Browse files Browse the repository at this point in the history
…an for running (#527)

Separate minimal Maven version detection for projects from requirements of plugins
This closes #526
  • Loading branch information
kwin authored Jan 13, 2022
1 parent 279d90a commit dce93be
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 629 deletions.
21 changes: 21 additions & 0 deletions src/it-repo/dummy-maven-plugin-3.1.pom
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>enforce-maven</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireMavenVersion>
<version>3.8.4</version>
</requireMavenVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:display-plugin-updates
45 changes: 45 additions & 0 deletions src/it/it-display-plugin-updates-010-issue-526/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>localhost</groupId>
<artifactId>it-101</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>display-plugin-updates</name>
<description>IT for https://github.com/mojohaus/versions-maven-plugin/issues/526</description>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>localhost</groupId>
<artifactId>dummy-maven-plugin</artifactId>
<version>1.0</version>
</plugin><!-- defines older compatible Maven runtime compatibility than build compatibility -->
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>enforce-maven</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireMavenVersion>
<version>3.3.9</version>
</requireMavenVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
33 changes: 33 additions & 0 deletions src/it/it-display-plugin-updates-010-issue-526/verify.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.io.*;
import org.codehaus.plexus.util.FileUtils;
import java.util.regex.*;

try
{
File file = new File( basedir, "build.log" );
String buf = FileUtils.fileRead( file );

Pattern p1 = Pattern.compile( "\\QNo plugins require a newer version of Maven than specified by the pom.\\E" );
Matcher m1 = p1.matcher( buf.toString() );
Pattern p2 = Pattern.compile( "\\Qlocalhost:dummy-maven-plugin\\E\\s*\\.*\\s*\\Q1.0 -> 3.1\\E" );
Matcher m2 = p2.matcher( buf.toString() );
if ( !m1.find() )
{
System.out.println( "Did not correctly detect minimum Maven build version 3.0 of this project" );
return false;
}
if ( !m2.find() )
{
System.out.println( "Did not suggest updating dummy-maven-plugin to version 3.1" );
return false;
}
System.out.println( m1.group( 0 ) );
System.out.println( m2.group( 0 ) );
}
catch( Throwable t )
{
t.printStackTrace();
return false;
}

return true;
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.maven.lifecycle.mapping.LifecycleMapping;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.Prerequisites;
import org.apache.maven.model.Profile;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
Expand Down Expand Up @@ -382,7 +383,7 @@ public void execute()
List<String> pluginUpdates = new ArrayList<>();
List<String> pluginLockdowns = new ArrayList<>();
ArtifactVersion curMavenVersion = runtimeInformation.getApplicationVersion();
ArtifactVersion specMavenVersion = new DefaultArtifactVersion( getRequiredMavenVersion( getProject(), "2.0" ) );
ArtifactVersion specMavenVersion = MinimalMavenBuildVersionFinder.find( getProject(), "2.0", getLog() );
ArtifactVersion minMavenVersion = null;
boolean superPomDrivingMinVersion = false;
// if Maven prerequisite upgraded to a version, Map<plugin compact key, latest compatible plugin vesion>
Expand Down Expand Up @@ -433,8 +434,7 @@ public void execute()
getHelper().resolveArtifact( probe, true );
MavenProject pluginMavenProject =
projectBuilder.buildFromRepository( probe, remotePluginRepositories, localRepository );
ArtifactVersion pluginRequires =
new DefaultArtifactVersion( getRequiredMavenVersion( pluginMavenProject, "2.0" ) );
ArtifactVersion pluginRequires = getPrerequisitesMavenVersion( pluginMavenProject );
if ( artifactVersion == null && compare( specMavenVersion, pluginRequires ) >= 0 )
{
// ok, newer version compatible with current specMavenVersion
Expand Down Expand Up @@ -492,8 +492,7 @@ public void execute()
getHelper().resolveArtifact( probe, true );
MavenProject mavenProject =
projectBuilder.buildFromRepository( probe, remotePluginRepositories, localRepository );
ArtifactVersion requires =
new DefaultArtifactVersion( getRequiredMavenVersion( mavenProject, "2.0" ) );
ArtifactVersion requires = getPrerequisitesMavenVersion( mavenProject );
if ( minMavenVersion == null || compare( minMavenVersion, requires ) < 0 )
{
minMavenVersion = requires;
Expand Down Expand Up @@ -595,30 +594,14 @@ && new DefaultArtifactVersion( effectiveVersion ).compareTo( new DefaultArtifact
logLine( false, "" );

// information on minimum Maven version
boolean noMavenMinVersion = getRequiredMavenVersion( getProject(), null ) == null;
boolean noExplicitMavenMinVersion =
getProject().getPrerequisites() == null || getProject().getPrerequisites().getMaven() == null;
boolean noMavenMinVersion = MinimalMavenBuildVersionFinder.find( getProject(), null, getLog() ) == null;
if ( noMavenMinVersion )
{
getLog().warn( "Project does not define minimum Maven version, default is: 2.0" );
}
else if ( noExplicitMavenMinVersion )
{
logLine( false, "Project inherits minimum Maven version as: " + specMavenVersion );
getLog().warn( "Project does not define minimum Maven version required for build, default is: 2.0" );
}
else
{
ArtifactVersion explicitMavenVersion =
new DefaultArtifactVersion( getProject().getPrerequisites().getMaven() );
if ( compare( explicitMavenVersion, specMavenVersion ) < 0 )
{
logLine( true, "Project's effective minimum Maven (from parent) is: " + specMavenVersion );
logLine( true, "Project defines minimum Maven version as: " + explicitMavenVersion );
}
else
{
logLine( false, "Project defines minimum Maven version as: " + specMavenVersion );
}
logLine( false, "Project requires minimum Maven version for build of: " + specMavenVersion );
}
logLine( false, "Plugins require minimum Maven version of: " + minMavenVersion );
if ( superPomDrivingMinVersion )
Expand Down Expand Up @@ -822,13 +805,6 @@ private String compactKey( String groupId, String artifactId )
return groupId + ":" + artifactId;
}

private String getRequiredMavenVersion( MavenProject mavenProject, String defaultValue )
{
ArtifactVersion requiredMavenVersion = new RequiredMavenVersionFinder( mavenProject ).find();

return requiredMavenVersion == null ? defaultValue : requiredMavenVersion.toString();
}

private static final class StackState
{
private final String path;
Expand Down Expand Up @@ -936,6 +912,26 @@ else if ( event.isEndElement() )

// -------------------------- OTHER METHODS --------------------------

/**
* Get the minimum required Maven version of the given plugin
* Same logic as in https://github.com/apache/maven-plugin-tools/blob/c8ddcdcb10d342a5a5e2f38245bb569af5730c7c/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/PluginReport.java#L711
* @param pluginProject the plugin for which to retrieve the minimum Maven version which is required
* @return The minimally required Maven version (never {@code null})
*/
private ArtifactVersion getPrerequisitesMavenVersion( MavenProject pluginProject ) {
Prerequisites prerequisites = pluginProject.getPrerequisites();
if (null == prerequisites) {
return new DefaultArtifactVersion("2.0");
}

String prerequisitesMavenValue = prerequisites.getMaven();
if (null == prerequisitesMavenValue) {
return new DefaultArtifactVersion("2.0");
}

return new DefaultArtifactVersion(prerequisitesMavenValue);
}

/**
* Gets the build plugins of a specific project.
*
Expand Down
Loading

0 comments on commit dce93be

Please sign in to comment.