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 de90d72fe1..2b4ff5f53d 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 @@ -6,11 +6,14 @@ import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.model.Plugin; import org.apache.maven.model.PluginExecution; +import org.apache.maven.model.Prerequisites; import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.codehaus.plexus.util.xml.Xpp3Dom; +import static java.util.Optional.ofNullable; + /** * Finds the minimal Maven version required to build a Maven project. * Evaluates the {@code maven-enforcer-plugin:enforce} goal and @@ -25,11 +28,13 @@ private MinimalMavenBuildVersionFinder() { } static ArtifactVersion find(MavenProject mavenProject, String defaultVersion, Log log) { - ArtifactVersion version = getEnforcerMavenVersion(mavenProject, log); - if (version == null && defaultVersion != null) { - version = DefaultArtifactVersionCache.of(defaultVersion); - } - return version; + return ofNullable(getEnforcerMavenVersion(mavenProject, log)) + .orElse(ofNullable(mavenProject.getPrerequisites()) + .map(Prerequisites::getMaven) + .map(__ -> (ArtifactVersion) DefaultArtifactVersionCache.of(__)) + .orElse(ofNullable(defaultVersion) + .map(___ -> (ArtifactVersion) DefaultArtifactVersionCache.of(___)) + .orElse(null))); } private static ArtifactVersion getEnforcerMavenVersion(MavenProject mavenProject, Log log) { diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojoTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojoTest.java new file mode 100644 index 0000000000..a4d7a5a01d --- /dev/null +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojoTest.java @@ -0,0 +1,93 @@ +package org.codehaus.mojo.versions; + +/* + * Copyright MojoHaus and Contributors + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.model.Prerequisites; +import org.apache.maven.plugin.testing.AbstractMojoTestCase; +import org.apache.maven.plugin.testing.MojoRule; +import org.apache.maven.project.*; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import static org.apache.commons.codec.CharEncoding.UTF_8; +import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Contains unit tests for {@link DisplayPluginUpdatesMojo}. + * + * @author Andrzej Jarmoniuk + */ +public class DisplayPluginUpdatesMojoTest extends AbstractMojoTestCase { + + @Rule + public MojoRule mojoRule = new MojoRule(this); + + private Path outputPath; + + @Before + public void setUp() throws Exception { + super.setUp(); + outputPath = Files.createTempFile("display-plugin-updates", ""); + } + + @Test + public void testMavenVersionInPrerequisites() throws Exception { + Prerequisites prerequisites = new Prerequisites(); + prerequisites.setMaven("3.6.3"); + MavenProject mavenProject = mock(MavenProject.class); + when(mavenProject.getPrerequisites()).thenReturn(prerequisites); + ProjectBuildingResult projectBuildingResult = mock(ProjectBuildingResult.class); + when(projectBuildingResult.getProject()).thenReturn(mavenProject); + ProjectBuilder projectBuilder = mock(ProjectBuilder.class); + try { + when(projectBuilder.build(any(Artifact.class), anyBoolean(), any(ProjectBuildingRequest.class))) + .thenReturn(projectBuildingResult); + } catch (ProjectBuildingException e) { + throw new RuntimeException(e); + } + + DisplayPluginUpdatesMojo mojo = (DisplayPluginUpdatesMojo) mojoRule.lookupConfiguredMojo( + new File("src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990"), + "display-plugin-updates"); + mojo.outputEncoding = UTF_8; + mojo.outputFile = outputPath.toFile(); + mojo.setPluginContext(new HashMap<>()); + mojo.aetherRepositorySystem = mockAetherRepositorySystem(new HashMap() { + { + put("default-plugin", new String[] {"1.0.0"}); + } + }); + setVariableValueToObject(mojo, "projectBuilder", projectBuilder); + mojo.execute(); + + List output = Files.readAllLines(outputPath); + assertThat(output, not(hasItem(containsString("Project does not define required minimum version of Maven.")))); + } +} diff --git a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/pom.xml b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/pom.xml new file mode 100644 index 0000000000..daf7937756 --- /dev/null +++ b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-plugin-updates/issue-990/pom.xml @@ -0,0 +1,39 @@ + + + + + 4.0.0 + + default-group + default-artifact + 1.0.0 + + + 3.8.0 + + + + + + default-group + default-plugin + 1.0.0 + + + +