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.
pull/336/head
Andy Wilkinson 11 years ago
parent af93ae2dac
commit 4f677bec08

@ -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");
FileCollection runtime = this.project.getConfigurations()
.getByName("runtime");
runtime = runtime.minus(compile);
FileCollection provided = this.project.getConfigurations().findByName(
this.providedConfigurationName);
if (provided != null) {
compile = compile.minus(provided);
runtime = runtime.minus(provided);
}
Set<ResolvedArtifact> compileArtifacts = getResolvedArtifacts("compile");
Set<ResolvedArtifact> runtimeArtifacts = getResolvedArtifacts("runtime");
runtimeArtifacts.removeAll(compileArtifacts);
Set<ResolvedArtifact> providedArtifacts = getResolvedArtifacts(this.providedConfigurationName);
compileArtifacts.removeAll(providedArtifacts);
runtimeArtifacts.removeAll(providedArtifacts);
libraries(LibraryScope.COMPILE, compileArtifacts, callback);
libraries(LibraryScope.RUNTIME, runtimeArtifacts, callback);
libraries(LibraryScope.PROVIDED, providedArtifacts, callback);
}
}
libraries(LibraryScope.COMPILE, compile, callback);
libraries(LibraryScope.RUNTIME, runtime, callback);
libraries(LibraryScope.PROVIDED, provided, callback);
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, FileCollection files,
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);
}
}
}
}

Loading…
Cancel
Save