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
pull/1209/merge
Phillip Webb 11 years ago
parent aa38d33404
commit 7455e4e86f

@ -18,13 +18,17 @@ package org.springframework.boot.gradle;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.jar.JarFile;
import org.gradle.tooling.ProjectConnection; import org.gradle.tooling.ProjectConnection;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.dependency.tools.ManagedDependencies; 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.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class RepackagingTests { public class RepackagingTests {
@ -111,4 +115,17 @@ public class RepackagingTests {
assertTrue(new File(buildLibs, "custom.jar").exists()); assertTrue(new File(buildLibs, "custom.jar").exists());
assertTrue(new File(buildLibs, "custom.jar.original").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();
}
} }

@ -17,7 +17,8 @@ apply plugin: 'java'
dependencies { dependencies {
compile 'org.springframework.boot:spring-boot-starter-freemarker' 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 { springBoot {
@ -46,4 +47,4 @@ task customRepackagedJar(type: BootRepackage, dependsOn: customJar) {
task customRepackagedJarWithStringReference(type: BootRepackage, dependsOn: customJar) { task customRepackagedJarWithStringReference(type: BootRepackage, dependsOn: customJar) {
withJarTask = 'customJar' withJarTask = 'customJar'
} }

@ -24,6 +24,8 @@ 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.artifacts.Dependency;
import org.gradle.api.artifacts.FileCollectionDependency;
import org.gradle.api.artifacts.ModuleVersionIdentifier; import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.artifacts.ResolvedArtifact; import org.gradle.api.artifacts.ResolvedArtifact;
import org.springframework.boot.gradle.SpringBootPluginExtension; import org.springframework.boot.gradle.SpringBootPluginExtension;
@ -72,70 +74,100 @@ class ProjectLibraries implements Libraries {
@Override @Override
public void doWithLibraries(LibraryCallback callback) throws IOException { public void doWithLibraries(LibraryCallback callback) throws IOException {
Set<ResolvedArtifact> custom = getArtifacts(this.customConfigurationName); Set<Library> custom = getLibraries(this.customConfigurationName,
LibraryScope.CUSTOM);
if (custom != null) { if (custom != null) {
libraries(LibraryScope.CUSTOM, custom, callback); libraries(custom, callback);
} }
else { 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); runtime = minus(runtime, compile);
Set<ResolvedArtifact> provided = getArtifacts(this.providedConfigurationName); Set<Library> provided = getLibraries(this.providedConfigurationName,
LibraryScope.PROVIDED);
if (provided != null) { if (provided != null) {
compile = minus(compile, provided); compile = minus(compile, provided);
runtime = minus(runtime, provided); runtime = minus(runtime, provided);
} }
libraries(LibraryScope.COMPILE, compile, callback); libraries(compile, callback);
libraries(LibraryScope.RUNTIME, runtime, callback); libraries(runtime, callback);
libraries(LibraryScope.PROVIDED, provided, 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 Configuration configuration = (configurationName == null ? null : this.project
.getConfigurations().findByName(configurationName)); .getConfigurations().findByName(configurationName));
return (configuration == null ? null : configuration.getResolvedConfiguration() if (configuration == null) {
.getResolvedArtifacts()); 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, private Set<Library> minus(Set<Library> source, Set<Library> toRemove) {
Set<ResolvedArtifact> toRemove) {
if (source == null || toRemove == null) { if (source == null || toRemove == null) {
return source; return source;
} }
Set<File> filesToRemove = new HashSet<File>(); Set<File> filesToRemove = new HashSet<File>();
for (ResolvedArtifact artifact : toRemove) { for (Library library : toRemove) {
filesToRemove.add(artifact.getFile()); filesToRemove.add(library.getFile());
} }
Set<ResolvedArtifact> result = new LinkedHashSet<ResolvedArtifact>(); Set<Library> result = new LinkedHashSet<Library>();
for (ResolvedArtifact artifact : source) { for (Library library : source) {
if (!filesToRemove.contains(artifact.getFile())) { if (!filesToRemove.contains(library.getFile())) {
result.add(artifact); result.add(library);
} }
} }
return result; return result;
} }
private void libraries(LibraryScope scope, Set<ResolvedArtifact> artifacts, private void libraries(Set<Library> libraries, LibraryCallback callback)
LibraryCallback callback) throws IOException { throws IOException {
if (artifacts != null) { if (libraries != null) {
for (ResolvedArtifact artifact : artifacts) { for (Library library : libraries) {
callback.library(new Library(artifact.getFile(), scope, callback.library(library);
isUnpackRequired(artifact)));
} }
} }
} }
private boolean isUnpackRequired(ResolvedArtifact artifact) { /**
if (this.extension.getRequiresUnpack() != null) { * Adapts a {@link ResolvedArtifact} to a {@link Library}.
ModuleVersionIdentifier id = artifact.getModuleVersion().getId(); */
return this.extension.getRequiresUnpack().contains( private class ResolvedArtifactLibrary extends Library {
id.getGroup() + ":" + id.getName());
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;
} }
} }

Loading…
Cancel
Save