diff --git a/enforcer-api/pom.xml b/enforcer-api/pom.xml
index 1893e0d7..f574b796 100644
--- a/enforcer-api/pom.xml
+++ b/enforcer-api/pom.xml
@@ -24,7 +24,7 @@
org.apache.maven.enforcer
enforcer
- 3.5.1-SNAPSHOT
+ 3.6.0-SNAPSHOT
enforcer-api
diff --git a/enforcer-rules/pom.xml b/enforcer-rules/pom.xml
index 1655a11b..fb54579b 100644
--- a/enforcer-rules/pom.xml
+++ b/enforcer-rules/pom.xml
@@ -24,7 +24,7 @@
org.apache.maven.enforcer
enforcer
- 3.5.1-SNAPSHOT
+ 3.6.0-SNAPSHOT
enforcer-rules
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 4e77e3e4..4f957665 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
@@ -33,6 +33,7 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
/**
@@ -51,11 +52,16 @@ public final class RequireSameVersions extends AbstractStandardEnforcerRule {
private Set reportPlugins = new HashSet<>();
+ private boolean sameModuleVersions;
+
private final MavenProject project;
+ private final MavenSession session;
+
@Inject
- public RequireSameVersions(MavenProject project) {
+ public RequireSameVersions(MavenProject project, MavenSession session) {
this.project = Objects.requireNonNull(project);
+ this.session = Objects.requireNonNull(session);
}
@Override
@@ -85,6 +91,14 @@ public void execute() throws EnforcerRuleException {
}
throw new EnforcerRuleException(builder.toString());
}
+
+ if (sameModuleVersions) {
+ MavenProject topLevelProject = session.getTopLevelProject();
+ if (!Objects.equals(topLevelProject.getVersion(), project.getVersion())) {
+ throw new EnforcerRuleException("Top level project has version " + topLevelProject.getVersion()
+ + " but current module has different version " + project.getVersion());
+ }
+ }
}
private Map> collectVersionMembers(
@@ -119,7 +133,7 @@ private Map> collectVersionMembers(
@Override
public String toString() {
return String.format(
- "RequireSameVersions[dependencies=%s, buildPlugins=%s, reportPlugins=%s, plugins=%s, uniqueVersions=%b]",
- dependencies, buildPlugins, reportPlugins, plugins, uniqueVersions);
+ "RequireSameVersions[dependencies=%s, buildPlugins=%s, reportPlugins=%s, plugins=%s, uniqueVersions=%b, sameModuleVersions=%b]",
+ dependencies, buildPlugins, reportPlugins, plugins, uniqueVersions, sameModuleVersions);
}
}
diff --git a/enforcer-rules/src/site/apt/requireSameVersions.apt.vm b/enforcer-rules/src/site/apt/requireSameVersions.apt.vm
index e4e55a7a..00a80627 100644
--- a/enforcer-rules/src/site/apt/requireSameVersions.apt.vm
+++ b/enforcer-rules/src/site/apt/requireSameVersions.apt.vm
@@ -37,8 +37,10 @@
* <> - an optional list of report plugin patterns
* <> - an optional list of both build and report plugin patterns
-
+
* <> - if SNAPSHOTs should be compared by their timestamped version or not. Default: false
+
+ * <> - if set to true enforces that the current module has the same version as the top level aggregator project. Default: false. Supported since version 3.6.0
[]
diff --git a/maven-enforcer-extension/pom.xml b/maven-enforcer-extension/pom.xml
index 99786b24..be5d1cc7 100644
--- a/maven-enforcer-extension/pom.xml
+++ b/maven-enforcer-extension/pom.xml
@@ -23,7 +23,7 @@
org.apache.maven.enforcer
enforcer
- 3.5.1-SNAPSHOT
+ 3.6.0-SNAPSHOT
org.apache.maven.extensions
maven-enforcer-extension
diff --git a/maven-enforcer-plugin/pom.xml b/maven-enforcer-plugin/pom.xml
index f90a96ad..cacc3c6e 100644
--- a/maven-enforcer-plugin/pom.xml
+++ b/maven-enforcer-plugin/pom.xml
@@ -24,7 +24,7 @@
org.apache.maven.enforcer
enforcer
- 3.5.1-SNAPSHOT
+ 3.6.0-SNAPSHOT
org.apache.maven.plugins
diff --git a/maven-enforcer-plugin/src/it/projects/require-same-versions_multi-module-build/invoker.properties b/maven-enforcer-plugin/src/it/projects/require-same-versions_multi-module-build/invoker.properties
new file mode 100644
index 00000000..58b6526e
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-same-versions_multi-module-build/invoker.properties
@@ -0,0 +1,18 @@
+# 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.
+
+invoker.buildResult = failure
diff --git a/maven-enforcer-plugin/src/it/projects/require-same-versions_multi-module-build/module1/pom.xml b/maven-enforcer-plugin/src/it/projects/require-same-versions_multi-module-build/module1/pom.xml
new file mode 100644
index 00000000..e1a8ea94
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-same-versions_multi-module-build/module1/pom.xml
@@ -0,0 +1,32 @@
+
+
+
+
+
+ 4.0.0
+
+
+ org.apache.maven.its.enforcer
+ require-same-versions_multi-module-build
+ 1.0-SNAPSHOT
+
+ module1
+
+
diff --git a/maven-enforcer-plugin/src/it/projects/require-same-versions_multi-module-build/module2/pom.xml b/maven-enforcer-plugin/src/it/projects/require-same-versions_multi-module-build/module2/pom.xml
new file mode 100644
index 00000000..3170c82d
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-same-versions_multi-module-build/module2/pom.xml
@@ -0,0 +1,33 @@
+
+
+
+
+
+ 4.0.0
+
+
+ org.apache.maven.its.enforcer
+ require-same-versions_multi-module-build
+ 1.0-SNAPSHOT
+
+ module2
+ 1.1-SNAPSHOT
+
+
diff --git a/maven-enforcer-plugin/src/it/projects/require-same-versions_multi-module-build/pom.xml b/maven-enforcer-plugin/src/it/projects/require-same-versions_multi-module-build/pom.xml
new file mode 100644
index 00000000..a36c063c
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-same-versions_multi-module-build/pom.xml
@@ -0,0 +1,70 @@
+
+
+
+
+
+ 4.0.0
+
+ org.apache.maven.its.enforcer
+ require-same-versions_multi-module-build
+ 1.0-SNAPSHOT
+ pom
+
+
+
+
+ first
+ This is the name
+ file:///Test
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-enforcer-plugin
+ @project.version@
+
+
+ test
+
+ enforce
+
+
+
+
+ true
+
+
+
+
+
+
+
+
+
+
+ module1
+ module2
+
+
diff --git a/maven-enforcer-plugin/src/it/projects/require-same-versions_multi-module-build/verify.groovy b/maven-enforcer-plugin/src/it/projects/require-same-versions_multi-module-build/verify.groovy
new file mode 100644
index 00000000..12752bf0
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/require-same-versions_multi-module-build/verify.groovy
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+File buildLog = new File( basedir, 'build.log' )
+assert buildLog.text.contains( '[INFO] BUILD FAILURE' )
+assert buildLog.text.contains( '[ERROR] Rule 0: org.apache.maven.enforcer.rules.RequireSameVersions failed with message:' )
+assert buildLog.text.contains( 'Top level project has version 1.0-SNAPSHOT but current module has different version 1.1-SNAPSHOT' )
diff --git a/pom.xml b/pom.xml
index ba051d02..8ef43f6a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
org.apache.maven.enforcer
enforcer
- 3.5.1-SNAPSHOT
+ 3.6.0-SNAPSHOT
pom
Apache Maven Enforcer
@@ -81,7 +81,7 @@
enforcer-archives/enforcer-LATEST
8
4.11.0
- 2024-05-26T17:46:08Z
+ 2024-07-30T15:32:39Z
1.4.1