This commit is contained in:
Phillip Webb 2014-05-06 17:06:57 +01:00
parent 1fc6bbda4f
commit 32d2fc25c7
9 changed files with 106 additions and 145 deletions

View File

@ -26,21 +26,22 @@ import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
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.FilterArtifacts;
import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
/**
* A base mojo filtering the dependencies of the project.
*
*
* @author Stephane Nicoll
* @since 1.1
*/
public abstract class AbstractDependencyFilterMojo extends AbstractMojo {
/**
* Collection of artifact definitions to exclude. The {@link Exclude}
* element defines a {@code groupId} and {@code artifactId} mandatory
* properties and an optional {@code classifier} property.
* Collection of artifact definitions to exclude. The {@link Exclude} element defines
* a {@code groupId} and {@code artifactId} mandatory properties and an optional
* {@code classifier} property.
* @since 1.1
*/
@Parameter
@ -51,15 +52,14 @@ public abstract class AbstractDependencyFilterMojo extends AbstractMojo {
* @since 1.1
*/
@Parameter(property = "excludeGroupIds", defaultValue = "")
protected String excludeGroupIds;
private String excludeGroupIds;
/**
* Comma separated list of artifact names to exclude.
* @since 1.1
*/
@Parameter(property = "excludeArtifactIds", defaultValue = "")
protected String excludeArtifactIds;
private String excludeArtifactIds;
protected void setExcludes(List<Exclude> excludes) {
this.excludes = excludes;
@ -74,8 +74,8 @@ public abstract class AbstractDependencyFilterMojo extends AbstractMojo {
}
@SuppressWarnings("unchecked")
protected Set<Artifact> filterDependencies(Set<Artifact> dependencies, FilterArtifacts filters)
throws MojoExecutionException {
protected Set<Artifact> filterDependencies(Set<Artifact> dependencies,
FilterArtifacts filters) throws MojoExecutionException {
try {
return filters.filter(dependencies);
}
@ -84,28 +84,37 @@ public abstract class AbstractDependencyFilterMojo extends AbstractMojo {
}
}
protected void initializeFilterArtifacts(FilterArtifacts filters) {
filters.addFilter(new ArtifactIdFilter("", cleanConfigItem(this.excludeArtifactIds)));
filters.addFilter(new GroupIdFilter("", cleanConfigItem(this.excludeGroupIds)));
/**
* Return artifact filters configured for this MOJO.
* @param additionalFilters optional additional filters to apply
* @return the filters
*/
protected final FilterArtifacts getFilters(ArtifactsFilter... additionalFilters) {
FilterArtifacts filters = new FilterArtifacts();
for (ArtifactsFilter additionalFilter : additionalFilters) {
filters.addFilter(additionalFilter);
}
filters.addFilter(new ArtifactIdFilter("",
cleanFilterConfig(this.excludeArtifactIds)));
filters.addFilter(new GroupIdFilter("", cleanFilterConfig(this.excludeGroupIds)));
if (this.excludes != null) {
filters.addFilter(new ExcludeFilter(this.excludes));
}
return filters;
}
static String cleanConfigItem(String content) {
private String cleanFilterConfig(String content) {
if (content == null || content.trim().isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(content, ",");
while (st.hasMoreElements()) {
String t = st.nextToken();
sb.append(t.trim());
if (st.hasMoreElements()) {
sb.append(",");
StringBuilder cleaned = new StringBuilder();
StringTokenizer tokenizer = new StringTokenizer(content, ",");
while (tokenizer.hasMoreElements()) {
cleaned.append(tokenizer.nextToken().trim());
if (tokenizer.hasMoreElements()) {
cleaned.append(",");
}
}
return sb.toString();
return cleaned.toString();
}
}

View File

@ -20,7 +20,7 @@ import org.apache.maven.plugins.annotations.Parameter;
/**
* A model for a dependency to exclude.
*
*
* @author Stephane Nicoll
* @since 1.1
*/
@ -45,7 +45,7 @@ public class Exclude {
private String classifier;
public String getGroupId() {
return groupId;
return this.groupId;
}
public void setGroupId(String groupId) {
@ -53,7 +53,7 @@ public class Exclude {
}
public String getArtifactId() {
return artifactId;
return this.artifactId;
}
public void setArtifactId(String artifactId) {
@ -61,7 +61,7 @@ public class Exclude {
}
public String getClassifier() {
return classifier;
return this.classifier;
}
public void setClassifier(String classifier) {

View File

@ -26,25 +26,25 @@ import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterExceptio
/**
* An {@link org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter
* ArtifactsFilter} that filters out any artifact matching a configurable list
* of {@link Exclude} instances.
*
* ArtifactsFilter} that filters out any artifact matching a configurable list of
* {@link Exclude} instances.
*
* @author Stephane Nicoll
* @since 1.1
*/
public class ExcludeFilter extends AbstractArtifactsFilter{
public class ExcludeFilter extends AbstractArtifactsFilter {
private final List<Exclude> excludes;
/**
* Create a new instance with the list of {@link Exclude}
* instance(s) to use.
* Create a new instance with the list of {@link Exclude} instance(s) to use.
*/
public ExcludeFilter(List<Exclude> excludes) {
this.excludes = excludes;
}
@Override
@SuppressWarnings("rawtypes")
public Set filter(Set artifacts) throws ArtifactFilterException {
Set<Artifact> result = new HashSet<Artifact>();
for (Object a : artifacts) {
@ -57,11 +57,11 @@ public class ExcludeFilter extends AbstractArtifactsFilter{
}
/**
* Check if the specified {@link Artifact} matches one of the
* known excludes. Returns {@code true} if it should be excluded
* Check if the specified {@link Artifact} matches one of the known excludes. Returns
* {@code true} if it should be excluded
*/
private boolean matchExclude(Artifact artifact) {
for (Exclude exclude : excludes) {
for (Exclude exclude : this.excludes) {
if (match(artifact, exclude)) {
return true;
}
@ -70,8 +70,8 @@ public class ExcludeFilter extends AbstractArtifactsFilter{
}
/**
* Check if the specified {@link Artifact} matches the specified
* {@link Exclude}. Returns {@code true} if it should be excluded
* Check if the specified {@link Artifact} matches the specified {@link Exclude}.
* Returns {@code true} if it should be excluded
*/
private boolean match(Artifact artifact, Exclude exclude) {
if (!exclude.getGroupId().equals(artifact.getGroupId())) {
@ -80,8 +80,8 @@ public class ExcludeFilter extends AbstractArtifactsFilter{
if (!exclude.getArtifactId().equals(artifact.getArtifactId())) {
return false;
}
return exclude.getClassifier() == null ||
artifact.getClassifier() != null && exclude.getClassifier().equals(artifact.getClassifier());
return (exclude.getClassifier() == null || artifact.getClassifier() != null
&& exclude.getClassifier().equals(artifact.getClassifier()));
}

View File

@ -23,7 +23,6 @@ import java.util.concurrent.TimeUnit;
import java.util.jar.JarFile;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
@ -33,8 +32,6 @@ import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
import org.springframework.boot.loader.tools.Layout;
import org.springframework.boot.loader.tools.Layouts;
import org.springframework.boot.loader.tools.Libraries;
@ -139,9 +136,8 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
repackager.setLayout(this.layout.layout());
}
FilterArtifacts filters = new FilterArtifacts();
initializeFilterArtifacts(filters);
Set<Artifact> artifacts = filterDependencies(this.project.getArtifacts(), filters);
Set<Artifact> artifacts = filterDependencies(this.project.getArtifacts(),
getFilters());
Libraries libraries = new ArtifactsLibraries(artifacts);
try {

View File

@ -38,7 +38,6 @@ import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactFeatureFilter;
import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
import org.springframework.boot.loader.tools.FileUtils;
import org.springframework.boot.loader.tools.JavaExecutable;
import org.springframework.boot.loader.tools.MainClassFinder;
@ -46,7 +45,7 @@ import org.springframework.boot.loader.tools.RunProcess;
/**
* Run an executable archive application.
*
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
@ -256,11 +255,9 @@ public class RunMojo extends AbstractDependencyFilterMojo {
urls.add(this.classesDirectory.toURI().toURL());
}
private void addDependencies(List<URL> urls) throws MalformedURLException, MojoExecutionException {
FilterArtifacts filters = new FilterArtifacts();
filters.addFilter(new TestArtifactFilter());
initializeFilterArtifacts(filters);
private void addDependencies(List<URL> urls) throws MalformedURLException,
MojoExecutionException {
FilterArtifacts filters = getFilters(new TestArtifactFilter());
Set<Artifact> artifacts = filterDependencies(this.project.getArtifacts(), filters);
for (Artifact artifact : artifacts) {
if (artifact.getFile() != null) {
@ -274,6 +271,7 @@ public class RunMojo extends AbstractDependencyFilterMojo {
super("", Artifact.SCOPE_TEST);
}
@Override
protected String getArtifactFeature(Artifact artifact) {
return artifact.getScope();
}

View File

@ -16,10 +16,6 @@
package org.springframework.boot.maven;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
@ -29,11 +25,15 @@ import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
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;
/**
*
*
* @author Stephane Nicoll
*/
public class DependencyFilterMojoTests {
@ -41,19 +41,16 @@ public class DependencyFilterMojoTests {
@Test
public void filterDependencies() throws MojoExecutionException {
TestableDependencyFilterMojo mojo = new TestableDependencyFilterMojo(
Collections.<Exclude>emptyList(), "com.foo", "exclude-id");
Collections.<Exclude> emptyList(), "com.foo", "exclude-id");
Artifact artifact = createArtifact("com.bar", "one");
Set<Artifact> artifacts = mojo.filterDependencies(createArtifact("com.foo", "one"),
createArtifact("com.foo", "two"),
createArtifact("com.bar", "exclude-id"),
artifact);
Set<Artifact> artifacts = mojo.filterDependencies(
createArtifact("com.foo", "one"), createArtifact("com.foo", "two"),
createArtifact("com.bar", "exclude-id"), artifact);
assertEquals("wrong filtering of artifacts", 1, artifacts.size());
assertSame("Wrong filtered artifact", artifact, artifacts.iterator().next());
}
private Artifact createArtifact(String groupId, String artifactId) {
Artifact a = mock(Artifact.class);
given(a.getGroupId()).willReturn(groupId);
@ -61,20 +58,20 @@ public class DependencyFilterMojoTests {
return a;
}
private static class TestableDependencyFilterMojo extends
AbstractDependencyFilterMojo {
private static class TestableDependencyFilterMojo extends AbstractDependencyFilterMojo {
private TestableDependencyFilterMojo(List<Exclude> excludes, String excludeGroupIds, String excludeArtifactIds) {
private TestableDependencyFilterMojo(List<Exclude> excludes,
String excludeGroupIds, String excludeArtifactIds) {
setExcludes(excludes);
setExcludeGroupIds(excludeGroupIds);
setExcludeArtifactIds(excludeArtifactIds);
}
public Set<Artifact> filterDependencies(Artifact... artifacts) throws MojoExecutionException {
public Set<Artifact> filterDependencies(Artifact... artifacts)
throws MojoExecutionException {
Set<Artifact> input = new HashSet<Artifact>(Arrays.asList(artifacts));
FilterArtifacts filters = new FilterArtifacts();
initializeFilterArtifacts(filters);
return filterDependencies(input, filters);
return filterDependencies(input, getFilters());
}
@Override

View File

@ -16,10 +16,6 @@
package org.springframework.boot.maven;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import static org.mockito.Mockito.mock;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
@ -29,23 +25,32 @@ 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 ExcludeFilter}.
*
* @author Stephane Nicoll
*/
@SuppressWarnings("rawtypes")
public class ExcludeFilterTests {
@Test
public void excludeSimple() throws ArtifactFilterException {
ExcludeFilter filter = new ExcludeFilter(Arrays.asList(createExclude("com.foo", "bar")));
Set result = filter.filter(Collections.singleton(createArtifact("com.foo", "bar")));
ExcludeFilter filter = new ExcludeFilter(Arrays.asList(createExclude("com.foo",
"bar")));
Set result = filter.filter(Collections
.singleton(createArtifact("com.foo", "bar")));
assertEquals("Should have been filtered", 0, result.size());
}
@Test
public void excludeGroupIdNoMatch() throws ArtifactFilterException {
ExcludeFilter filter = new ExcludeFilter(Arrays.asList(createExclude("com.foo", "bar")));
ExcludeFilter filter = new ExcludeFilter(Arrays.asList(createExclude("com.foo",
"bar")));
Artifact artifact = createArtifact("com.baz", "bar");
Set result = filter.filter(Collections.singleton(artifact));
assertEquals("Should not have been filtered", 1, result.size());
@ -54,7 +59,8 @@ public class ExcludeFilterTests {
@Test
public void excludeArtifactIdNoMatch() throws ArtifactFilterException {
ExcludeFilter filter = new ExcludeFilter(Arrays.asList(createExclude("com.foo", "bar")));
ExcludeFilter filter = new ExcludeFilter(Arrays.asList(createExclude("com.foo",
"bar")));
Artifact artifact = createArtifact("com.foo", "biz");
Set result = filter.filter(Collections.singleton(artifact));
assertEquals("Should not have been filtered", 1, result.size());
@ -63,14 +69,17 @@ public class ExcludeFilterTests {
@Test
public void excludeClassifier() throws ArtifactFilterException {
ExcludeFilter filter = new ExcludeFilter(Arrays.asList(createExclude("com.foo", "bar", "jdk5")));
Set result = filter.filter(Collections.singleton(createArtifact("com.foo", "bar", "jdk5")));
ExcludeFilter filter = new ExcludeFilter(Arrays.asList(createExclude("com.foo",
"bar", "jdk5")));
Set result = filter.filter(Collections.singleton(createArtifact("com.foo", "bar",
"jdk5")));
assertEquals("Should have been filtered", 0, result.size());
}
@Test
public void excludeClassifierNoTargetClassifier() throws ArtifactFilterException {
ExcludeFilter filter = new ExcludeFilter(Arrays.asList(createExclude("com.foo", "bar", "jdk5")));
ExcludeFilter filter = new ExcludeFilter(Arrays.asList(createExclude("com.foo",
"bar", "jdk5")));
Artifact artifact = createArtifact("com.foo", "bar");
Set result = filter.filter(Collections.singleton(artifact));
assertEquals("Should not have been filtered", 1, result.size());
@ -79,7 +88,8 @@ public class ExcludeFilterTests {
@Test
public void excludeClassifierNoMatch() throws ArtifactFilterException {
ExcludeFilter filter = new ExcludeFilter(Arrays.asList(createExclude("com.foo", "bar", "jdk5")));
ExcludeFilter filter = new ExcludeFilter(Arrays.asList(createExclude("com.foo",
"bar", "jdk5")));
Artifact artifact = createArtifact("com.foo", "bar", "jdk6");
Set result = filter.filter(Collections.singleton(artifact));
assertEquals("Should not have been filtered", 1, result.size());
@ -89,8 +99,7 @@ public class ExcludeFilterTests {
@Test
public void excludeMulti() throws ArtifactFilterException {
ExcludeFilter filter = new ExcludeFilter(Arrays.asList(
createExclude("com.foo", "bar"),
createExclude("com.foo", "bar2"),
createExclude("com.foo", "bar"), createExclude("com.foo", "bar2"),
createExclude("org.acme", "app")));
Set<Artifact> artifacts = new HashSet<Artifact>();
artifacts.add(createArtifact("com.foo", "bar"));
@ -102,7 +111,6 @@ public class ExcludeFilterTests {
assertSame(anotherAcme, result.iterator().next());
}
private Exclude createExclude(String groupId, String artifactId, String classifier) {
Exclude e = new Exclude();
e.setGroupId(groupId);

View File

@ -1,44 +0,0 @@
/*
* 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 static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
*
* @author Stephane Nicoll
*/
public class RepackageMojoTests {
@Test
public void cleanConfigItemWithSpaces() {
assertEquals("foo,bar,biz", RepackageMojo.cleanConfigItem("foo, bar , biz"));
}
@Test
public void cleanNullConfigItemWith() {
assertEquals("", RepackageMojo.cleanConfigItem(null));
}
@Test
public void cleanEmptyConfigItemWith() {
assertEquals("", RepackageMojo.cleanConfigItem(""));
}
}

View File

@ -81,8 +81,8 @@ public class Verify {
public void assertHasNoEntryNameStartingWith(String entry) {
for (String name : this.content.keySet()) {
if (name.startsWith(entry)) {
throw new IllegalStateException("Entry starting with "
+ entry + " should not have been found");
throw new IllegalStateException("Entry starting with " + entry
+ " should not have been found");
}
}
}
@ -98,7 +98,7 @@ public class Verify {
public InputStream getEntryContent(String entry) throws IOException {
ZipEntry zipEntry = getEntry(entry);
if (zipEntry == null) {
throw new IllegalArgumentException("No entry with name ["+entry+"]");
throw new IllegalArgumentException("No entry with name [" + entry + "]");
}
return this.zipFile.getInputStream(zipEntry);
}
@ -127,13 +127,13 @@ public class Verify {
}
}
protected void verifyZipEntries(ArchiveVerifier verifier)
throws Exception {
protected void verifyZipEntries(ArchiveVerifier verifier) throws Exception {
verifyManifest(verifier);
}
private void verifyManifest(ArchiveVerifier verifier) throws Exception {
Manifest manifest = new Manifest(verifier.getEntryContent("META-INF/MANIFEST.MF"));
Manifest manifest = new Manifest(
verifier.getEntryContent("META-INF/MANIFEST.MF"));
verifyManifest(manifest);
}
@ -151,8 +151,7 @@ public class Verify {
}
@Override
protected void verifyZipEntries(ArchiveVerifier verifier)
throws Exception {
protected void verifyZipEntries(ArchiveVerifier verifier) throws Exception {
super.verifyZipEntries(verifier);
verifier.assertHasEntryNameStartingWith("lib/spring-context");
verifier.assertHasEntryNameStartingWith("lib/spring-core");
@ -179,13 +178,11 @@ public class Verify {
}
@Override
protected void verifyZipEntries(ArchiveVerifier verifier)
throws Exception {
protected void verifyZipEntries(ArchiveVerifier verifier) throws Exception {
super.verifyZipEntries(verifier);
verifier.assertHasEntryNameStartingWith("WEB-INF/lib/spring-context");
verifier.assertHasEntryNameStartingWith("WEB-INF/lib/spring-core");
verifier.assertHasEntryNameStartingWith(
"WEB-INF/lib-provided/javax.servlet-api-3.0.1.jar");
verifier.assertHasEntryNameStartingWith("WEB-INF/lib-provided/javax.servlet-api-3.0.1.jar");
assertTrue("Unpacked launcher classes", verifier.hasEntry("org/"
+ "springframework/boot/loader/JarLauncher.class"));
assertTrue("Own classes", verifier.hasEntry("WEB-INF/classes/org/"