Skip to content

Commit 7848853

Browse files
[MENFORCER-276] Allow ignoring dependency scopes in RequireUpperBoundDeps
1 parent 42dacab commit 7848853

File tree

8 files changed

+159
-6
lines changed

8 files changed

+159
-6
lines changed

enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDeps.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public final class RequireUpperBoundDeps extends AbstractStandardEnforcerRule {
7373
*/
7474
private List<String> includes = null;
7575

76+
/**
77+
* Scope to exclude.
78+
*/
79+
private List<String> excludedScopes = Arrays.asList(SCOPE_TEST, SCOPE_PROVIDED);
80+
7681
private RequireUpperBoundDepsVisitor upperBoundDepsVisitor;
7782

7883
private final ResolverUtil resolverUtil;
@@ -101,14 +106,14 @@ public void setIncludes(List<String> includes) {
101106

102107
@Override
103108
public void execute() throws EnforcerRuleException {
104-
DependencyNode node =
105-
resolverUtil.resolveTransitiveDependenciesVerbose(Arrays.asList(SCOPE_TEST, SCOPE_PROVIDED));
109+
DependencyNode node = resolverUtil.resolveTransitiveDependenciesVerbose(excludedScopes);
106110
upperBoundDepsVisitor = new RequireUpperBoundDepsVisitor()
107111
.setUniqueVersions(uniqueVersions)
108112
.setIncludes(includes);
113+
getLog().debug(() -> resolverUtil.dumpTree(node));
109114
node.accept(upperBoundDepsVisitor);
110115
List<String> errorMessages = buildErrorMessages(upperBoundDepsVisitor.getConflicts());
111-
if (errorMessages.size() > 0) {
116+
if (!errorMessages.isEmpty()) {
112117
throw new EnforcerRuleException(
113118
"Failed while enforcing RequireUpperBoundDeps. The error(s) are " + errorMessages);
114119
}

enforcer-rules/src/site/apt/requireUpperBoundDeps.apt.vm

+17
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929
information about Maven dependency resolution, see
3030
{{{http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html}the Maven site}}.
3131

32+
The following parameters are supported by this rule:
33+
34+
* <<uniqueVersions>> - if SNAPSHOTs should be compared by their timestamped version or not. Default: <<<false>>>
35+
36+
* <<excludes>> - specify the dependencies that will be ignored. The format is <<<groupId[:artifactId]>>>
37+
38+
* <<includes>> - specify the dependencies that will be checked. If not empty only these will be checked. The format is <<<groupId[:artifactId]>>>
39+
40+
* <<excludedScopes>> - a list of dependency scope which will be excluded. Default: <<<test>>>, <<<provided>>>
41+
42+
[]
43+
3244
Here is a concrete example. This will cause a build to fail:
3345

3446
-----------------------------------------------------------------------------------
@@ -114,6 +126,11 @@ and
114126
<include>com.google.guava:guava</include>
115127
</includes>
116128
-->
129+
<!-- only artifacts with provided scope will be excluded
130+
<excludedScopes>
131+
<excludedScope>provided</excludedScopes>
132+
</excludedScopes>
133+
-->
117134
</requireUpperBoundDeps>
118135
</rules>
119136
</configuration>

enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/RequireUpperBoundDepsTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.apache.maven.enforcer.rules.dependency;
2020

21+
import org.apache.maven.enforcer.rule.api.EnforcerLogger;
2122
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
2223
import org.apache.maven.enforcer.rules.utils.DependencyNodeBuilder;
2324
import org.junit.jupiter.api.Test;
@@ -28,6 +29,7 @@
2829

2930
import static org.assertj.core.api.Assertions.assertThatCode;
3031
import static org.mockito.ArgumentMatchers.anyList;
32+
import static org.mockito.Mockito.mock;
3133
import static org.mockito.Mockito.when;
3234

3335
@ExtendWith(MockitoExtension.class)
@@ -42,6 +44,8 @@ class RequireUpperBoundDepsTest {
4244
@Test
4345
void testRule() throws Exception {
4446

47+
rule.setLog(mock(EnforcerLogger.class));
48+
4549
when(resolverUtil.resolveTransitiveDependenciesVerbose(anyList()))
4650
.thenReturn(new DependencyNodeBuilder()
4751
.withType(DependencyNodeBuilder.Type.POM)

maven-enforcer-plugin/src/it/mrm/repository/menforcer138_container-default-1.0-alpha-9-stable-1.pom

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<dependencies>
2828
<dependency>
2929
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
30-
<artifactId>menforcer1308_utils</artifactId>
30+
<artifactId>menforcer138_utils</artifactId>
3131
<version>1.0.4</version>
3232
</dependency>
3333
<dependency>

maven-enforcer-plugin/src/it/mrm/repository/menforcer138_utils-1.0.4.pom

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
<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">
2222
<modelVersion>4.0.0</modelVersion>
2323
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
24-
<artifactId>menforcer1308_utils</artifactId>
24+
<artifactId>menforcer138_utils</artifactId>
2525
<version>1.0.4</version>
26-
</project>
26+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.buildResult=failure
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*
20+
-->
21+
<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">
22+
<modelVersion>4.0.0</modelVersion>
23+
<groupId>org.apache.maven.enforcer.its</groupId>
24+
<artifactId>require-upper-bound-dependencies-scope-change</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
31+
<artifactId>maven-enforcer-plugin</artifactId>
32+
<version>@project.version@</version>
33+
<executions>
34+
<execution>
35+
<id>enforce</id>
36+
<goals>
37+
<goal>enforce</goal>
38+
</goals>
39+
<configuration>
40+
<rules>
41+
<requireUpperBoundDeps>
42+
<excludedScopes>
43+
<!-- only test scope is excluded -->
44+
<scope>test</scope>
45+
</excludedScopes>
46+
</requireUpperBoundDeps>
47+
</rules>
48+
</configuration>
49+
</execution>
50+
</executions>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
55+
<dependencies>
56+
<dependency>
57+
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
58+
<artifactId>menforcer128_api</artifactId>
59+
<version>1.4.0</version>
60+
<scope>provided</scope>
61+
</dependency>
62+
63+
<dependency>
64+
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
65+
<artifactId>menforcer466_requires_api150</artifactId>
66+
<version>1.0</version>
67+
</dependency>
68+
69+
<dependency>
70+
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
71+
<artifactId>menforcer138_utils</artifactId>
72+
<version>1.0.4</version>
73+
<scope>test</scope>
74+
</dependency>
75+
76+
<dependency>
77+
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
78+
<artifactId>menforcer466_requires_utils30</artifactId>
79+
<version>1.0</version>
80+
</dependency>
81+
82+
</dependencies>
83+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
def buildLog = new File( basedir, 'build.log' ).text
21+
22+
assert buildLog.contains( 'Rule 0: org.apache.maven.enforcer.rules.dependency.RequireUpperBoundDeps failed with message:' )
23+
24+
assert buildLog.contains('[ERROR] Require upper bound dependencies error for org.apache.maven.plugins.enforcer.its:menforcer128_api:1.4.0 [provided] paths to dependency are:')
25+
26+
assert !buildLog.contains('[ERROR] Require upper bound dependencies error for org.apache.maven.plugins.enforcer.its:menforcer138_utils:1.0.4')

0 commit comments

Comments
 (0)