-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MENFORCER-423] Add rule to enforce an explicit dependency scope
- Loading branch information
Showing
8 changed files
with
351 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...rules/src/main/java/org/apache/maven/plugins/enforcer/RequireExplicitDependencyScope.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package org.apache.maven.plugins.enforcer; | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
import java.text.ChoiceFormat; | ||
import java.util.List; | ||
|
||
import org.apache.maven.enforcer.rule.api.EnforcerLevel; | ||
import org.apache.maven.enforcer.rule.api.EnforcerRule2; | ||
import org.apache.maven.enforcer.rule.api.EnforcerRuleException; | ||
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; | ||
import org.apache.maven.model.Dependency; | ||
import org.apache.maven.project.MavenProject; | ||
import org.apache.maven.shared.utils.logging.MessageBuilder; | ||
import org.apache.maven.shared.utils.logging.MessageUtils; | ||
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; | ||
|
||
/** | ||
* Checks that all dependencies have an explicitly declared scope in the non-effective pom (i.e. without taking | ||
* inheritance or dependency management into account). | ||
*/ | ||
public class RequireExplicitDependencyScope | ||
extends AbstractNonCacheableEnforcerRule | ||
implements EnforcerRule2 | ||
{ | ||
|
||
@Override | ||
public void execute( EnforcerRuleHelper helper ) | ||
throws EnforcerRuleException | ||
{ | ||
try | ||
{ | ||
int numMissingDependencyScopes = 0; | ||
MavenProject project = (MavenProject) helper.evaluate( "${project}" ); | ||
if ( project == null ) | ||
{ | ||
throw new ExpressionEvaluationException( "${project} is null" ); | ||
} | ||
List<Dependency> dependencies = project.getOriginalModel().getDependencies(); // this is the non-effective | ||
// model but the original one | ||
// without inheritance and | ||
// interpolation resolved | ||
// check scope without considering inheritance | ||
for ( Dependency dependency : dependencies ) | ||
{ | ||
helper.getLog().debug( "Found dependency " + dependency ); | ||
if ( dependency.getScope() == null ) | ||
{ | ||
MessageBuilder msgBuilder = MessageUtils.buffer(); | ||
msgBuilder | ||
.a( "Dependency " ).strong( dependency.getManagementKey() ) | ||
.a( " @ " ).strong( formatLocation( project, dependency.getLocation( "" ) ) ) | ||
.a( " does not have an explicit scope defined!" ).toString(); | ||
if ( getLevel() == EnforcerLevel.ERROR ) | ||
{ | ||
helper.getLog().error( msgBuilder.toString() ); | ||
} | ||
else | ||
{ | ||
helper.getLog().warn( msgBuilder.toString() ); | ||
} | ||
numMissingDependencyScopes++; | ||
} | ||
} | ||
if ( numMissingDependencyScopes > 0 ) | ||
{ | ||
ChoiceFormat scopesFormat = new ChoiceFormat( "1#scope|1<scopes" ); | ||
String logCategory = getLevel() == EnforcerLevel.ERROR ? "errors" : "warnings"; | ||
throw new EnforcerRuleException( "Found " + numMissingDependencyScopes + " missing dependency " | ||
+ scopesFormat.format( numMissingDependencyScopes ) | ||
+ ". Look at the " + logCategory + " emitted above for the details." ); | ||
} | ||
} | ||
catch ( ExpressionEvaluationException eee ) | ||
{ | ||
throw new EnforcerRuleException( "Cannot resolve expression: " + eee.getCause(), eee ); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
enforcer-rules/src/site/apt/requireExplicitDependencyScope.apt.vm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
~~ 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. | ||
|
||
------ | ||
Require Explicit Dependency Scope | ||
------ | ||
Konrad Windszus | ||
------ | ||
2022-08-03 | ||
------ | ||
|
||
Require Explicit Dependency Scope | ||
|
||
This rule enforces that all dependencies have an explicitly declared scope in the non-effective pom (i.e. without taking inheritance or dependency management into account). | ||
Useful when the scope is no longer part of the <<<dependencyManagement>>> or in general to force making developers a distinct decision (prevents the default scope <<<compile>>> being used for test dependencies by accident) | ||
|
||
The rule does not support parameters. | ||
|
||
Sample Plugin Configuration: | ||
|
||
+---+ | ||
<project> | ||
[...] | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-enforcer-plugin</artifactId> | ||
<version>${project.version}</version> | ||
<executions> | ||
<execution> | ||
<id>require-explicit-dependency-scope</id> | ||
<goals> | ||
<goal>enforce</goal> | ||
</goals> | ||
<configuration> | ||
<rules> | ||
<requireExplicitDependencyScope /> | ||
</rules> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
[...] | ||
</project> | ||
+---+ |
18 changes: 18 additions & 0 deletions
18
maven-enforcer-plugin/src/it/projects/require-dependency-scope/invoker.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.