Skip to content

Commit 7455e4e

Browse files
author
Phillip Webb
committed
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
1 parent aa38d33 commit 7455e4e

File tree

3 files changed

+83
-33
lines changed

3 files changed

+83
-33
lines changed

spring-boot-integration-tests/src/test/java/org/springframework/boot/gradle/RepackagingTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.util.jar.JarFile;
2122

2223
import org.gradle.tooling.ProjectConnection;
2324
import org.junit.BeforeClass;
2425
import org.junit.Test;
2526
import org.springframework.boot.dependency.tools.ManagedDependencies;
27+
import org.springframework.util.FileCopyUtils;
2628

29+
import static org.hamcrest.Matchers.notNullValue;
2730
import static org.junit.Assert.assertFalse;
31+
import static org.junit.Assert.assertThat;
2832
import static org.junit.Assert.assertTrue;
2933

3034
public class RepackagingTests {
@@ -111,4 +115,17 @@ public void repackagingEnableWithCustomRepackagedJarUsingStringJarTaskReference(
111115
assertTrue(new File(buildLibs, "custom.jar").exists());
112116
assertTrue(new File(buildLibs, "custom.jar.original").exists());
113117
}
118+
119+
@Test
120+
public void repackageWithFileDependency() throws Exception {
121+
FileCopyUtils.copy(new File("src/test/resources/foo.jar"), new File(
122+
"target/repackage/foo.jar"));
123+
project.newBuild().forTasks("clean", "build")
124+
.withArguments("-PbootVersion=" + BOOT_VERSION, "-Prepackage=true").run();
125+
File buildLibs = new File("target/repackage/build/libs");
126+
JarFile jarFile = new JarFile(new File(buildLibs, "repackage.jar"));
127+
assertThat(jarFile.getEntry("lib/foo.jar"), notNullValue());
128+
jarFile.close();
129+
}
130+
114131
}

spring-boot-integration-tests/src/test/resources/repackage.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ apply plugin: 'java'
1717

1818
dependencies {
1919
compile 'org.springframework.boot:spring-boot-starter-freemarker'
20-
compile "org.springframework.boot:spring-boot-starter-web"
20+
compile 'org.springframework.boot:spring-boot-starter-web'
21+
compile files("foo.jar")
2122
}
2223

2324
springBoot {
@@ -46,4 +47,4 @@ task customRepackagedJar(type: BootRepackage, dependsOn: customJar) {
4647

4748
task customRepackagedJarWithStringReference(type: BootRepackage, dependsOn: customJar) {
4849
withJarTask = 'customJar'
49-
}
50+
}

spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/ProjectLibraries.java

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import org.gradle.api.Project;
2626
import org.gradle.api.artifacts.Configuration;
27+
import org.gradle.api.artifacts.Dependency;
28+
import org.gradle.api.artifacts.FileCollectionDependency;
2729
import org.gradle.api.artifacts.ModuleVersionIdentifier;
2830
import org.gradle.api.artifacts.ResolvedArtifact;
2931
import org.springframework.boot.gradle.SpringBootPluginExtension;
@@ -72,70 +74,100 @@ public void setCustomConfigurationName(String customConfigurationName) {
7274

7375
@Override
7476
public void doWithLibraries(LibraryCallback callback) throws IOException {
75-
Set<ResolvedArtifact> custom = getArtifacts(this.customConfigurationName);
77+
Set<Library> custom = getLibraries(this.customConfigurationName,
78+
LibraryScope.CUSTOM);
7679
if (custom != null) {
77-
libraries(LibraryScope.CUSTOM, custom, callback);
80+
libraries(custom, callback);
7881
}
7982
else {
80-
Set<ResolvedArtifact> compile = getArtifacts("compile");
83+
Set<Library> compile = getLibraries("compile", LibraryScope.COMPILE);
8184

82-
Set<ResolvedArtifact> runtime = getArtifacts("runtime");
85+
Set<Library> runtime = getLibraries("runtime", LibraryScope.RUNTIME);
8386
runtime = minus(runtime, compile);
8487

85-
Set<ResolvedArtifact> provided = getArtifacts(this.providedConfigurationName);
88+
Set<Library> provided = getLibraries(this.providedConfigurationName,
89+
LibraryScope.PROVIDED);
8690
if (provided != null) {
8791
compile = minus(compile, provided);
8892
runtime = minus(runtime, provided);
8993
}
9094

91-
libraries(LibraryScope.COMPILE, compile, callback);
92-
libraries(LibraryScope.RUNTIME, runtime, callback);
93-
libraries(LibraryScope.PROVIDED, provided, callback);
95+
libraries(compile, callback);
96+
libraries(runtime, callback);
97+
libraries(provided, callback);
9498
}
9599
}
96100

97-
private Set<ResolvedArtifact> getArtifacts(String configurationName) {
101+
private Set<Library> getLibraries(String configurationName, LibraryScope scope) {
98102
Configuration configuration = (configurationName == null ? null : this.project
99103
.getConfigurations().findByName(configurationName));
100-
return (configuration == null ? null : configuration.getResolvedConfiguration()
101-
.getResolvedArtifacts());
104+
if (configuration == null) {
105+
return null;
106+
}
107+
Set<Library> libraries = new LinkedHashSet<Library>();
108+
for (ResolvedArtifact artifact : configuration.getResolvedConfiguration()
109+
.getResolvedArtifacts()) {
110+
libraries.add(new ResolvedArtifactLibrary(artifact, scope));
111+
}
112+
for (Dependency dependency : configuration.getIncoming().getDependencies()) {
113+
if (dependency instanceof FileCollectionDependency) {
114+
FileCollectionDependency fileDependency = (FileCollectionDependency) dependency;
115+
for (File file : fileDependency.resolve()) {
116+
libraries.add(new Library(file, scope));
117+
}
118+
}
119+
}
120+
return libraries;
102121
}
103122

104-
private Set<ResolvedArtifact> minus(Set<ResolvedArtifact> source,
105-
Set<ResolvedArtifact> toRemove) {
123+
private Set<Library> minus(Set<Library> source, Set<Library> toRemove) {
106124
if (source == null || toRemove == null) {
107125
return source;
108126
}
109127
Set<File> filesToRemove = new HashSet<File>();
110-
for (ResolvedArtifact artifact : toRemove) {
111-
filesToRemove.add(artifact.getFile());
128+
for (Library library : toRemove) {
129+
filesToRemove.add(library.getFile());
112130
}
113-
Set<ResolvedArtifact> result = new LinkedHashSet<ResolvedArtifact>();
114-
for (ResolvedArtifact artifact : source) {
115-
if (!filesToRemove.contains(artifact.getFile())) {
116-
result.add(artifact);
131+
Set<Library> result = new LinkedHashSet<Library>();
132+
for (Library library : source) {
133+
if (!filesToRemove.contains(library.getFile())) {
134+
result.add(library);
117135
}
118136
}
119137
return result;
120138
}
121139

122-
private void libraries(LibraryScope scope, Set<ResolvedArtifact> artifacts,
123-
LibraryCallback callback) throws IOException {
124-
if (artifacts != null) {
125-
for (ResolvedArtifact artifact : artifacts) {
126-
callback.library(new Library(artifact.getFile(), scope,
127-
isUnpackRequired(artifact)));
140+
private void libraries(Set<Library> libraries, LibraryCallback callback)
141+
throws IOException {
142+
if (libraries != null) {
143+
for (Library library : libraries) {
144+
callback.library(library);
128145
}
129146
}
130147
}
131148

132-
private boolean isUnpackRequired(ResolvedArtifact artifact) {
133-
if (this.extension.getRequiresUnpack() != null) {
134-
ModuleVersionIdentifier id = artifact.getModuleVersion().getId();
135-
return this.extension.getRequiresUnpack().contains(
136-
id.getGroup() + ":" + id.getName());
149+
/**
150+
* Adapts a {@link ResolvedArtifact} to a {@link Library}.
151+
*/
152+
private class ResolvedArtifactLibrary extends Library {
153+
154+
private final ResolvedArtifact artifact;
155+
156+
public ResolvedArtifactLibrary(ResolvedArtifact artifact, LibraryScope scope) {
157+
super(artifact.getFile(), scope);
158+
this.artifact = artifact;
159+
}
160+
161+
@Override
162+
public boolean isUnpackRequired() {
163+
if (ProjectLibraries.this.extension.getRequiresUnpack() != null) {
164+
ModuleVersionIdentifier id = artifact.getModuleVersion().getId();
165+
return ProjectLibraries.this.extension.getRequiresUnpack().contains(
166+
id.getGroup() + ":" + id.getName());
167+
}
168+
return false;
137169
}
138-
return false;
170+
139171
}
140172

141173
}

0 commit comments

Comments
 (0)