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
This commit is contained in:
Phillip Webb 2014-07-07 13:22:36 -07:00
parent aa38d33404
commit 7455e4e86f
3 changed files with 83 additions and 33 deletions

View File

@ -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();
}
}

View File

@ -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'
}
}

View File

@ -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<ResolvedArtifact> custom = getArtifacts(this.customConfigurationName);
Set<Library> custom = getLibraries(this.customConfigurationName,
LibraryScope.CUSTOM);
if (custom != null) {
libraries(LibraryScope.CUSTOM, custom, callback);
libraries(custom, callback);
}
else {
Set<ResolvedArtifact> compile = getArtifacts("compile");
Set<Library> compile = getLibraries("compile", LibraryScope.COMPILE);
Set<ResolvedArtifact> runtime = getArtifacts("runtime");
Set<Library> runtime = getLibraries("runtime", LibraryScope.RUNTIME);
runtime = minus(runtime, compile);
Set<ResolvedArtifact> provided = getArtifacts(this.providedConfigurationName);
Set<Library> 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<ResolvedArtifact> getArtifacts(String configurationName) {
private Set<Library> 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<Library> libraries = new LinkedHashSet<Library>();
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<ResolvedArtifact> minus(Set<ResolvedArtifact> source,
Set<ResolvedArtifact> toRemove) {
private Set<Library> minus(Set<Library> source, Set<Library> toRemove) {
if (source == null || toRemove == null) {
return source;
}
Set<File> filesToRemove = new HashSet<File>();
for (ResolvedArtifact artifact : toRemove) {
filesToRemove.add(artifact.getFile());
for (Library library : toRemove) {
filesToRemove.add(library.getFile());
}
Set<ResolvedArtifact> result = new LinkedHashSet<ResolvedArtifact>();
for (ResolvedArtifact artifact : source) {
if (!filesToRemove.contains(artifact.getFile())) {
result.add(artifact);
Set<Library> result = new LinkedHashSet<Library>();
for (Library library : source) {
if (!filesToRemove.contains(library.getFile())) {
result.add(library);
}
}
return result;
}
private void libraries(LibraryScope scope, Set<ResolvedArtifact> 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<Library> 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;
}
return false;
@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;
}
}
}