Filter non 'jar' types from nested libs

Update the `ArtifactsLibraries` used by the maven plugin to filter
based on artifact types. This prevent `.pom` files from accidentally
being packaged in `/libs` and ultimately resulting in 'Unable to find
ZIP central directory records' errors.

Fixes gh-324
This commit is contained in:
Phillip Webb 2014-02-07 10:07:35 -08:00
parent 2d8f66e3c5
commit d4f5cf4496
2 changed files with 89 additions and 3 deletions

View File

@ -17,8 +17,10 @@
package org.springframework.boot.maven;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@ -34,6 +36,10 @@ import org.springframework.boot.loader.tools.LibraryScope;
*/
public class ArtifactsLibraries implements Libraries {
private static final Set<String> SUPPORTED_TYPES = Collections
.unmodifiableSet(new HashSet<String>(Arrays.asList("jar", "ejb",
"ejb-client", "test-jar", "bundle")));
private static final Map<String, LibraryScope> SCOPES;
static {
Map<String, LibraryScope> scopes = new HashMap<String, LibraryScope>();
@ -52,9 +58,11 @@ public class ArtifactsLibraries implements Libraries {
@Override
public void doWithLibraries(LibraryCallback callback) throws IOException {
for (Artifact artifact : this.artifacts) {
LibraryScope scope = SCOPES.get(artifact.getScope());
if (scope != null && artifact.getFile() != null) {
callback.library(artifact.getFile(), scope);
if (SUPPORTED_TYPES.contains(artifact.getType())) {
LibraryScope scope = SCOPES.get(artifact.getScope());
if (scope != null && artifact.getFile() != null) {
callback.library(artifact.getFile(), scope);
}
}
}
}

View File

@ -0,0 +1,78 @@
/*
* 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 java.io.File;
import java.util.Collections;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.loader.tools.LibraryCallback;
import org.springframework.boot.loader.tools.LibraryScope;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
/**
* Tests for {@link ArtifactsLibraries}.
*
* @author Phillip Webb
*/
public class ArtifactsLibrariesTest {
@Mock
private Artifact artifact;
private Set<Artifact> artifacts;
private File file = new File(".");
private ArtifactsLibraries libs;
@Mock
private LibraryCallback callback;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.artifacts = Collections.singleton(this.artifact);
this.libs = new ArtifactsLibraries(this.artifacts);
given(this.artifact.getFile()).willReturn(this.file);
}
@Test
public void callbackForJars() throws Exception {
given(this.artifact.getType()).willReturn("jar");
given(this.artifact.getScope()).willReturn("compile");
this.libs.doWithLibraries(this.callback);
verify(this.callback).library(this.file, LibraryScope.COMPILE);
}
@Test
public void doesNotIncludePoms() throws Exception {
given(this.artifact.getType()).willReturn("pom");
given(this.artifact.getScope()).willReturn("compile");
this.libs.doWithLibraries(this.callback);
verifyZeroInteractions(this.callback);
}
}