From 6234b2dcc8f21b1308d703394951e1bd36335625 Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Sun, 30 Jul 2023 09:07:45 +0200 Subject: [PATCH] Resolves #995: Minimum version required for Versions Maven Plugin should is not the minimum version for the project --- .../invoker.properties | 1 + .../it-display-plugin-updates-005/verify.bsh | 31 ------------------- .../verify.groovy | 2 ++ .../verify.groovy | 4 +-- .../versions/DisplayPluginUpdatesMojo.java | 21 +++++-------- .../MinimalMavenBuildVersionFinder.java | 5 +-- 6 files changed, 14 insertions(+), 50 deletions(-) delete mode 100644 versions-maven-plugin/src/it/it-display-plugin-updates-005/verify.bsh create mode 100644 versions-maven-plugin/src/it/it-display-plugin-updates-005/verify.groovy diff --git a/versions-maven-plugin/src/it/it-display-plugin-updates-005/invoker.properties b/versions-maven-plugin/src/it/it-display-plugin-updates-005/invoker.properties index 3e9221daf4..70a8abe31e 100644 --- a/versions-maven-plugin/src/it/it-display-plugin-updates-005/invoker.properties +++ b/versions-maven-plugin/src/it/it-display-plugin-updates-005/invoker.properties @@ -1 +1,2 @@ invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:display-plugin-updates +invoker.mavenOpts = -Dversions.outputFile=./output.txt -DoutputEncoding=UTF-8 diff --git a/versions-maven-plugin/src/it/it-display-plugin-updates-005/verify.bsh b/versions-maven-plugin/src/it/it-display-plugin-updates-005/verify.bsh deleted file mode 100644 index 8e1e8966e6..0000000000 --- a/versions-maven-plugin/src/it/it-display-plugin-updates-005/verify.bsh +++ /dev/null @@ -1,31 +0,0 @@ -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 p = Pattern.compile( "\\QThe following plugin updates are available\\E" ); - Matcher m = p.matcher( buf.toString() ); - if ( !m.find() ) - { - System.out.println( "cannot find The following plugin updates are available" ); - return false; - } - - p = Pattern.compile( "maven-deploy-plugin \\.+ 2\\.3 ->" ); - m = p.matcher( buf.toString() ); - if ( !m.find() ) - { - System.out.println( "cannot find update of deploy plugin"); - return false; - } -} -catch( Throwable t ) -{ - t.printStackTrace(); - return false; -} - -return true; diff --git a/versions-maven-plugin/src/it/it-display-plugin-updates-005/verify.groovy b/versions-maven-plugin/src/it/it-display-plugin-updates-005/verify.groovy new file mode 100644 index 0000000000..1984fa9827 --- /dev/null +++ b/versions-maven-plugin/src/it/it-display-plugin-updates-005/verify.groovy @@ -0,0 +1,2 @@ +output = new File(basedir, "output.txt").text +assert output =~ /\Qmaven-deploy-plugin\E\s*\.*\s*\Q2.3 ->\E/ diff --git a/versions-maven-plugin/src/it/it-display-plugin-updates-issue-533/verify.groovy b/versions-maven-plugin/src/it/it-display-plugin-updates-issue-533/verify.groovy index 88307c7512..6f7a83ce5c 100644 --- a/versions-maven-plugin/src/it/it-display-plugin-updates-issue-533/verify.groovy +++ b/versions-maven-plugin/src/it/it-display-plugin-updates-issue-533/verify.groovy @@ -17,5 +17,5 @@ assert new File(basedir, 'output.txt').text .replaceAll('\r', '') - .contains('The following plugin updates are available:\n' - + ' maven-jar-plugin ................................... 3.0.0 -> 3.3.0') + .contains('Require Maven 3.2.5 to use the following plugin updates:\n' + + ' maven-jar-plugin ................................... 3.0.0 -> 3.3.0') diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java index d263633cce..a7441b979e 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java @@ -124,8 +124,6 @@ public class DisplayPluginUpdatesMojo extends AbstractVersionsDisplayMojo { */ private static final String FROM_SUPER_POM = "(from super-pom) "; - public static final String DEFAULT_MVN_VERSION = "3.2.5"; - /** * @since 1.0-alpha-1 */ @@ -345,8 +343,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { List pluginUpdates = new ArrayList<>(); List pluginLockdowns = new ArrayList<>(); ArtifactVersion curMavenVersion = DefaultArtifactVersionCache.of(runtimeInformation.getMavenVersion()); - ArtifactVersion specMavenVersion = - MinimalMavenBuildVersionFinder.find(getProject(), DEFAULT_MVN_VERSION, getLog()); + ArtifactVersion specMavenVersion = MinimalMavenBuildVersionFinder.find(getProject(), getLog()); ArtifactVersion minMavenVersion = null; boolean superPomDrivingMinVersion = false; // if Maven prerequisite upgraded to a version, Map @@ -472,10 +469,8 @@ public void execute() throws MojoExecutionException, MojoFailureException { logLine(false, ""); // information on minimum Maven version - boolean noMavenMinVersion = MinimalMavenBuildVersionFinder.find(getProject(), null, getLog()) == null; - if (noMavenMinVersion) { - getLog().warn("Project does not define minimum Maven version required for build, default is: " - + DEFAULT_MVN_VERSION); + if (specMavenVersion == null) { + getLog().warn("Project does not define minimum Maven version required for build"); } else { logLine(false, "Project requires minimum Maven version for build of: " + specMavenVersion); } @@ -488,7 +483,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { logLine(false, ""); if (isMavenPluginProject()) { - if (noMavenMinVersion) { + if (specMavenVersion == null) { getLog().warn("Project (which is a Maven plugin) does not define required minimum version of Maven."); getLog().warn("Update the pom.xml to contain"); getLog().warn(" "); @@ -507,7 +502,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { logLine(false, "No plugins require a newer version of Maven than specified by the pom."); } } else { - if (noMavenMinVersion) { + if (specMavenVersion == null) { logLine(true, "Project does not define required minimum version of Maven."); logLine(true, "Update the pom.xml to contain maven-enforcer-plugin to"); logLine(true, "force the Maven version which is needed to build this project."); @@ -528,7 +523,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { for (Map.Entry> mavenUpgrade : mavenUpgrades.entrySet()) { ArtifactVersion mavenUpgradeVersion = mavenUpgrade.getKey(); Map upgradePlugins = mavenUpgrade.getValue(); - if (upgradePlugins.isEmpty() || compare(specMavenVersion, mavenUpgradeVersion) >= 0) { + if (upgradePlugins.isEmpty() || compare(mavenUpgradeVersion, specMavenVersion) < 0) { continue; } logLine(false, ""); @@ -816,7 +811,7 @@ private ArtifactVersion getPrerequisitesMavenVersion(MavenProject pluginProject) return ofNullable(pluginProject.getPrerequisites()) .map(Prerequisites::getMaven) .map(DefaultArtifactVersionCache::of) - .orElse(DefaultArtifactVersionCache.of(DEFAULT_MVN_VERSION)); + .orElse(null); } /** @@ -1159,7 +1154,7 @@ protected void update(ModifiedPomXMLEventReader pom) { } private static int compare(ArtifactVersion a, ArtifactVersion b) { - return a.compareTo(b); + return a == null ? b == null ? 0 : -1 : b == null ? 1 : a.compareTo(b); } private static class IgnoringModelProblemCollector implements ModelProblemCollector { diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinder.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinder.java index 8e7e841afe..f7890c9fdf 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinder.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/MinimalMavenBuildVersionFinder.java @@ -39,14 +39,11 @@ static Optional getGreatestVersion(ArtifactVersion... v) { }); } - static ArtifactVersion find(MavenProject mavenProject, String defaultMavenVersion, Log log) { + static ArtifactVersion find(MavenProject mavenProject, Log log) { return getGreatestVersion( getEnforcerMavenVersion(mavenProject, log), ofNullable(mavenProject.getPrerequisites()) .map(Prerequisites::getMaven) - .map(DefaultArtifactVersionCache::of) - .orElse(null), - ofNullable(defaultMavenVersion) .map(DefaultArtifactVersionCache::of) .orElse(null)) .orElse(null);