diff --git a/spring-boot-docs/src/main/asciidoc/build-tool-plugins.adoc b/spring-boot-docs/src/main/asciidoc/build-tool-plugins.adoc index 00f44f2ea2..3619131612 100644 --- a/spring-boot-docs/src/main/asciidoc/build-tool-plugins.adoc +++ b/spring-boot-docs/src/main/asciidoc/build-tool-plugins.adoc @@ -619,7 +619,7 @@ Here is a typical example repackage: @Override public void doWithLibraries(LibraryCallback callback) throws IOException { // Build system specific implementation, callback for each dependency - // callback.library(nestedFile, LibraryScope.COMPILE); + // callback.library(new Library(nestedFile, LibraryScope.COMPILE)); } }); ---- 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 0f9caf9b65..17d783a09c 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 @@ -23,6 +23,7 @@ import org.gradle.api.Project; import org.gradle.api.artifacts.Configuration; import org.gradle.api.file.FileCollection; import org.springframework.boot.loader.tools.Libraries; +import org.springframework.boot.loader.tools.Library; import org.springframework.boot.loader.tools.LibraryCallback; import org.springframework.boot.loader.tools.LibraryScope; @@ -97,7 +98,7 @@ class ProjectLibraries implements Libraries { LibraryCallback callback) throws IOException { if (files != null) { for (File file: files) { - callback.library(file, scope); + callback.library(new Library(file, scope)); } } } diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackagePluginFeatures.java b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackagePluginFeatures.java index f42b1eb969..8d8079ae7a 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackagePluginFeatures.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/main/groovy/org/springframework/boot/gradle/repackage/RepackagePluginFeatures.java @@ -28,8 +28,8 @@ import org.gradle.api.plugins.BasePlugin; import org.gradle.api.tasks.bundling.Jar; import org.springframework.boot.gradle.PluginFeatures; import org.springframework.boot.gradle.SpringBootPluginExtension; +import org.springframework.boot.loader.tools.Library; import org.springframework.boot.loader.tools.LibraryCallback; -import org.springframework.boot.loader.tools.LibraryScope; import org.springframework.util.StringUtils; /** @@ -135,9 +135,8 @@ public class RepackagePluginFeatures implements PluginFeatures { private void addLibraryDependencies(final RepackageTask task) { try { task.getLibraries().doWithLibraries(new LibraryCallback() { - @Override - public void library(File file, LibraryScope scope) throws IOException { - task.getInputs().file(file); + public void library(Library library) throws IOException { + task.getInputs().file(library.getFile()); } }); } diff --git a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Library.java b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Library.java new file mode 100644 index 0000000000..584b2028cf --- /dev/null +++ b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Library.java @@ -0,0 +1,58 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.loader.tools; + +import java.io.File; + +/** + * Encapsulates information about a single library that may be packed into the archive. + * + * @author Phillip Webb + * @since 1.1.2 + * @see Libraries + */ +public class Library { + + private final File file; + + private final LibraryScope scope; + + /** + * Create a new {@link Library}. + * @param file the source file + * @param scope the scope of the library + */ + public Library(File file, LibraryScope scope) { + this.file = file; + this.scope = scope; + } + + /** + * @return the library file + */ + public File getFile() { + return this.file; + } + + /** + * @return the scope of the library + */ + public LibraryScope getScope() { + return this.scope; + } + +} diff --git a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/LibraryCallback.java b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/LibraryCallback.java index 7b2bdd4caa..1aeff158a6 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/LibraryCallback.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/LibraryCallback.java @@ -28,10 +28,9 @@ public interface LibraryCallback { /** * Callback to for a single library backed by a {@link File}. - * @param file the library file - * @param scope the scope of the library + * @param library the library * @throws IOException */ - void library(File file, LibraryScope scope) throws IOException; + void library(Library library) throws IOException; } diff --git a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java index 3f3a815e84..2b8c7855b7 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java @@ -141,10 +141,11 @@ public class Repackager { libraries.doWithLibraries(new LibraryCallback() { @Override - public void library(File file, LibraryScope scope) throws IOException { + public void library(Library library) throws IOException { + File file = library.getFile(); if (isZip(file)) { String destination = Repackager.this.layout - .getLibraryDestination(file.getName(), scope); + .getLibraryDestination(file.getName(), library.getScope()); if (destination != null) { writer.writeNestedLibrary(destination, file); } diff --git a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java index 49a42e502e..1fa28536a8 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java @@ -266,8 +266,8 @@ public class RepackagerTests { repackager.repackage(new Libraries() { @Override public void doWithLibraries(LibraryCallback callback) throws IOException { - callback.library(libJarFile, LibraryScope.COMPILE); - callback.library(libNonJarFile, LibraryScope.COMPILE); + callback.library(new Library(libJarFile, LibraryScope.COMPILE)); + callback.library(new Library(libNonJarFile, LibraryScope.COMPILE)); } }); assertThat(hasEntry(file, "lib/" + libJarFile.getName()), equalTo(true)); @@ -290,7 +290,7 @@ public class RepackagerTests { repackager.repackage(new Libraries() { @Override public void doWithLibraries(LibraryCallback callback) throws IOException { - callback.library(libJarFile, scope); + callback.library(new Library(libJarFile, scope)); } }); assertThat(hasEntry(file, "test/" + libJarFile.getName()), equalTo(true)); @@ -331,7 +331,7 @@ public class RepackagerTests { repackager.repackage(new Libraries() { @Override public void doWithLibraries(LibraryCallback callback) throws IOException { - callback.library(nestedFile, LibraryScope.COMPILE); + callback.library(new Library(nestedFile, LibraryScope.COMPILE)); } }); diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/ArtifactsLibraries.java b/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/ArtifactsLibraries.java index 2e672703e2..e32ea54d20 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/ArtifactsLibraries.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/ArtifactsLibraries.java @@ -24,6 +24,7 @@ import java.util.Set; import org.apache.maven.artifact.Artifact; import org.springframework.boot.loader.tools.Libraries; +import org.springframework.boot.loader.tools.Library; import org.springframework.boot.loader.tools.LibraryCallback; import org.springframework.boot.loader.tools.LibraryScope; @@ -55,7 +56,7 @@ public class ArtifactsLibraries implements Libraries { for (Artifact artifact : this.artifacts) { LibraryScope scope = SCOPES.get(artifact.getScope()); if (scope != null && artifact.getFile() != null) { - callback.library(artifact.getFile(), scope); + callback.library(new Library(artifact.getFile(), scope)); } } } diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ArtifactsLibrariesTest.java b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ArtifactsLibrariesTests.java similarity index 77% rename from spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ArtifactsLibrariesTest.java rename to spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ArtifactsLibrariesTests.java index 3faf8f3a48..9eaf7271d1 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ArtifactsLibrariesTest.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ArtifactsLibrariesTests.java @@ -23,11 +23,16 @@ import java.util.Set; import org.apache.maven.artifact.Artifact; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.springframework.boot.loader.tools.Library; import org.springframework.boot.loader.tools.LibraryCallback; import org.springframework.boot.loader.tools.LibraryScope; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.verify; @@ -36,7 +41,7 @@ import static org.mockito.Mockito.verify; * * @author Phillip Webb */ -public class ArtifactsLibrariesTest { +public class ArtifactsLibrariesTests { @Mock private Artifact artifact; @@ -50,6 +55,9 @@ public class ArtifactsLibrariesTest { @Mock private LibraryCallback callback; + @Captor + private ArgumentCaptor libraryCaptor; + @Before public void setup() { MockitoAnnotations.initMocks(this); @@ -63,6 +71,10 @@ public class ArtifactsLibrariesTest { given(this.artifact.getType()).willReturn("jar"); given(this.artifact.getScope()).willReturn("compile"); this.libs.doWithLibraries(this.callback); - verify(this.callback).library(this.file, LibraryScope.COMPILE); + verify(this.callback).library(this.libraryCaptor.capture()); + Library library = this.libraryCaptor.getValue(); + assertThat(library.getFile(), equalTo(this.file)); + assertThat(library.getScope(), equalTo(LibraryScope.COMPILE)); } + }