From 7455e4e86fa6ae8c4201ca62516b0e4e4e13d791 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 7 Jul 2014 13:22:36 -0700 Subject: [PATCH] Restore support for `files` gradle dependencies Allow `compile files("$rootDir/vendor/foo.jar")` style declarations with the jars repackaged from the gradle plugin. Fixes gh-1215 --- .../boot/gradle/RepackagingTests.java | 17 ++++ .../src/test/resources/repackage.gradle | 5 +- .../gradle/repackage/ProjectLibraries.java | 94 +++++++++++++------ 3 files changed, 83 insertions(+), 33 deletions(-) diff --git a/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/RepackagingTests.java b/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/RepackagingTests.java index 0a6cea36cf..7009a67f6a 100644 --- a/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/RepackagingTests.java +++ b/spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/RepackagingTests.java @@ -18,13 +18,17 @@ package org.springframework.boot.gradle; import java.io.File; import java.io.IOException; +import java.util.jar.JarFile; import org.gradle.tooling.ProjectConnection; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.boot.dependency.tools.ManagedDependencies; +import org.springframework.util.FileCopyUtils; +import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; public class RepackagingTests { @@ -111,4 +115,17 @@ public class RepackagingTests { assertTrue(new File(buildLibs, "custom.jar").exists()); assertTrue(new File(buildLibs, "custom.jar.original").exists()); } + + @Test + public void repackageWithFileDependency() throws Exception { + FileCopyUtils.copy(new File("src/test/resources/foo.jar"), new File( + "target/repackage/foo.jar")); + project.newBuild().forTasks("clean", "build") + .withArguments("-PbootVersion=" + BOOT_VERSION, "-Prepackage=true").run(); + File buildLibs = new File("target/repackage/build/libs"); + JarFile jarFile = new JarFile(new File(buildLibs, "repackage.jar")); + assertThat(jarFile.getEntry("lib/foo.jar"), notNullValue()); + jarFile.close(); + } + } diff --git a/spring-boot-integration-tests/src/test/resources/repackage.gradle b/spring-boot-integration-tests/src/test/resources/repackage.gradle index f7ea1b3d22..439ef9e485 100644 --- a/spring-boot-integration-tests/src/test/resources/repackage.gradle +++ b/spring-boot-integration-tests/src/test/resources/repackage.gradle @@ -17,7 +17,8 @@ apply plugin: 'java' dependencies { compile 'org.springframework.boot:spring-boot-starter-freemarker' - compile "org.springframework.boot:spring-boot-starter-web" + compile 'org.springframework.boot:spring-boot-starter-web' + compile files("foo.jar") } springBoot { @@ -46,4 +47,4 @@ task customRepackagedJar(type: BootRepackage, dependsOn: customJar) { task customRepackagedJarWithStringReference(type: BootRepackage, dependsOn: customJar) { withJarTask = 'customJar' -} \ No newline at end of file +} diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/ProjectLibraries.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/ProjectLibraries.java index 216670aeb0..69e063633c 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/ProjectLibraries.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/ProjectLibraries.java @@ -24,6 +24,8 @@ import java.util.Set; import org.gradle.api.Project; import org.gradle.api.artifacts.Configuration; +import org.gradle.api.artifacts.Dependency; +import org.gradle.api.artifacts.FileCollectionDependency; import org.gradle.api.artifacts.ModuleVersionIdentifier; import org.gradle.api.artifacts.ResolvedArtifact; import org.springframework.boot.gradle.SpringBootPluginExtension; @@ -72,70 +74,100 @@ class ProjectLibraries implements Libraries { @Override public void doWithLibraries(LibraryCallback callback) throws IOException { - Set custom = getArtifacts(this.customConfigurationName); + Set custom = getLibraries(this.customConfigurationName, + LibraryScope.CUSTOM); if (custom != null) { - libraries(LibraryScope.CUSTOM, custom, callback); + libraries(custom, callback); } else { - Set compile = getArtifacts("compile"); + Set compile = getLibraries("compile", LibraryScope.COMPILE); - Set runtime = getArtifacts("runtime"); + Set runtime = getLibraries("runtime", LibraryScope.RUNTIME); runtime = minus(runtime, compile); - Set provided = getArtifacts(this.providedConfigurationName); + Set provided = getLibraries(this.providedConfigurationName, + LibraryScope.PROVIDED); if (provided != null) { compile = minus(compile, provided); runtime = minus(runtime, provided); } - libraries(LibraryScope.COMPILE, compile, callback); - libraries(LibraryScope.RUNTIME, runtime, callback); - libraries(LibraryScope.PROVIDED, provided, callback); + libraries(compile, callback); + libraries(runtime, callback); + libraries(provided, callback); } } - private Set getArtifacts(String configurationName) { + private Set getLibraries(String configurationName, LibraryScope scope) { Configuration configuration = (configurationName == null ? null : this.project .getConfigurations().findByName(configurationName)); - return (configuration == null ? null : configuration.getResolvedConfiguration() - .getResolvedArtifacts()); + if (configuration == null) { + return null; + } + Set libraries = new LinkedHashSet(); + for (ResolvedArtifact artifact : configuration.getResolvedConfiguration() + .getResolvedArtifacts()) { + libraries.add(new ResolvedArtifactLibrary(artifact, scope)); + } + for (Dependency dependency : configuration.getIncoming().getDependencies()) { + if (dependency instanceof FileCollectionDependency) { + FileCollectionDependency fileDependency = (FileCollectionDependency) dependency; + for (File file : fileDependency.resolve()) { + libraries.add(new Library(file, scope)); + } + } + } + return libraries; } - private Set minus(Set source, - Set toRemove) { + private Set minus(Set source, Set toRemove) { if (source == null || toRemove == null) { return source; } Set filesToRemove = new HashSet(); - for (ResolvedArtifact artifact : toRemove) { - filesToRemove.add(artifact.getFile()); + for (Library library : toRemove) { + filesToRemove.add(library.getFile()); } - Set result = new LinkedHashSet(); - for (ResolvedArtifact artifact : source) { - if (!filesToRemove.contains(artifact.getFile())) { - result.add(artifact); + Set result = new LinkedHashSet(); + for (Library library : source) { + if (!filesToRemove.contains(library.getFile())) { + result.add(library); } } return result; } - private void libraries(LibraryScope scope, Set artifacts, - LibraryCallback callback) throws IOException { - if (artifacts != null) { - for (ResolvedArtifact artifact : artifacts) { - callback.library(new Library(artifact.getFile(), scope, - isUnpackRequired(artifact))); + private void libraries(Set libraries, LibraryCallback callback) + throws IOException { + if (libraries != null) { + for (Library library : libraries) { + callback.library(library); } } } - private boolean isUnpackRequired(ResolvedArtifact artifact) { - if (this.extension.getRequiresUnpack() != null) { - ModuleVersionIdentifier id = artifact.getModuleVersion().getId(); - return this.extension.getRequiresUnpack().contains( - id.getGroup() + ":" + id.getName()); + /** + * Adapts a {@link ResolvedArtifact} to a {@link Library}. + */ + private class ResolvedArtifactLibrary extends Library { + + private final ResolvedArtifact artifact; + + public ResolvedArtifactLibrary(ResolvedArtifact artifact, LibraryScope scope) { + super(artifact.getFile(), scope); + this.artifact = artifact; + } + + @Override + public boolean isUnpackRequired() { + if (ProjectLibraries.this.extension.getRequiresUnpack() != null) { + ModuleVersionIdentifier id = artifact.getModuleVersion().getId(); + return ProjectLibraries.this.extension.getRequiresUnpack().contains( + id.getGroup() + ":" + id.getName()); + } + return false; } - return false; + } }