Skip to content

Commit

Permalink
[MENFORCER-508] Add option to enforce same versions among Maven modules
Browse files Browse the repository at this point in the history
  • Loading branch information
kwin committed Jul 30, 2024
1 parent abca51d commit cf3fb67
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 10 deletions.
2 changes: 1 addition & 1 deletion enforcer-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>org.apache.maven.enforcer</groupId>
<artifactId>enforcer</artifactId>
<version>3.5.1-SNAPSHOT</version>
<version>3.6.0-SNAPSHOT</version>
</parent>

<artifactId>enforcer-api</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion enforcer-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>org.apache.maven.enforcer</groupId>
<artifactId>enforcer</artifactId>
<version>3.5.1-SNAPSHOT</version>
<version>3.6.0-SNAPSHOT</version>
</parent>

<artifactId>enforcer-rules</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -51,11 +52,16 @@ public final class RequireSameVersions extends AbstractStandardEnforcerRule {

private Set<String> 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
Expand Down Expand Up @@ -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<String, List<String>> collectVersionMembers(
Expand Down Expand Up @@ -119,7 +133,7 @@ private Map<String, List<String>> 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);
}
}
4 changes: 3 additions & 1 deletion enforcer-rules/src/site/apt/requireSameVersions.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
* <<reportPlugins>> - an optional list of report plugin patterns

* <<plugins>> - an optional list of both build and report plugin patterns

* <<uniqueVersions>> - if SNAPSHOTs should be compared by their timestamped version or not. Default: false

* <<sameModuleVersions>> - 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

[]

Expand Down
2 changes: 1 addition & 1 deletion maven-enforcer-extension/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.maven.enforcer</groupId>
<artifactId>enforcer</artifactId>
<version>3.5.1-SNAPSHOT</version>
<version>3.6.0-SNAPSHOT</version>
</parent>
<groupId>org.apache.maven.extensions</groupId>
<artifactId>maven-enforcer-extension</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion maven-enforcer-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>org.apache.maven.enforcer</groupId>
<artifactId>enforcer</artifactId>
<version>3.5.1-SNAPSHOT</version>
<version>3.6.0-SNAPSHOT</version>
</parent>

<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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>
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.maven.its.enforcer</groupId>
<artifactId>require-same-versions_multi-module-build</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>module1</artifactId>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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>
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.maven.its.enforcer</groupId>
<artifactId>require-same-versions_multi-module-build</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>module2</artifactId>
<version>1.1-SNAPSHOT</version>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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>
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.its.enforcer</groupId>
<artifactId>require-same-versions_multi-module-build</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<!--
! This entry is allowed
-->
<distributionManagement>
<repository>
<id>first</id>
<name>This is the name</name>
<url>file:///Test</url>
</repository>
</distributionManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>test</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireSameVersions>
<sameModuleVersions>true</sameModuleVersions>
</requireSameVersions>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<modules>
<module>module1</module>
<module>module2</module>
</modules>
</project>
Original file line number Diff line number Diff line change
@@ -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' )
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</parent>
<groupId>org.apache.maven.enforcer</groupId>
<artifactId>enforcer</artifactId>
<version>3.5.1-SNAPSHOT</version>
<version>3.6.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Apache Maven Enforcer</name>
Expand Down Expand Up @@ -81,7 +81,7 @@
<maven.site.path>enforcer-archives/enforcer-LATEST</maven.site.path>
<javaVersion>8</javaVersion>
<mockito.version>4.11.0</mockito.version>
<project.build.outputTimestamp>2024-05-26T17:46:08Z</project.build.outputTimestamp>
<project.build.outputTimestamp>2024-07-30T15:32:39Z</project.build.outputTimestamp>
<!-- the same as Maven 3.6.3 -->
<resolver.version>1.4.1</resolver.version>

Expand Down

0 comments on commit cf3fb67

Please sign in to comment.