Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to new Enforcer API - require m-enforcer-p 3.2.0 #263

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Expand Down
10 changes: 10 additions & 0 deletions src/it/circular-simple/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
File file = new File(basedir, "build.log");
assert file.exists();

String text = file.getText("utf-8");


assert text.contains('ERROR] Rule 0: org.codehaus.mojo.extraenforcer.dependencies.BanCircularDependencies failed with message:')
assert text.contains('[ERROR] Circular Dependency found. Your project\'s groupId:artifactId combination must not exist in the list of direct or transitive dependencies.')

return true;
2 changes: 1 addition & 1 deletion src/it/mojo-1744/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import org.codehaus.mojo.extraenforcerrules.it.BanDuplicateClassesLogParser;
import org.codehaus.mojo.extraenforcer.it.BanDuplicateClassesLogParser;

File log = new File( basedir, 'build.log' )
assert log.exists()
Expand Down
2 changes: 1 addition & 1 deletion src/it/mojo-1853/verify.groovy
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
final File file = new File( basedir, "build.log" );
final String buf = file.getText("utf-8");

assert buf.contains(/org.apache.maven.plugins.enforcer.RequirePropertyDiverges failed with message/);
assert buf.contains(/org.codehaus.mojo.extraenforcer.model.RequirePropertyDiverges failed with message/);

assert buf.contains('Property \'project.url\' evaluates to \'http://company/company-parent-pom/child-fail');

Expand Down
2 changes: 1 addition & 1 deletion src/it/mojo-1929/verify.groovy
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
final File file = new File( basedir, "build.log" );
final String buf = file.getText("utf-8");

assert buf.contains(/org.apache.maven.plugins.enforcer.RequirePropertyDiverges failed with message/);
assert buf.contains(/org.codehaus.mojo.extraenforcer.model.RequirePropertyDiverges failed with message/);

assert buf.contains('Property \'project.url\' evaluates to \'http://company/company-parent-pom/child-fail');

Expand Down
2 changes: 1 addition & 1 deletion src/it/require-project-url-missing/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ File file = new File( basedir, "build.log" );
assert file.exists();

String text = file.getText("utf-8");
assert text.contains('org.apache.maven.plugins.enforcer.RequireProjectUrl failed with message');
assert text.contains('org.codehaus.mojo.extraenforcer.model.RequireProjectUrl failed with message');

return true;
2 changes: 1 addition & 1 deletion src/it/require-project-url-present/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ File file = new File( basedir, "build.log" );
assert file.exists();

String text = file.getText("utf-8");
assert !text.contains('org.apache.maven.plugins.enforcer.RequireProjectUrl failed with message');
assert !text.contains('org.codehaus.mojo.extraenforcer.model.RequireProjectUrl failed with message');

return true;
2 changes: 1 addition & 1 deletion src/it/require-project-url-regex/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ File file = new File( basedir, "build.log" );
assert file.exists();

String text = file.getText("utf-8");
assert text.contains('org.apache.maven.plugins.enforcer.RequireProjectUrl failed with message');
assert text.contains('org.codehaus.mojo.extraenforcer.model.RequireProjectUrl failed with message');

return true;
2 changes: 1 addition & 1 deletion src/it/require-property-diverges/verify.groovy
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
final File file = new File( basedir, "build.log" );
final String buf = file.getText( "utf-8" );

assert buf.contains(/org.apache.maven.plugins.enforcer.RequirePropertyDiverges failed with message/);
assert buf.contains(/org.codehaus.mojo.extraenforcer.model.RequirePropertyDiverges failed with message/);

assert buf.contains('Property \'project.issueManagement\' is required for this build and not defined in hierarchy at all.');

Expand Down
2 changes: 1 addition & 1 deletion src/it/require-roles-missing-contributor/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ File file = new File( basedir, "build.log" );
assert file.exists();

String text = file.getText("utf-8");
assert text.contains('org.apache.maven.plugins.enforcer.RequireContributorRoles failed with message');
assert text.contains('org.codehaus.mojo.extraenforcer.model.RequireContributorRoles failed with message');
assert text.contains('Found no contributor representing role(s) \'[quality manager]\'');
return true;
2 changes: 1 addition & 1 deletion src/it/smokes/verify.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ try
File file = new File( basedir, "build.log" );
String buf = FileUtils.fileRead( file );

Pattern p = Pattern.compile( "\\Qorg.apache.maven.plugins.enforcer.BanDuplicateClasses failed with message\\E" );
Pattern p = Pattern.compile( "\\Qorg.codehaus.mojo.extraenforcer.dependencies.BanDuplicateClasses failed with message\\E" );
Matcher m = p.matcher( buf.toString() );
if ( !m.find() )
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.apache.maven.plugins.enforcer;
package org.codehaus.mojo.extraenforcer.dependencies;

import java.util.ArrayList;
import java.util.HashSet;
Expand All @@ -8,18 +8,14 @@

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.enforcer.rule.api.EnforcerRule;
import org.apache.maven.enforcer.rule.api.AbstractEnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
Expand All @@ -31,32 +27,22 @@
* @author Robert Scholte
*
*/
public abstract class AbstractResolveDependencies extends AbstractMojoHausEnforcerRule {
abstract class AbstractResolveDependencies extends AbstractEnforcerRule {

private DependencyGraphBuilder graphBuilder;
private final MavenSession session;
private final RepositorySystem repositorySystem;

private MavenSession session;
private RepositorySystem repositorySystem;
private final DependencyGraphBuilder graphBuilder;

private EnforcerRuleHelper helper;

public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
this.helper = helper;
protected AbstractResolveDependencies(
MavenSession session, RepositorySystem repositorySystem, DependencyGraphBuilder graphBuilder) {
this.session = session;
this.repositorySystem = repositorySystem;
this.graphBuilder = graphBuilder;
}

// Get components
try {
repositorySystem = helper.getComponent(RepositorySystem.class);
graphBuilder = helper.getComponent(DependencyGraphBuilder.class);
} catch (ComponentLookupException e) {
throw new EnforcerRuleException("Unable to lookup DependencyTreeBuilder: ", e);
}

// Resolve expressions
try {
session = (MavenSession) helper.evaluate("${session}");
} catch (ExpressionEvaluationException e) {
throw new EnforcerRuleException("Unable to lookup an expression " + e.getLocalizedMessage(), e);
}
@Override
public void execute() throws EnforcerRuleException {

ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
buildingRequest.setProject(session.getCurrentProject());
Expand Down Expand Up @@ -124,31 +110,6 @@ private void resolveArtifact(Artifact artifact) throws ArtifactResolutionExcepti
artifact.setResolved(true);
}

protected Log getLog() {
return helper.getLog();
}

/**
* {@inheritDoc}
*/
public boolean isCacheable() {
return false;
}

/**
* {@inheritDoc}
*/
public boolean isResultValid(EnforcerRule enforcerRule) {
return false;
}

/**
* {@inheritDoc}
*/
public String getCacheId() {
return "Does not matter as not cacheable";
}

/**
* Convert a wildcard into a regex.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.apache.maven.plugins.enforcer;
package org.codehaus.mojo.extraenforcer.dependencies;

/*
* Licensed to the Apache Software Foundation (ASF) under one
Expand All @@ -19,82 +19,76 @@
* under the License.
*/

import javax.inject.Inject;
import javax.inject.Named;

import java.util.HashSet;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.enforcer.rule.api.EnforcerRule;
import org.apache.maven.enforcer.rule.api.AbstractEnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRuleError;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;

/**
* Bans circular dependencies on the classpath.
*
* @since 1.0-alpha-4
*/
public class BanCircularDependencies extends AbstractMojoHausEnforcerRule {
@Named("banCircularDependencies")
public class BanCircularDependencies extends AbstractEnforcerRule {

private transient DependencyGraphBuilder graphBuilder;
private final DependencyGraphBuilder graphBuilder;
private final MavenProject project;
private final MavenSession session;

private String message;

@Inject
public BanCircularDependencies(DependencyGraphBuilder graphBuilder, MavenProject project, MavenSession session) {
this.graphBuilder = graphBuilder;
this.project = project;
this.session = session;
}

/**
* {@inheritDoc}
*/
public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
Log log = helper.getLog();

try {
graphBuilder = helper.getComponent(DependencyGraphBuilder.class);
} catch (ComponentLookupException e) {
throw new EnforcerRuleException("Unable to lookup DependencyGraphBuilder: ", e);
}

try {
MavenProject project = (MavenProject) helper.evaluate("${project}");
MavenSession session = (MavenSession) helper.evaluate("${session}");

ProjectBuildingRequest buildingRequest =
new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
buildingRequest.setProject(project);

Set<Artifact> artifacts = getDependenciesToCheck(buildingRequest);
if (artifacts != null) {
for (Artifact artifact : artifacts) {
log.debug("groupId: " + artifact.getGroupId() + project.getGroupId());
if (artifact.getGroupId().equals(project.getGroupId())) {
log.debug("artifactId: " + artifact.getArtifactId() + " " + project.getArtifactId());
if (artifact.getArtifactId().equals(project.getArtifactId())) {
throw new EnforcerRuleException(getErrorMessage() + "\n " + artifact.getGroupId() + ":"
+ artifact.getArtifactId() + "\n ");
}
public void execute() throws EnforcerRuleException {

ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
buildingRequest.setProject(project);

Set<Artifact> artifacts = getDependenciesToCheck(buildingRequest);
if (artifacts != null) {
for (Artifact artifact : artifacts) {
getLog().debug("groupId: " + artifact.getGroupId() + project.getGroupId());
if (artifact.getGroupId().equals(project.getGroupId())) {
getLog().debug("artifactId: " + artifact.getArtifactId() + " " + project.getArtifactId());
if (artifact.getArtifactId().equals(project.getArtifactId())) {
throw new EnforcerRuleException(getErrorMessage() + "\n " + artifact.getGroupId() + ":"
+ artifact.getArtifactId() + "\n ");
}
}
}
} catch (ExpressionEvaluationException e) {
log.error("Error checking for circular dependencies", e);
e.printStackTrace();
}
}

protected Set<Artifact> getDependenciesToCheck(ProjectBuildingRequest buildingRequest) {
protected Set<Artifact> getDependenciesToCheck(ProjectBuildingRequest buildingRequest) throws EnforcerRuleError {
Set<Artifact> dependencies;
try {
DependencyNode node = graphBuilder.buildDependencyGraph(buildingRequest, null);
dependencies = getAllDescendants(node);
} catch (DependencyGraphBuilderException e) {
// otherwise we need to change the signature of this protected method
throw new RuntimeException(e);
throw new EnforcerRuleError(e);
}
return dependencies;
}
Expand All @@ -121,25 +115,4 @@ private String getErrorMessage() {
}
return message;
}

/**
* {@inheritDoc}
*/
public boolean isCacheable() {
return false;
}

/**
* {@inheritDoc}
*/
public boolean isResultValid(EnforcerRule enforcerRule) {
return false;
}

/**
* {@inheritDoc}
*/
public String getCacheId() {
return "Does not matter as not cacheable";
}
}
Loading