Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Adding exclusion unit tests
  • Loading branch information
ldhardy committed Jun 24, 2024
commit 99f95091419976257607d1cf67902092dceb0d71
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ under the License.
<configuration>
<excludeGroupIds>skip.this.groupid,skip.this.groupid.too</excludeGroupIds>
<excludeArtifactIds>skip-this-artifact,skip-this-artifact-too</excludeArtifactIds>
<excludeScope>compile,system</excludeScope>
<!-- the maven-common-artifact-filters library does not support comma delimited
scope exclusions. only one at a time. the other filters support comma delimited lists-->
<!-- <excludeScope>compile,system</excludeScope> -->
<excludeScope>system</excludeScope>
<excludeTypes>ear</excludeTypes>
<excludeClassifiers>skipThisClassifier,skipThisClassifierToo</excludeClassifiers>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ assert !buildLog.contains( 'Resolving artifact skip.this.groupid.too' )
// Did type excludes work?

// Did classifier excludes work?
assert !buildLog.contains( 'Resolving artifact ch.qos.logback:logback-core:jar:skipThisClassifier' )


return true
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ public abstract class AbstractDependencyFilterMojo extends AbstractDependencyMoj

/**
* Scope threshold to exclude, if no value is defined for include.
* An empty string indicates no dependencies (default).<br>
* An empty string indicates no dependencies (default). Unlike the other exclusion parameters,
* does not support a comma-delimited list of scope exclusions. Just one scope may be excluded at a time.<br>
* The scope threshold value being interpreted is the scope as
* Maven filters for creating a classpath, not as specified in the pom. In summary:
* <ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.dependency.utils.DependencyUtil;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
import org.apache.maven.shared.artifact.filter.collection.ClassifierFilter;
import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
import org.apache.maven.shared.artifact.filter.collection.TypeFilter;
import org.apache.maven.shared.artifact.filter.resolve.TransformableFilter;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
Expand Down Expand Up @@ -71,7 +78,7 @@ protected void doExecute() throws MojoExecutionException {
}
}

} catch (DependencyResolverException e) {
} catch (DependencyResolverException | ArtifactFilterException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
Expand Down Expand Up @@ -133,8 +140,9 @@ private TransformableFilter getTransformableFilter() {
*
* @return set of resolved plugin artifacts.
* @throws DependencyResolverException in case of an error while resolving the artifacts.
* @throws ArtifactFilterException
*/
protected Set<Artifact> resolvePluginArtifacts() throws DependencyResolverException {
protected Set<Artifact> resolvePluginArtifacts() throws DependencyResolverException, ArtifactFilterException {

Set<Artifact> plugins = getProject().getPluginArtifacts();
Set<Artifact> reports = getProject().getReportArtifacts();
Expand All @@ -143,6 +151,13 @@ protected Set<Artifact> resolvePluginArtifacts() throws DependencyResolverExcept
artifacts.addAll(reports);
artifacts.addAll(plugins);

getLog().debug("Size of artifacts before filtering " + artifacts.size());

final FilterArtifacts filter = getArtifactsFilter();
artifacts = filter.filter(artifacts);

getLog().debug("Size of artifacts after filtering " + artifacts.size());

Set<DependableCoordinate> dependableCoordinates = artifacts.stream()
.map(this::createDependendableCoordinateFromArtifact)
.collect(Collectors.toSet());
Expand All @@ -152,6 +167,37 @@ protected Set<Artifact> resolvePluginArtifacts() throws DependencyResolverExcept
return resolveDependableCoordinate(buildingRequest, dependableCoordinates, "plugins");
}

protected FilterArtifacts getArtifactsFilter() {
final FilterArtifacts filter = new FilterArtifacts();

if (excludeReactor) {

filter.addFilter(new ExcludeReactorProjectsArtifactFilter(reactorProjects, getLog()));
}

filter.addFilter(new ScopeFilter(
DependencyUtil.cleanToBeTokenizedString(this.includeScope),
DependencyUtil.cleanToBeTokenizedString(this.excludeScope)));

filter.addFilter(new TypeFilter(
DependencyUtil.cleanToBeTokenizedString(this.includeTypes),
DependencyUtil.cleanToBeTokenizedString(this.excludeTypes)));

filter.addFilter(new ClassifierFilter(
DependencyUtil.cleanToBeTokenizedString(this.includeClassifiers),
DependencyUtil.cleanToBeTokenizedString(this.excludeClassifiers)));

filter.addFilter(new GroupIdFilter(
DependencyUtil.cleanToBeTokenizedString(this.includeGroupIds),
DependencyUtil.cleanToBeTokenizedString(this.excludeGroupIds)));

filter.addFilter(new ArtifactIdFilter(
DependencyUtil.cleanToBeTokenizedString(this.includeArtifactIds),
DependencyUtil.cleanToBeTokenizedString(this.excludeArtifactIds)));

return filter;
}

private DependableCoordinate createDependendableCoordinateFromArtifact(final Artifact artifact) {
final DefaultDependableCoordinate result = new DefaultDependableCoordinate();
result.setGroupId(artifact.getGroupId());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
/*
* 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.
*/
package org.apache.maven.plugins.dependency.resolvers;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.testing.stubs.ArtifactStub;
import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
import org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
import org.junit.jupiter.api.Disabled;

public class GoOfflineMojoTest extends AbstractDependencyMojoTestCase {
private GoOfflineMojo subject;

protected void setUp() throws Exception {
// required for mojo lookups to work
super.setUp("go-offline", true);
MavenProject project = new DependencyProjectStub();
getContainer().addComponent(project, MavenProject.class.getName());

MavenSession session = newMavenSession(project);
getContainer().addComponent(session, MavenSession.class.getName());
}

String GROUP_EXCLUDE_PREFIX = "skip.this.groupid";

String ARTIFACT_EXCLUDE_PREFIX = "skip-this-artifact";

String CLASSIFIER_EXCLUDE_PREFIX = "skipThisClassifier";

String DUMMY_ARTIFACT_NAME = "dummy-artifact";

String STUB_ARTIFACT_VERSION = "3.14";

String VALID_GROUP = "org.junit.jupiter";

public void test_excludeGroupIds() throws Exception {
File testPom = new File(getBasedir(), "target/test-classes/unit/go-offline-test/exclude-plugin-config.xml");

subject = (GoOfflineMojo) lookupMojo("go-offline", testPom);
assertNotNull(subject);

Artifact artifact1 = new ArtifactStub();
artifact1.setGroupId(GROUP_EXCLUDE_PREFIX);
artifact1.setArtifactId(DUMMY_ARTIFACT_NAME);
artifact1.setVersion(STUB_ARTIFACT_VERSION);

Artifact artifact2 = new ArtifactStub();
artifact2.setGroupId(GROUP_EXCLUDE_PREFIX + ".too");
artifact2.setArtifactId(DUMMY_ARTIFACT_NAME);
artifact2.setVersion(STUB_ARTIFACT_VERSION + "-SNAPSHOT");

Artifact artifact3 = new ArtifactStub();
artifact3.setGroupId("dont.skip.me");
artifact3.setArtifactId(DUMMY_ARTIFACT_NAME);
artifact3.setVersion("1.0");

Set<Artifact> artifacts = new HashSet<>();
artifacts.add(artifact1);
artifacts.add(artifact2);
artifacts.add(artifact3);

assertEquals(3, artifacts.size());
FilterArtifacts filter = subject.getArtifactsFilter();
artifacts = filter.filter(artifacts);
assertEquals(1, artifacts.size());
assertTrue(artifacts.contains(artifact3));
assertFalse(artifacts.contains(artifact1));
assertFalse(artifacts.contains(artifact2));
}

public void test_excludeArtifactIds() throws Exception {
File testPom = new File(getBasedir(), "target/test-classes/unit/go-offline-test/exclude-plugin-config.xml");

subject = (GoOfflineMojo) lookupMojo("go-offline", testPom);
assertNotNull(subject);

Artifact artifact1 = new ArtifactStub();
artifact1.setGroupId(VALID_GROUP);
artifact1.setArtifactId(ARTIFACT_EXCLUDE_PREFIX);
artifact1.setVersion(STUB_ARTIFACT_VERSION + "-SNAPSHOT");

Artifact artifact2 = new ArtifactStub();
artifact2.setGroupId(VALID_GROUP);
artifact2.setArtifactId(ARTIFACT_EXCLUDE_PREFIX + "-too");
artifact2.setVersion(STUB_ARTIFACT_VERSION);

Artifact artifact3 = new ArtifactStub();
artifact3.setGroupId("dont.skip.me");
artifact3.setArtifactId("dummy-artifact");
artifact3.setVersion("1.0");

Set<Artifact> artifacts = new HashSet<>();
artifacts.add(artifact1);
artifacts.add(artifact2);
artifacts.add(artifact3);

assertEquals(3, artifacts.size());
FilterArtifacts filter = subject.getArtifactsFilter();
artifacts = filter.filter(artifacts);
assertEquals(1, artifacts.size());
assertTrue(artifacts.contains(artifact3));
assertFalse(artifacts.contains(artifact1));
assertFalse(artifacts.contains(artifact2));
}

public void test_excludeScope() throws Exception {
File testPom = new File(getBasedir(), "target/test-classes/unit/go-offline-test/exclude-plugin-config.xml");

subject = (GoOfflineMojo) lookupMojo("go-offline", testPom);
assertNotNull(subject);

Artifact artifact1 = new ArtifactStub();
artifact1.setGroupId(VALID_GROUP);
artifact1.setArtifactId(DUMMY_ARTIFACT_NAME);
artifact1.setVersion(STUB_ARTIFACT_VERSION + "-SNAPSHOT");

Artifact artifact2 = new ArtifactStub();
artifact2.setGroupId(VALID_GROUP);
artifact2.setArtifactId(DUMMY_ARTIFACT_NAME + "-too");
artifact2.setVersion(STUB_ARTIFACT_VERSION);
artifact2.setScope("system");

Artifact artifact3 = new ArtifactStub();
artifact3.setGroupId(VALID_GROUP);
artifact3.setArtifactId(DUMMY_ARTIFACT_NAME);
artifact3.setVersion(STUB_ARTIFACT_VERSION);

Set<Artifact> artifacts = new HashSet<>();
artifacts.add(artifact1);
artifacts.add(artifact2);
artifacts.add(artifact3);

assertEquals(3, artifacts.size());
FilterArtifacts filter = subject.getArtifactsFilter();
artifacts = filter.filter(artifacts);
assertEquals(2, artifacts.size());
assertTrue(artifacts.contains(artifact3));
assertTrue(artifacts.contains(artifact1));
assertFalse(artifacts.contains(artifact2));
}

public void test_excludeTypes() throws Exception {
File testPom = new File(getBasedir(), "target/test-classes/unit/go-offline-test/exclude-plugin-config.xml");

subject = (GoOfflineMojo) lookupMojo("go-offline", testPom);
assertNotNull(subject);

ArtifactStub artifact1 = new ArtifactStub();
artifact1.setGroupId(VALID_GROUP);
artifact1.setArtifactId(DUMMY_ARTIFACT_NAME);
artifact1.setVersion(STUB_ARTIFACT_VERSION + "-SNAPSHOT");
artifact1.setType("ear");

ArtifactStub artifact2 = new ArtifactStub();
artifact2.setGroupId(VALID_GROUP);
artifact2.setArtifactId(DUMMY_ARTIFACT_NAME + "-too");
artifact2.setVersion(STUB_ARTIFACT_VERSION);
artifact2.setType("war");

ArtifactStub artifact3 = new ArtifactStub();
artifact3.setGroupId(VALID_GROUP);
artifact3.setArtifactId(DUMMY_ARTIFACT_NAME);
artifact3.setVersion(STUB_ARTIFACT_VERSION);
artifact3.setType("pom");

Set<Artifact> artifacts = new HashSet<>();
artifacts.add(artifact1);
artifacts.add(artifact2);
artifacts.add(artifact3);

assertEquals(3, artifacts.size());
FilterArtifacts filter = subject.getArtifactsFilter();
artifacts = filter.filter(artifacts);
assertEquals(1, artifacts.size());
assertFalse(artifacts.contains(artifact3));
assertTrue(artifacts.contains(artifact2));
assertFalse(artifacts.contains(artifact1));
}

/**
* Can't set a classifier on the ArtifactStub as of maven-plugin-testing-harness-3.3.0, there is a getter but no
* setter. If that ever gets implemented, comment in these two lines to support unit testing for this case
*
* @throws Exception
*/
@Disabled("Requires update to maven-plugin-test-harness to support this test")
public void xtest_excludeClassifiers() throws Exception {
File testPom = new File(getBasedir(), "target/test-classes/unit/go-offline-test/exclude-plugin-config.xml");

subject = (GoOfflineMojo) lookupMojo("go-offline", testPom);
assertNotNull(subject);

ArtifactStub artifact1 = new ArtifactStub();
artifact1.setGroupId(VALID_GROUP);
artifact1.setArtifactId(DUMMY_ARTIFACT_NAME);
artifact1.setVersion(STUB_ARTIFACT_VERSION + "-SNAPSHOT");
// artifact1.setClassifier(CLASSIFIER_EXCLUDE_PREFIX);

ArtifactStub artifact2 = new ArtifactStub();
artifact2.setGroupId(VALID_GROUP);
artifact2.setArtifactId(DUMMY_ARTIFACT_NAME + "-too");
artifact2.setVersion(STUB_ARTIFACT_VERSION);
// artifact2.setClassifier(CLASSIFIER_EXCLUDE_PREFIX + "Too");

ArtifactStub artifact3 = new ArtifactStub();
artifact3.setGroupId(VALID_GROUP);
artifact3.setArtifactId(DUMMY_ARTIFACT_NAME);
artifact3.setVersion(STUB_ARTIFACT_VERSION);

Set<Artifact> artifacts = new HashSet<>();
artifacts.add(artifact1);
artifacts.add(artifact2);
artifacts.add(artifact3);

assertEquals(3, artifacts.size());
FilterArtifacts filter = subject.getArtifactsFilter();
artifacts = filter.filter(artifacts);
assertEquals(1, artifacts.size());
assertFalse(artifacts.contains(artifact1));
assertFalse(artifacts.contains(artifact2));
assertTrue(artifacts.contains(artifact3));
}
}
Loading