Filter non-jar artifacts when packaging libs in Gradle plugin

Previously, the Gradle plugin would package all of a project's
dependencies in the jar's lib directory, irrespective of each
dependency's type. This led to non-jar artifacts being packaged in
the lib directory where only jar dependencies are expected. See #334
for an example failure.

This commit updates the Gradle plugin such that it only packages
dependencies of type jar, ejb, ejb-client, test-jar, or bundle. This
brings the Gradle plugin into line with the Maven plugin.

Fixes #334.
This commit is contained in:
Andy Wilkinson 2014-02-10 15:51:29 +00:00
parent af93ae2dac
commit 4f677bec08

View File

@ -16,12 +16,15 @@
package org.springframework.boot.gradle.task;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.file.FileCollection;
import org.gradle.api.artifacts.ResolvedArtifact;
import org.springframework.boot.loader.tools.Libraries;
import org.springframework.boot.loader.tools.LibraryCallback;
import org.springframework.boot.loader.tools.LibraryScope;
@ -30,9 +33,14 @@ import org.springframework.boot.loader.tools.LibraryScope;
* Expose Gradle {@link Configuration}s as {@link Libraries}.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
class ProjectLibraries implements Libraries {
private static final Set<String> SUPPORTED_TYPES = Collections
.unmodifiableSet(new HashSet<String>(Arrays.asList("jar", "ejb",
"ejb-client", "test-jar", "bundle")));
private final Project project;
private String providedConfigurationName = "providedRuntime";
@ -64,41 +72,48 @@ class ProjectLibraries implements Libraries {
@Override
public void doWithLibraries(LibraryCallback callback) throws IOException {
FileCollection custom = this.customConfigurationName != null ? this.project
Configuration custom = this.customConfigurationName != null ? this.project
.getConfigurations().findByName(this.customConfigurationName) : null;
if (custom != null) {
libraries(LibraryScope.CUSTOM, custom, callback);
libraries(LibraryScope.CUSTOM, getResolvedArtifacts(custom), callback);
}
else {
FileCollection compile = this.project.getConfigurations()
.getByName("compile");
Set<ResolvedArtifact> compileArtifacts = getResolvedArtifacts("compile");
Set<ResolvedArtifact> runtimeArtifacts = getResolvedArtifacts("runtime");
runtimeArtifacts.removeAll(compileArtifacts);
FileCollection runtime = this.project.getConfigurations()
.getByName("runtime");
runtime = runtime.minus(compile);
Set<ResolvedArtifact> providedArtifacts = getResolvedArtifacts(this.providedConfigurationName);
compileArtifacts.removeAll(providedArtifacts);
runtimeArtifacts.removeAll(providedArtifacts);
FileCollection provided = this.project.getConfigurations().findByName(
this.providedConfigurationName);
if (provided != null) {
compile = compile.minus(provided);
runtime = runtime.minus(provided);
}
libraries(LibraryScope.COMPILE, compile, callback);
libraries(LibraryScope.RUNTIME, runtime, callback);
libraries(LibraryScope.PROVIDED, provided, callback);
libraries(LibraryScope.COMPILE, compileArtifacts, callback);
libraries(LibraryScope.RUNTIME, runtimeArtifacts, callback);
libraries(LibraryScope.PROVIDED, providedArtifacts, callback);
}
}
private void libraries(LibraryScope scope, FileCollection files,
private Set<ResolvedArtifact> getResolvedArtifacts(Configuration configuration) {
if (configuration != null) {
return configuration.getResolvedConfiguration().getResolvedArtifacts();
}
else {
return Collections.emptySet();
}
}
private Set<ResolvedArtifact> getResolvedArtifacts(String configurationName) {
Configuration configuration = this.project.getConfigurations().findByName(
configurationName);
return getResolvedArtifacts(configuration);
}
private void libraries(LibraryScope scope, Set<ResolvedArtifact> artifacts,
LibraryCallback callback) throws IOException {
if (files != null) {
for (File file : files) {
callback.library(file, scope);
for (ResolvedArtifact artifact : artifacts) {
if (SUPPORTED_TYPES.contains(artifact.getType())) {
callback.library(artifact.getFile(), scope);
}
}
}
}