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-420] cache dependencies across rules #152

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -21,6 +21,7 @@

import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import javax.annotation.Nonnull;

Expand Down Expand Up @@ -131,4 +132,13 @@ List<?> getComponentList ( String role )
* @return the container
*/
PlexusContainer getContainer();

/**
* Gets a cached value, or uses the provided producer to compute it.
*
* @param key a key to identify the value stored
* @param producer a supplier for the value if it's not already present
* @return a previously-cached or freshly-computed value
*/
Object getCache( String key, Supplier<?> producer );
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Up @@ -77,7 +77,7 @@ public void execute( EnforcerRuleHelper helper )

try
{
graphBuilder = (DependencyGraphBuilder) helper.getComponent( DependencyGraphBuilder.class );
graphBuilder = helper.getComponent( DependencyGraphBuilder.class );
}
catch ( ComponentLookupException e )
{
Expand All @@ -89,7 +89,7 @@ public void execute( EnforcerRuleHelper helper )
buildingRequest.setProject( project );

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

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

private Set<Artifact> getDependenciesToCheck( EnforcerRuleHelper helper,
ProjectBuildingRequest buildingRequest )
{
String cacheKey = buildingRequest.getProject().getId() + "_" + searchTransitive;

// check in the cache
Set<Artifact> dependencies =
(Set<Artifact>) helper.getCache( cacheKey, () -> getDependenciesToCheck( buildingRequest ) );

return dependencies;
}

protected Set<Artifact> getDependenciesToCheck( ProjectBuildingRequest buildingRequest )
{
Set<Artifact> dependencies = null;
Expand Down
Expand Up @@ -20,8 +20,10 @@
*/

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.execution.MavenSession;
Expand Down Expand Up @@ -50,6 +52,9 @@ public class DefaultEnforcementRuleHelper
/** The container. */
private PlexusContainer container;

/** A cache. */
private Map<String, Object> cache;

/**
* Instantiates a new default enforcement rule helper.
*
Expand All @@ -71,6 +76,8 @@ public DefaultEnforcementRuleHelper( MavenSession session, ExpressionEvaluator e
{
this.container = session.getContainer();
}

this.cache = new HashMap<>();
}

@Override
Expand Down Expand Up @@ -139,4 +146,10 @@ public PlexusContainer getContainer()
{
return container;
}

@Override
public Object getCache( String key, Supplier<?> producer )
{
return cache.computeIfAbsent( key, ( x ) -> producer.get() );
}
}