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

[MENFORCER-332] Dependency graph performance fix #53

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions enforcer-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-common-artifact-filters</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-dependency-tree</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
Expand Down Expand Up @@ -85,14 +89,6 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-dependency-tree</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand All @@ -114,6 +110,10 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.enforcer.utils.DependencyGraphLookup;
import org.apache.maven.project.MavenProject;
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;
Expand All @@ -44,7 +43,7 @@ public abstract class AbstractBanDependencies
/** Specify if transitive dependencies should be searched (default) or only look at direct dependencies. */
private boolean searchTransitive = true;

private transient DependencyGraphBuilder graphBuilder;
private transient DependencyGraphLookup graphLookup;

@Override
public void execute( EnforcerRuleHelper helper )
Expand All @@ -64,25 +63,15 @@ public void execute( EnforcerRuleHelper helper )

try
{
graphBuilder = (DependencyGraphBuilder) helper.getComponent( DependencyGraphBuilder.class );
graphLookup = helper.getComponent( DependencyGraphLookup.class );
}
catch ( ComponentLookupException e )
{
// real cause is probably that one of the Maven3 graph builder could not be initiated and fails with a
// ClassNotFoundException
try
{
graphBuilder =
(DependencyGraphBuilder) helper.getComponent( DependencyGraphBuilder.class.getName(), "maven2" );
}
catch ( ComponentLookupException e1 )
{
throw new EnforcerRuleException( "Unable to lookup DependencyGraphBuilder: ", e );
}
throw new EnforcerRuleException( "Unable to lookup DependencyGraphLookup: ", e );
}

// get the correct list of dependencies
Set<Artifact> dependencies = getDependenciesToCheck( project );
Set<Artifact> dependencies = getDependenciesToCheck( helper );

// look for banned dependencies
Set<Artifact> foundExcludes = checkDependencies( dependencies, helper.getLog() );
Expand Down Expand Up @@ -113,29 +102,21 @@ protected CharSequence getErrorMessage( Artifact artifact )
return "Found Banned Dependency: " + artifact.getId() + System.lineSeparator();
}

protected Set<Artifact> getDependenciesToCheck( MavenProject project )
protected Set<Artifact> getDependenciesToCheck( EnforcerRuleHelper helper ) throws EnforcerRuleException
{
Set<Artifact> dependencies = null;
try
DependencyNode node = graphLookup.getTransformedDependencyGraph( helper );
if ( searchTransitive )
{
DependencyNode node = graphBuilder.buildDependencyGraph( project, null );
if ( searchTransitive )
{
dependencies = getAllDescendants( node );
}
else if ( node.getChildren() != null )
{
dependencies = new HashSet<Artifact>();
for ( DependencyNode depNode : node.getChildren() )
{
dependencies.add( depNode.getArtifact() );
}
}
dependencies = getAllDescendants( node );
}
catch ( DependencyGraphBuilderException e )
else if ( node.getChildren() != null )
{
// otherwise we need to change the signature of this protected method
throw new RuntimeException( e );
dependencies = new HashSet<>();
for ( DependencyNode depNode : node.getChildren() )
{
dependencies.add( depNode.getArtifact() );
}
}
return dependencies;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.plugins.enforcer.utils.ArtifactMatcher;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
import org.apache.maven.plugins.enforcer.utils.DependencyGraphLookup;
import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.apache.maven.shared.dependency.graph.internal.DefaultDependencyGraphBuilder;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.logging.console.ConsoleLogger;

/**
* This rule bans all transitive dependencies. There is a configuration option to exclude certain artifacts from being
Expand Down Expand Up @@ -65,6 +62,8 @@ public class BanTransitiveDependencies
*/
private List<String> includes;

private transient DependencyGraphLookup graphLookup;

/**
* Searches dependency tree recursively for transitive dependencies that are not excluded, while generating nice
* info message along the way.
Expand Down Expand Up @@ -153,18 +152,17 @@ public void execute( EnforcerRuleHelper helper )

final ArtifactMatcher exclusions = new ArtifactMatcher( excludes, includes );

DependencyNode rootNode = null;

try
{
MavenProject project = (MavenProject) helper.evaluate( "${project}" );
rootNode = createDependencyGraphBuilder().buildDependencyGraph( project, null );
graphLookup = helper.getComponent( DependencyGraphLookup.class );
}
catch ( Exception e )
catch ( ComponentLookupException e )
{
throw new EnforcerRuleException( "Error: Could not construct dependency tree.", e );
throw new EnforcerRuleException( "Unable to lookup DependencyGraphLookup: ", e );
}

DependencyNode rootNode = graphLookup.getTransformedDependencyGraph( helper );

String message = getMessage();
StringBuilder generatedMessage = null;
if ( message == null )
Expand All @@ -183,21 +181,5 @@ public void execute( EnforcerRuleHelper helper )
{
throw new EnforcerRuleException( "Error: Invalid version range.", e );
}

}

private DependencyGraphBuilder createDependencyGraphBuilder()
throws ComponentLookupException
{
// CHECKSTYLE_OFF: LineLength
DefaultDependencyGraphBuilder builder =
(DefaultDependencyGraphBuilder) helper.getContainer().lookup( DependencyGraphBuilder.class.getCanonicalName(),
"default" );
// CHECKSTYLE_ON: LineLength

builder.enableLogging( new ConsoleLogger( ConsoleLogger.LEVEL_DISABLED, "DefaultDependencyGraphBuilder" ) );

return builder;
}

}
Original file line number Diff line number Diff line change
@@ -1,47 +1,58 @@
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.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.project.MavenProject;

/**
* This rule checks that lists of plugins are not included.
*
* @author <a href="mailto:velo.br@gmail.com">Marvin Froeder</a>
*/
public class BannedPlugins
extends BannedDependencies
{
@Override
protected Set<Artifact> getDependenciesToCheck( MavenProject project )
{
return project.getPluginArtifacts();
}

@Override
protected CharSequence getErrorMessage( Artifact artifact )
{
return "Found Banned Plugin: " + artifact.getId() + System.lineSeparator();
}

}
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.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;

/**
* This rule checks that lists of plugins are not included.
*
* @author <a href="mailto:velo.br@gmail.com">Marvin Froeder</a>
*/
public class BannedPlugins
extends BannedDependencies
{
@Override
protected Set<Artifact> getDependenciesToCheck( EnforcerRuleHelper helper ) throws EnforcerRuleException
{
try
{
MavenProject project = helper.getComponent( MavenProject.class );
return project.getPluginArtifacts();
}
catch ( ComponentLookupException e )
{
throw new EnforcerRuleException( "Could not load MavenProject: ", e );
}
}

@Override
protected CharSequence getErrorMessage( Artifact artifact )
{
return "Found Banned Plugin: " + artifact.getId() + System.lineSeparator();
}

}