Skip to content

Commit

Permalink
[MENFORCER-211] wildcard ignore in requireReleaseDeps
Browse files Browse the repository at this point in the history
  • Loading branch information
rfscholte committed Jul 24, 2021
1 parent 335f26e commit 5409be8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
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.ArtifactUtils;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.artifact.filter.StrictPatternExcludesArtifactFilter;
import org.apache.maven.shared.artifact.filter.StrictPatternIncludesArtifactFilter;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;

/**
Expand Down Expand Up @@ -162,32 +160,35 @@ protected Set<Artifact> checkDependencies( Set<Artifact> dependencies, Log log )
* @param dependencies the list of dependencies to filter
* @return the resulting set of dependencies
*/
public Set<Artifact> filterArtifacts( Set<Artifact> dependencies )
protected Set<Artifact> filterArtifacts( Set<Artifact> dependencies ) throws EnforcerRuleException
{
if ( includes == null && excludes == null )
{
return dependencies;
}

AndArtifactFilter filter = new AndArtifactFilter( );
Set<Artifact> included;
if ( includes != null )
{
filter.add( new StrictPatternIncludesArtifactFilter( includes ) );
included = ArtifactUtils.checkDependencies( dependencies, includes );
}
if ( excludes != null )
else
{
filter.add( new StrictPatternExcludesArtifactFilter( excludes ) );
included = new HashSet<>( dependencies );
}

Set<Artifact> result = new HashSet<>();
for ( Artifact artifact : dependencies )

// anything specifically included should be removed
// from the ban list.
if ( included != null )
{
if ( filter.include( artifact ) )
Set<Artifact> excluded = ArtifactUtils.checkDependencies( dependencies, excludes );

if ( excluded != null )
{
result.add( artifact );
included.removeAll( excluded );
}
}
return result;
return included;
}

public final boolean isOnlyWhenRelease()
Expand Down
4 changes: 3 additions & 1 deletion enforcer-rules/src/site/apt/requireReleaseDeps.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ Require Release Dependencies

* failWhenParentIsSnapshot - if the parent should be checked. Default: true

* includes - List of dependency patterns to include when checking for snapshot versions
* includes - List of dependency patterns to include when checking for snapshot versions.

* excludes - List of dependency patterns to exclude when checking for snapshot versions

[]

The include/exclude pattern looks like <<<groupId:artifactId:version:type:scope>>> and supports wildcards(*).


Sample Plugin Configuration:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,34 @@
* under the License.
*/

import java.io.IOException;
import java.util.Collections;
import java.util.Set;

import junit.framework.TestCase;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.plugin.testing.ArtifactStubFactory;
import org.apache.maven.plugins.enforcer.utils.EnforcerRuleUtilsHelper;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;
import org.junit.Test;

/**
* The Class TestNoSnapshots.
*
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
*/
public class TestRequireReleaseDeps
extends TestCase
{

/**
* Test rule.
*
* @throws IOException Signals that an I/O exception has occurred.
*/
@Test
public void testRule()
throws IOException
throws Exception
{

ArtifactStubFactory factory = new ArtifactStubFactory();
MockProject project = new MockProject();
EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
Expand Down Expand Up @@ -89,9 +87,26 @@ public void testRule()

rule.setFailWhenParentIsSnapshot( false );
EnforcerRuleUtilsHelper.execute( rule, helper, false );


}

@Test
public void testWildcardIgnore() throws Exception
{
RequireReleaseDeps rule = newRequireReleaseDeps();
rule.setExcludes( Collections.singletonList( "*:*:*:*:test" ) );
rule.setOnlyWhenRelease( true );
rule.setSearchTransitive( false );

ArtifactStubFactory factory = new ArtifactStubFactory();
MockProject project = new MockProject();
project.setArtifact( factory.getReleaseArtifact() );
project.setDependencyArtifacts( Collections.singleton( factory.createArtifact( "g", "a", "1.0-SNAPSHOT", "test" ) ) );
EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );

EnforcerRuleUtilsHelper.execute( rule, helper, false );
}



private RequireReleaseDeps newRequireReleaseDeps()
{
Expand All @@ -114,6 +129,7 @@ protected Set<Artifact> getDependenciesToCheck( ProjectBuildingRequest buildingR
/**
* Test id.
*/
@Test
public void testId()
{
RequireReleaseDeps rule = newRequireReleaseDeps();
Expand Down

0 comments on commit 5409be8

Please sign in to comment.