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
1 changed files with 40 additions and 25 deletions

View File

@ -16,12 +16,15 @@
package org.springframework.boot.gradle.task; package org.springframework.boot.gradle.task;
import java.io.File;
import java.io.IOException; 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.Project;
import org.gradle.api.artifacts.Configuration; 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.Libraries;
import org.springframework.boot.loader.tools.LibraryCallback; import org.springframework.boot.loader.tools.LibraryCallback;
import org.springframework.boot.loader.tools.LibraryScope; 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}. * Expose Gradle {@link Configuration}s as {@link Libraries}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson
*/ */
class ProjectLibraries implements Libraries { 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 final Project project;
private String providedConfigurationName = "providedRuntime"; private String providedConfigurationName = "providedRuntime";
@ -64,41 +72,48 @@ class ProjectLibraries implements Libraries {
@Override @Override
public void doWithLibraries(LibraryCallback callback) throws IOException { 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; .getConfigurations().findByName(this.customConfigurationName) : null;
if (custom != null) { if (custom != null) {
libraries(LibraryScope.CUSTOM, custom, callback); libraries(LibraryScope.CUSTOM, getResolvedArtifacts(custom), callback);
} }
else { else {
FileCollection compile = this.project.getConfigurations() Set<ResolvedArtifact> compileArtifacts = getResolvedArtifacts("compile");
.getByName("compile"); Set<ResolvedArtifact> runtimeArtifacts = getResolvedArtifacts("runtime");
runtimeArtifacts.removeAll(compileArtifacts);
FileCollection runtime = this.project.getConfigurations() Set<ResolvedArtifact> providedArtifacts = getResolvedArtifacts(this.providedConfigurationName);
.getByName("runtime"); compileArtifacts.removeAll(providedArtifacts);
runtime = runtime.minus(compile); runtimeArtifacts.removeAll(providedArtifacts);
FileCollection provided = this.project.getConfigurations().findByName( libraries(LibraryScope.COMPILE, compileArtifacts, callback);
this.providedConfigurationName); libraries(LibraryScope.RUNTIME, runtimeArtifacts, callback);
if (provided != null) { libraries(LibraryScope.PROVIDED, providedArtifacts, callback);
compile = compile.minus(provided);
runtime = runtime.minus(provided);
}
libraries(LibraryScope.COMPILE, compile, callback);
libraries(LibraryScope.RUNTIME, runtime, callback);
libraries(LibraryScope.PROVIDED, provided, 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 { LibraryCallback callback) throws IOException {
if (files != null) { for (ResolvedArtifact artifact : artifacts) {
for (File file : files) { if (SUPPORTED_TYPES.contains(artifact.getType())) {
callback.library(file, scope); callback.library(artifact.getFile(), scope);
} }
} }
} }
} }