Add "include filter" support for the Maven plugin
Update AbstractDependencyFilterMojo to support include filters as well as exclude filters. Fixes gh-1824pull/1856/head
parent
e02dbbc16d
commit
c12ffe76be
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
* Licensed 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.springframework.boot.maven;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.maven.artifact.Artifact;
|
||||
import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactsFilter;
|
||||
import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
|
||||
import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
|
||||
|
||||
/**
|
||||
* Base class for {@link ArtifactsFilter} based on a {@link FilterableDependency} list.
|
||||
*
|
||||
* @author Stephane Nicol
|
||||
* @author David Turanski
|
||||
* @since 1.2
|
||||
*/
|
||||
public abstract class DependencyFilter extends AbstractArtifactsFilter {
|
||||
|
||||
private final List<? extends FilterableDependency> filters;
|
||||
|
||||
/**
|
||||
* Create a new instance with the list of {@link FilterableDependency} instance(s) to use.
|
||||
*/
|
||||
public DependencyFilter(List<? extends FilterableDependency> dependencies) {
|
||||
this.filters = dependencies;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public Set filter(Set artifacts) throws ArtifactFilterException {
|
||||
Set result = new HashSet();
|
||||
for (Object artifact : artifacts) {
|
||||
if (!filter((Artifact) artifact)) {
|
||||
result.add(artifact);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected abstract boolean filter(Artifact artifact);
|
||||
|
||||
/**
|
||||
* Check if the specified {@link org.apache.maven.artifact.Artifact} matches the
|
||||
* specified {@link org.springframework.boot.maven.FilterableDependency}. Returns {@code true}
|
||||
* if it should be excluded
|
||||
*/
|
||||
protected final boolean equals(Artifact artifact, FilterableDependency dependency) {
|
||||
if (!dependency.getGroupId().equals(artifact.getGroupId())) {
|
||||
return false;
|
||||
}
|
||||
if (!dependency.getArtifactId().equals(artifact.getArtifactId())) {
|
||||
return false;
|
||||
}
|
||||
return (dependency.getClassifier() == null || artifact.getClassifier() != null
|
||||
&& dependency.getClassifier().equals(artifact.getClassifier()));
|
||||
}
|
||||
|
||||
protected final List<? extends FilterableDependency> getFilters() {
|
||||
return this.filters;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.boot.maven;
|
||||
|
||||
import org.apache.maven.plugins.annotations.Parameter;
|
||||
|
||||
/**
|
||||
* A model for a dependency to include or exclude.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author David Turanski
|
||||
* @since 1.2
|
||||
*/
|
||||
abstract class FilterableDependency {
|
||||
|
||||
/**
|
||||
* The groupId of the artifact to exclude.
|
||||
*/
|
||||
@Parameter(required = true)
|
||||
private String groupId;
|
||||
|
||||
/**
|
||||
* The artifactId of the artifact to exclude.
|
||||
*/
|
||||
@Parameter(required = true)
|
||||
private String artifactId;
|
||||
|
||||
/**
|
||||
* The classifier of the artifact to exclude
|
||||
*/
|
||||
@Parameter
|
||||
private String classifier;
|
||||
|
||||
public String getGroupId() {
|
||||
return this.groupId;
|
||||
}
|
||||
|
||||
public void setGroupId(String groupId) {
|
||||
this.groupId = groupId;
|
||||
}
|
||||
|
||||
public String getArtifactId() {
|
||||
return this.artifactId;
|
||||
}
|
||||
|
||||
public void setArtifactId(String artifactId) {
|
||||
this.artifactId = artifactId;
|
||||
}
|
||||
|
||||
public String getClassifier() {
|
||||
return this.classifier;
|
||||
}
|
||||
|
||||
public void setClassifier(String classifier) {
|
||||
this.classifier = classifier;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.boot.maven;
|
||||
|
||||
/**
|
||||
* A model for a dependency to include.
|
||||
*
|
||||
* @author David Turanski
|
||||
* @since 1.2
|
||||
*/
|
||||
public class Include extends FilterableDependency {
|
||||
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
* Licensed 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.springframework.boot.maven;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.maven.artifact.Artifact;
|
||||
import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
|
||||
|
||||
/**
|
||||
* An {@link ArtifactsFilter} that filters out any artifact not matching an
|
||||
* {@link Include}.
|
||||
*
|
||||
* @author David Turanski
|
||||
* @since 1.2
|
||||
*/
|
||||
public class IncludeFilter extends DependencyFilter {
|
||||
|
||||
public IncludeFilter(List<Include> includes) {
|
||||
super(includes);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean filter(Artifact artifact) {
|
||||
for (FilterableDependency dependency : getFilters()) {
|
||||
if (equals(artifact, dependency)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
* Licensed 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.springframework.boot.maven;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.maven.artifact.Artifact;
|
||||
import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link org.springframework.boot.maven.IncludeFilter}.
|
||||
*
|
||||
* @author David Turanski
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public class IncludeFilterTests {
|
||||
|
||||
@Test
|
||||
public void includeSimple() throws ArtifactFilterException {
|
||||
IncludeFilter filter = new IncludeFilter(Arrays.asList(createInclude("com.foo",
|
||||
"bar")));
|
||||
Artifact artifact = createArtifact("com.foo", "bar");
|
||||
Set result = filter.filter(Collections.singleton(artifact));
|
||||
assertEquals("Should not have been filtered", 1, result.size());
|
||||
assertSame(artifact, result.iterator().next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void includeGroupIdNoMatch() throws ArtifactFilterException {
|
||||
IncludeFilter filter = new IncludeFilter(Arrays.asList(createInclude("com.foo",
|
||||
"bar")));
|
||||
Artifact artifact = createArtifact("com.baz", "bar");
|
||||
Set result = filter.filter(Collections.singleton(artifact));
|
||||
assertEquals("Should have been filtered", 0, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void includeArtifactIdNoMatch() throws ArtifactFilterException {
|
||||
IncludeFilter filter = new IncludeFilter(Arrays.asList(createInclude("com.foo",
|
||||
"bar")));
|
||||
Artifact artifact = createArtifact("com.foo", "biz");
|
||||
Set result = filter.filter(Collections.singleton(artifact));
|
||||
assertEquals("Should have been filtered", 0, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void includeClassifier() throws ArtifactFilterException {
|
||||
IncludeFilter filter = new IncludeFilter(Arrays.asList(createInclude("com.foo",
|
||||
"bar", "jdk5")));
|
||||
Artifact artifact = createArtifact("com.foo", "bar", "jdk5");
|
||||
Set result = filter.filter(Collections.singleton(artifact));
|
||||
assertEquals("Should not have been filtered", 1, result.size());
|
||||
assertSame(artifact, result.iterator().next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void includeClassifierNoTargetClassifier() throws ArtifactFilterException {
|
||||
IncludeFilter filter = new IncludeFilter(Arrays.asList(createInclude("com.foo",
|
||||
"bar", "jdk5")));
|
||||
Artifact artifact = createArtifact("com.foo", "bar");
|
||||
Set result = filter.filter(Collections.singleton(artifact));
|
||||
assertEquals("Should have been filtered", 0, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void includeClassifierNoMatch() throws ArtifactFilterException {
|
||||
IncludeFilter filter = new IncludeFilter(Arrays.asList(createInclude("com.foo",
|
||||
"bar", "jdk5")));
|
||||
Artifact artifact = createArtifact("com.foo", "bar", "jdk6");
|
||||
Set result = filter.filter(Collections.singleton(artifact));
|
||||
assertEquals("Should have been filtered", 0, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void includeMulti() throws ArtifactFilterException {
|
||||
IncludeFilter filter = new IncludeFilter(Arrays.asList(
|
||||
createInclude("com.foo", "bar"), createInclude("com.foo", "bar2"),
|
||||
createInclude("org.acme", "app")));
|
||||
Set<Artifact> artifacts = new HashSet<Artifact>();
|
||||
artifacts.add(createArtifact("com.foo", "bar"));
|
||||
artifacts.add(createArtifact("com.foo", "bar"));
|
||||
Artifact anotherAcme = createArtifact("org.acme", "another-app");
|
||||
artifacts.add(anotherAcme);
|
||||
Set result = filter.filter(artifacts);
|
||||
assertEquals("One dependency should have been filtered", 2, result.size());
|
||||
}
|
||||
|
||||
private Include createInclude(String groupId, String artifactId) {
|
||||
return createInclude(groupId, artifactId, null);
|
||||
}
|
||||
|
||||
private Include createInclude(String groupId, String artifactId, String classifier) {
|
||||
Include include = new Include();
|
||||
include.setGroupId(groupId);
|
||||
include.setArtifactId(artifactId);
|
||||
if (classifier != null) {
|
||||
include.setClassifier(classifier);
|
||||
}
|
||||
return include;
|
||||
}
|
||||
|
||||
private Artifact createArtifact(String groupId, String artifactId, String classifier) {
|
||||
Artifact a = mock(Artifact.class);
|
||||
given(a.getGroupId()).willReturn(groupId);
|
||||
given(a.getArtifactId()).willReturn(artifactId);
|
||||
given(a.getClassifier()).willReturn(classifier);
|
||||
return a;
|
||||
}
|
||||
|
||||
private Artifact createArtifact(String groupId, String artifactId) {
|
||||
return createArtifact(groupId, artifactId, null);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue