Skip to content

Commit 5f8fbfd

Browse files
author
Phillip Webb
committed
Add Library abstraction
Add a Library class update the LibraryCallback interface and implementations to use it. This change is in preparation for an addition `unpack` flag that will be required to allow the automatic unpacking of certain nested jars. See gh-1070
1 parent 3d6c8a8 commit 5f8fbfd

File tree

9 files changed

+89
-18
lines changed

9 files changed

+89
-18
lines changed

spring-boot-docs/src/main/asciidoc/build-tool-plugins.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ Here is a typical example repackage:
619619
@Override
620620
public void doWithLibraries(LibraryCallback callback) throws IOException {
621621
// Build system specific implementation, callback for each dependency
622-
// callback.library(nestedFile, LibraryScope.COMPILE);
622+
// callback.library(new Library(nestedFile, LibraryScope.COMPILE));
623623
}
624624
});
625625
----

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.gradle.api.artifacts.Configuration;
2424
import org.gradle.api.file.FileCollection;
2525
import org.springframework.boot.loader.tools.Libraries;
26+
import org.springframework.boot.loader.tools.Library;
2627
import org.springframework.boot.loader.tools.LibraryCallback;
2728
import org.springframework.boot.loader.tools.LibraryScope;
2829

@@ -97,7 +98,7 @@ private void libraries(LibraryScope scope, FileCollection files,
9798
LibraryCallback callback) throws IOException {
9899
if (files != null) {
99100
for (File file: files) {
100-
callback.library(file, scope);
101+
callback.library(new Library(file, scope));
101102
}
102103
}
103104
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import org.gradle.api.tasks.bundling.Jar;
2929
import org.springframework.boot.gradle.PluginFeatures;
3030
import org.springframework.boot.gradle.SpringBootPluginExtension;
31+
import org.springframework.boot.loader.tools.Library;
3132
import org.springframework.boot.loader.tools.LibraryCallback;
32-
import org.springframework.boot.loader.tools.LibraryScope;
3333
import org.springframework.util.StringUtils;
3434

3535
/**
@@ -135,9 +135,8 @@ private void setupInputOutputs(Jar jarTask, String classifier) {
135135
private void addLibraryDependencies(final RepackageTask task) {
136136
try {
137137
task.getLibraries().doWithLibraries(new LibraryCallback() {
138-
@Override
139-
public void library(File file, LibraryScope scope) throws IOException {
140-
task.getInputs().file(file);
138+
public void library(Library library) throws IOException {
139+
task.getInputs().file(library.getFile());
141140
}
142141
});
143142
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.loader.tools;
18+
19+
import java.io.File;
20+
21+
/**
22+
* Encapsulates information about a single library that may be packed into the archive.
23+
*
24+
* @author Phillip Webb
25+
* @since 1.1.2
26+
* @see Libraries
27+
*/
28+
public class Library {
29+
30+
private final File file;
31+
32+
private final LibraryScope scope;
33+
34+
/**
35+
* Create a new {@link Library}.
36+
* @param file the source file
37+
* @param scope the scope of the library
38+
*/
39+
public Library(File file, LibraryScope scope) {
40+
this.file = file;
41+
this.scope = scope;
42+
}
43+
44+
/**
45+
* @return the library file
46+
*/
47+
public File getFile() {
48+
return this.file;
49+
}
50+
51+
/**
52+
* @return the scope of the library
53+
*/
54+
public LibraryScope getScope() {
55+
return this.scope;
56+
}
57+
58+
}

spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/LibraryCallback.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ public interface LibraryCallback {
2828

2929
/**
3030
* Callback to for a single library backed by a {@link File}.
31-
* @param file the library file
32-
* @param scope the scope of the library
31+
* @param library the library
3332
* @throws IOException
3433
*/
35-
void library(File file, LibraryScope scope) throws IOException;
34+
void library(Library library) throws IOException;
3635

3736
}

spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,11 @@ private void repackage(JarFile sourceJar, File destination, Libraries libraries)
141141

142142
libraries.doWithLibraries(new LibraryCallback() {
143143
@Override
144-
public void library(File file, LibraryScope scope) throws IOException {
144+
public void library(Library library) throws IOException {
145+
File file = library.getFile();
145146
if (isZip(file)) {
146147
String destination = Repackager.this.layout
147-
.getLibraryDestination(file.getName(), scope);
148+
.getLibraryDestination(file.getName(), library.getScope());
148149
if (destination != null) {
149150
writer.writeNestedLibrary(destination, file);
150151
}

spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ public void libraries() throws Exception {
266266
repackager.repackage(new Libraries() {
267267
@Override
268268
public void doWithLibraries(LibraryCallback callback) throws IOException {
269-
callback.library(libJarFile, LibraryScope.COMPILE);
270-
callback.library(libNonJarFile, LibraryScope.COMPILE);
269+
callback.library(new Library(libJarFile, LibraryScope.COMPILE));
270+
callback.library(new Library(libNonJarFile, LibraryScope.COMPILE));
271271
}
272272
});
273273
assertThat(hasEntry(file, "lib/" + libJarFile.getName()), equalTo(true));
@@ -290,7 +290,7 @@ public void customLayout() throws Exception {
290290
repackager.repackage(new Libraries() {
291291
@Override
292292
public void doWithLibraries(LibraryCallback callback) throws IOException {
293-
callback.library(libJarFile, scope);
293+
callback.library(new Library(libJarFile, scope));
294294
}
295295
});
296296
assertThat(hasEntry(file, "test/" + libJarFile.getName()), equalTo(true));
@@ -331,7 +331,7 @@ public void dontRecompressZips() throws Exception {
331331
repackager.repackage(new Libraries() {
332332
@Override
333333
public void doWithLibraries(LibraryCallback callback) throws IOException {
334-
callback.library(nestedFile, LibraryScope.COMPILE);
334+
callback.library(new Library(nestedFile, LibraryScope.COMPILE));
335335
}
336336
});
337337

spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/ArtifactsLibraries.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.apache.maven.artifact.Artifact;
2626
import org.springframework.boot.loader.tools.Libraries;
27+
import org.springframework.boot.loader.tools.Library;
2728
import org.springframework.boot.loader.tools.LibraryCallback;
2829
import org.springframework.boot.loader.tools.LibraryScope;
2930

@@ -55,7 +56,7 @@ public void doWithLibraries(LibraryCallback callback) throws IOException {
5556
for (Artifact artifact : this.artifacts) {
5657
LibraryScope scope = SCOPES.get(artifact.getScope());
5758
if (scope != null && artifact.getFile() != null) {
58-
callback.library(artifact.getFile(), scope);
59+
callback.library(new Library(artifact.getFile(), scope));
5960
}
6061
}
6162
}
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@
2323
import org.apache.maven.artifact.Artifact;
2424
import org.junit.Before;
2525
import org.junit.Test;
26+
import org.mockito.ArgumentCaptor;
27+
import org.mockito.Captor;
2628
import org.mockito.Mock;
2729
import org.mockito.MockitoAnnotations;
30+
import org.springframework.boot.loader.tools.Library;
2831
import org.springframework.boot.loader.tools.LibraryCallback;
2932
import org.springframework.boot.loader.tools.LibraryScope;
3033

34+
import static org.hamcrest.Matchers.equalTo;
35+
import static org.junit.Assert.assertThat;
3136
import static org.mockito.BDDMockito.given;
3237
import static org.mockito.Mockito.verify;
3338

@@ -36,7 +41,7 @@
3641
*
3742
* @author Phillip Webb
3843
*/
39-
public class ArtifactsLibrariesTest {
44+
public class ArtifactsLibrariesTests {
4045

4146
@Mock
4247
private Artifact artifact;
@@ -50,6 +55,9 @@ public class ArtifactsLibrariesTest {
5055
@Mock
5156
private LibraryCallback callback;
5257

58+
@Captor
59+
private ArgumentCaptor<Library> libraryCaptor;
60+
5361
@Before
5462
public void setup() {
5563
MockitoAnnotations.initMocks(this);
@@ -63,6 +71,10 @@ public void callbackForJars() throws Exception {
6371
given(this.artifact.getType()).willReturn("jar");
6472
given(this.artifact.getScope()).willReturn("compile");
6573
this.libs.doWithLibraries(this.callback);
66-
verify(this.callback).library(this.file, LibraryScope.COMPILE);
74+
verify(this.callback).library(this.libraryCaptor.capture());
75+
Library library = this.libraryCaptor.getValue();
76+
assertThat(library.getFile(), equalTo(this.file));
77+
assertThat(library.getScope(), equalTo(LibraryScope.COMPILE));
6778
}
79+
6880
}

0 commit comments

Comments
 (0)