You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2026/09/15 20:46:11 UTC

(maven-enforcer) branch master updated: Fix/916 require same versions for managed report plugins (#1024)

This is an automated email from the ASF dual-hosted git repository.

slawekjaranowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-enforcer.git


The following commit(s) were added to refs/heads/master by this push:
     new d255d1f8 Fix/916 require same versions for managed report plugins (#1024)
d255d1f8 is described below

commit d255d1f8e61196f6c6003ac00ad0c881b42f5881
Author: Dhruv_G_123 <9b...@gmail.com>
AuthorDate: Wed Sep 16 02:16:06 2026 +0530

    Fix/916 require same versions for managed report plugins (#1024)
    
    * Fix RequireSameVersions for managed report plugins
    
    * Handle build plugin versions for report plugins
    
    * Address review feedback for RequireSameVersions
---
 .../maven/enforcer/rules/RequireSameVersions.java  |  61 +++++++++-
 .../enforcer/rules/TestRequireSameVersions.java    | 132 ++++++++++++++++++++-
 .../pom.xml                                        |  89 ++++++++++++++
 3 files changed, 273 insertions(+), 9 deletions(-)

diff --git a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireSameVersions.java b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireSameVersions.java
index 1c006a66..8acaa204 100644
--- a/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireSameVersions.java
+++ b/enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/RequireSameVersions.java
@@ -29,11 +29,15 @@ import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
+import java.util.function.Function;
 import java.util.regex.Pattern;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
 import org.apache.maven.execution.MavenSession;
+import org.apache.maven.model.Plugin;
+import org.apache.maven.model.PluginManagement;
+import org.apache.maven.model.ReportPlugin;
 import org.apache.maven.project.MavenProject;
 
 /**
@@ -74,9 +78,20 @@ public final class RequireSameVersions extends AbstractStandardEnforcerRule {
         Set<String> allReportPlugins = new HashSet<>(reportPlugins);
         allReportPlugins.addAll(plugins);
         // CHECKSTYLE_OFF: LineLength
-        versionMembers.putAll(collectVersionMembers(project.getArtifacts(), dependencies, " (dependency)"));
-        versionMembers.putAll(collectVersionMembers(project.getPluginArtifacts(), allBuildPlugins, " (buildPlugin)"));
-        versionMembers.putAll(collectVersionMembers(project.getReportArtifacts(), allReportPlugins, " (reportPlugin)"));
+        versionMembers.putAll(collectVersionMembers(
+                project.getArtifacts(),
+                dependencies,
+                " (dependency)",
+                artifact -> uniqueVersions ? artifact.getVersion() : artifact.getBaseVersion()));
+
+        versionMembers.putAll(collectVersionMembers(
+                project.getPluginArtifacts(),
+                allBuildPlugins,
+                " (buildPlugin)",
+                artifact -> uniqueVersions ? artifact.getVersion() : artifact.getBaseVersion()));
+
+        versionMembers.putAll(collectVersionMembers(
+                project.getReportArtifacts(), allReportPlugins, " (reportPlugin)", this::getReportPluginVersion));
         // CHECKSTYLE_ON: LineLength
 
         if (versionMembers.size() > 1) {
@@ -109,7 +124,10 @@ public final class RequireSameVersions extends AbstractStandardEnforcerRule {
     }
 
     private Map<String, List<String>> collectVersionMembers(
-            Set<Artifact> artifacts, Collection<String> patterns, String source) {
+            Set<Artifact> artifacts,
+            Collection<String> patterns,
+            String source,
+            Function<Artifact, String> versionResolver) {
         Map<String, List<String>> versionMembers = new LinkedHashMap<>();
 
         List<Pattern> regExs = new ArrayList<>();
@@ -126,7 +144,7 @@ public final class RequireSameVersions extends AbstractStandardEnforcerRule {
         for (Artifact artifact : artifacts) {
             for (Pattern regEx : regExs) {
                 if (regEx.matcher(artifact.getDependencyConflictId()).matches()) {
-                    String version = uniqueVersions ? artifact.getVersion() : artifact.getBaseVersion();
+                    String version = versionResolver.apply(artifact);
                     versionMembers
                             .computeIfAbsent(version, unused -> new ArrayList<>())
                             .add(artifact.getDependencyConflictId() + source);
@@ -136,6 +154,39 @@ public final class RequireSameVersions extends AbstractStandardEnforcerRule {
         return versionMembers;
     }
 
+    private String getReportPluginVersion(Artifact artifact) {
+        for (ReportPlugin reportPlugin : project.getReportPlugins()) {
+            if (Objects.equals(artifact.getGroupId(), reportPlugin.getGroupId())
+                    && Objects.equals(artifact.getArtifactId(), reportPlugin.getArtifactId())
+                    && reportPlugin.getVersion() != null) {
+                return reportPlugin.getVersion();
+            }
+        }
+        if (project.getBuild() != null) {
+            for (Plugin plugin : project.getBuild().getPlugins()) {
+                if (Objects.equals(artifact.getGroupId(), plugin.getGroupId())
+                        && Objects.equals(artifact.getArtifactId(), plugin.getArtifactId())
+                        && plugin.getVersion() != null) {
+                    return plugin.getVersion();
+                }
+            }
+        }
+        PluginManagement pluginManagement =
+                project.getBuild() != null ? project.getBuild().getPluginManagement() : null;
+
+        if (pluginManagement != null) {
+            for (Plugin plugin : pluginManagement.getPlugins()) {
+                if (Objects.equals(artifact.getGroupId(), plugin.getGroupId())
+                        && Objects.equals(artifact.getArtifactId(), plugin.getArtifactId())
+                        && plugin.getVersion() != null) {
+                    return plugin.getVersion();
+                }
+            }
+        }
+
+        return uniqueVersions ? artifact.getVersion() : artifact.getBaseVersion();
+    }
+
     void addDependency(String dependency) {
         dependencies.add(dependency);
     }
diff --git a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/TestRequireSameVersions.java b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/TestRequireSameVersions.java
index e0a9b319..73056275 100644
--- a/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/TestRequireSameVersions.java
+++ b/enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/TestRequireSameVersions.java
@@ -19,12 +19,16 @@
 package org.apache.maven.enforcer.rules;
 
 import java.io.IOException;
+import java.util.Collections;
 import java.util.HashSet;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
 import org.apache.maven.enforcer.rules.utils.EnforcerArtifactStubFactory;
 import org.apache.maven.execution.MavenSession;
+import org.apache.maven.model.Build;
+import org.apache.maven.model.Plugin;
+import org.apache.maven.model.PluginManagement;
 import org.apache.maven.project.MavenProject;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -254,12 +258,124 @@ class TestRequireSameVersions {
         assertThatCode(rule::execute).isInstanceOf(EnforcerRuleException.class);
     }
 
-    private static Artifact constructArtifact(String artifactId, String version) throws IOException {
-        return ARTIFACT_FACTORY.createArtifact("org.acme", artifactId, version);
+    @Test
+    void testReportPluginUsesManagedVersionWhenArtifactVersionIsRelease() throws IOException {
+        String version = "3.5.3";
+
+        Artifact buildPlugin =
+                ARTIFACT_FACTORY.createArtifact("org.apache.maven.plugins", "maven-surefire-plugin", version);
+        Artifact reportPlugin =
+                ARTIFACT_FACTORY.createArtifact("org.apache.maven.plugins", "maven-surefire-report-plugin", "RELEASE");
+
+        rule.addBuildPlugin(extractGaString(buildPlugin));
+        rule.addReportPlugin(extractGaString(reportPlugin));
+
+        HashSet<Artifact> pluginArtifacts = new HashSet<>();
+        pluginArtifacts.add(buildPlugin);
+
+        HashSet<Artifact> reportArtifacts = new HashSet<>();
+        reportArtifacts.add(reportPlugin);
+
+        when(project.getArtifacts()).thenReturn(new HashSet<>());
+        when(project.getPluginArtifacts()).thenReturn(pluginArtifacts);
+        when(project.getReportArtifacts()).thenReturn(reportArtifacts);
+
+        Plugin managedReportPlugin = new Plugin();
+        managedReportPlugin.setGroupId("org.apache.maven.plugins");
+        managedReportPlugin.setArtifactId("maven-surefire-report-plugin");
+        managedReportPlugin.setVersion(version);
+
+        Build build = new Build();
+        PluginManagement pluginManagement = new PluginManagement();
+        pluginManagement.setPlugins(Collections.singletonList(managedReportPlugin));
+        build.setPluginManagement(pluginManagement);
+
+        when(project.getBuild()).thenReturn(build);
+
+        assertThatCode(rule::execute).doesNotThrowAnyException();
     }
 
-    private static String extractGaString(Artifact dependency) {
-        return String.format("%s:%s", dependency.getGroupId(), dependency.getArtifactId());
+    @Test
+    void testReportPluginUsesBuildPluginVersionBeforeManagedVersion() throws IOException {
+        String buildPluginVersion = "3.5.3";
+        String managedVersion = "3.6.0";
+
+        Artifact buildPlugin = ARTIFACT_FACTORY.createArtifact(
+                "org.apache.maven.plugins", "maven-surefire-plugin", buildPluginVersion);
+        Artifact reportPlugin =
+                ARTIFACT_FACTORY.createArtifact("org.apache.maven.plugins", "maven-surefire-report-plugin", "RELEASE");
+
+        rule.addBuildPlugin(extractGaString(buildPlugin));
+        rule.addReportPlugin(extractGaString(reportPlugin));
+
+        HashSet<Artifact> pluginArtifacts = new HashSet<>();
+        pluginArtifacts.add(buildPlugin);
+
+        HashSet<Artifact> reportArtifacts = new HashSet<>();
+        reportArtifacts.add(reportPlugin);
+
+        when(project.getArtifacts()).thenReturn(new HashSet<>());
+        when(project.getPluginArtifacts()).thenReturn(pluginArtifacts);
+        when(project.getReportArtifacts()).thenReturn(reportArtifacts);
+
+        Plugin buildReportPlugin = new Plugin();
+        buildReportPlugin.setGroupId("org.apache.maven.plugins");
+        buildReportPlugin.setArtifactId("maven-surefire-report-plugin");
+        buildReportPlugin.setVersion(buildPluginVersion);
+
+        Plugin managedReportPlugin = new Plugin();
+        managedReportPlugin.setGroupId("org.apache.maven.plugins");
+        managedReportPlugin.setArtifactId("maven-surefire-report-plugin");
+        managedReportPlugin.setVersion(managedVersion);
+
+        Build build = new Build();
+        build.setPlugins(Collections.singletonList(buildReportPlugin));
+
+        PluginManagement pluginManagement = new PluginManagement();
+        pluginManagement.setPlugins(Collections.singletonList(managedReportPlugin));
+        build.setPluginManagement(pluginManagement);
+
+        when(project.getBuild()).thenReturn(build);
+
+        assertThatCode(rule::execute).doesNotThrowAnyException();
+    }
+
+    @Test
+    void testReportPluginFailsWhenManagedVersionDiffersFromBuildPlugin() throws IOException {
+        String buildPluginVersion = "3.5.3";
+        String managedVersion = "3.6.0";
+
+        Artifact buildPlugin = ARTIFACT_FACTORY.createArtifact(
+                "org.apache.maven.plugins", "maven-surefire-plugin", buildPluginVersion);
+        Artifact reportPlugin =
+                ARTIFACT_FACTORY.createArtifact("org.apache.maven.plugins", "maven-surefire-report-plugin", "RELEASE");
+
+        rule.addBuildPlugin(extractGaString(buildPlugin));
+        rule.addReportPlugin(extractGaString(reportPlugin));
+
+        HashSet<Artifact> pluginArtifacts = new HashSet<>();
+        pluginArtifacts.add(buildPlugin);
+
+        HashSet<Artifact> reportArtifacts = new HashSet<>();
+        reportArtifacts.add(reportPlugin);
+
+        when(project.getArtifacts()).thenReturn(new HashSet<>());
+        when(project.getPluginArtifacts()).thenReturn(pluginArtifacts);
+        when(project.getReportArtifacts()).thenReturn(reportArtifacts);
+
+        Plugin managedReportPlugin = new Plugin();
+        managedReportPlugin.setGroupId("org.apache.maven.plugins");
+        managedReportPlugin.setArtifactId("maven-surefire-report-plugin");
+        managedReportPlugin.setVersion(managedVersion);
+
+        Build build = new Build();
+        PluginManagement pluginManagement = new PluginManagement();
+        pluginManagement.setPlugins(Collections.singletonList(managedReportPlugin));
+        build.setPluginManagement(pluginManagement);
+
+        when(project.getBuild()).thenReturn(build);
+
+        assertThatThrownBy(rule::execute).isInstanceOf(EnforcerRuleException.class);
     }
 
     @Test
@@ -285,4 +401,12 @@ class TestRequireSameVersions {
                 .isInstanceOf(EnforcerRuleException.class)
                 .hasMessageStartingWith(customMessage);
     }
+
+    private static Artifact constructArtifact(String artifactId, String version) throws IOException {
+        return ARTIFACT_FACTORY.createArtifact("org.acme", artifactId, version);
+    }
+
+    private static String extractGaString(Artifact dependency) {
+        return String.format("%s:%s", dependency.getGroupId(), dependency.getArtifactId());
+    }
 }
diff --git a/maven-enforcer-plugin/src/it/projects/require-same-versions_managed-report-plugin/pom.xml b/maven-enforcer-plugin/src/it/projects/require-same-versions_managed-report-plugin/pom.xml
new file mode 100644
index 00000000..d8c43fb7
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-same-versions_managed-report-plugin/pom.xml
@@ -0,0 +1,89 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you 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
+
+http://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.
+-->
+<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>org.apache.maven.enforcer.its</groupId>
+  <artifactId>requireSameVersions-managed-report-plugin</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <build>
+    <pluginManagement>
+      <plugins>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-surefire-plugin</artifactId>
+          <version>3.5.3</version>
+        </plugin>
+
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-surefire-report-plugin</artifactId>
+          <version>3.5.3</version>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>enforce</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <requireSameVersions>
+                  <plugins>
+                    <plugin>org.apache.maven.plugins:maven-surefire-plugin</plugin>
+                    <plugin>org.apache.maven.plugins:maven-surefire-report-plugin</plugin>
+                  </plugins>
+                </requireSameVersions>
+              </rules>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+      </plugin>
+    </plugins>
+  </build>
+
+  <reporting>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-report-plugin</artifactId>
+      </plugin>
+    </plugins>
+  </reporting>
+
+</project>