Include transitive file dependencies during Gradle repackaging

Previously, ProjectLibraries only considered a configuration's
direct file dependencies. This meant that a transitive file
dependency that should have been pulled in via a project dependency
was not included in the repackaged jar's lib directory.

ProjectLibraries has been updated to walk down the tree of project
dependencies and create libraries for any file dependencies that
are found.

Fixes gh-1368
pull/1487/merge
Andy Wilkinson 10 years ago
parent c7045f5294
commit 69c61d0e8e

@ -0,0 +1,61 @@
/*
* 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.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.assertThat;
/**
* Integration tests for Gradle repackaging with a multi-project build.
*
* @author Andy Wilkinson
*/
public class MultiProjectRepackagingTests {
private static final String BOOT_VERSION = ManagedDependencies.get()
.find("spring-boot").getVersion();
private static ProjectConnection project;
@BeforeClass
public static void createProject() throws IOException {
project = new ProjectCreator().createProject("multi-project-repackage");
}
@Test
public void repackageWithTransitiveFileDependency() throws Exception {
FileCopyUtils.copy(new File("src/test/resources/foo.jar"), new File(
"target/multi-project-repackage/foo.jar"));
project.newBuild().forTasks("clean", "build")
.withArguments("-PbootVersion=" + BOOT_VERSION, "-Prepackage=true").run();
File buildLibs = new File("target/multi-project-repackage/main/build/libs");
JarFile jarFile = new JarFile(new File(buildLibs, "main.jar"));
assertThat(jarFile.getEntry("lib/commons-logging-1.1.3.jar"), notNullValue());
assertThat(jarFile.getEntry("lib/foo.jar"), notNullValue());
jarFile.close();
}
}

@ -23,6 +23,7 @@ import org.gradle.tooling.GradleConnector;
import org.gradle.tooling.ProjectConnection;
import org.gradle.tooling.internal.consumer.DefaultGradleConnector;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.FileSystemUtils;
/**
* @author Andy Wilkinson
@ -34,8 +35,15 @@ public class ProjectCreator {
projectDirectory.mkdirs();
File gradleScript = new File(projectDirectory, "build.gradle");
FileCopyUtils.copy(new File("src/test/resources/" + name + ".gradle"),
gradleScript);
if (new File("src/test/resources", name).isDirectory()) {
FileSystemUtils.copyRecursively(new File("src/test/resources", name),
projectDirectory);
}
else {
FileCopyUtils.copy(new File("src/test/resources/" + name + ".gradle"),
gradleScript);
}
GradleConnector gradleConnector = GradleConnector.newConnector();
((DefaultGradleConnector) gradleConnector).embedded(true);

@ -132,5 +132,4 @@ public class RepackagingTests {
assertThat(jarFile.getEntry("lib/foo.jar"), notNullValue());
jarFile.close();
}
}

@ -0,0 +1,40 @@
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${project.bootVersion}"
}
}
project('main') {
apply plugin: 'spring-boot'
apply plugin: 'java'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile project(':common')
}
springBoot {
mainClass = 'foo.bar.Baz'
}
}
project('common') {
apply plugin: 'java'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile "commons-logging:commons-logging:1.1.3"
compile files { "lib/foo.jar" }
}
}

@ -27,6 +27,7 @@ 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.ProjectDependency;
import org.gradle.api.artifacts.ResolvedArtifact;
import org.springframework.boot.gradle.SpringBootPluginExtension;
import org.springframework.boot.loader.tools.Libraries;
@ -109,6 +110,14 @@ class ProjectLibraries implements Libraries {
.getResolvedArtifacts()) {
libraries.add(new ResolvedArtifactLibrary(artifact, scope));
}
libraries.addAll(getLibrariesForFileDependencies(configuration, scope));
return libraries;
}
private Set<Library> getLibrariesForFileDependencies(Configuration configuration,
LibraryScope scope) {
Set<Library> libraries = new LinkedHashSet<Library>();
for (Dependency dependency : configuration.getIncoming().getDependencies()) {
if (dependency instanceof FileCollectionDependency) {
FileCollectionDependency fileDependency = (FileCollectionDependency) dependency;
@ -116,6 +125,11 @@ class ProjectLibraries implements Libraries {
libraries.add(new Library(file, scope));
}
}
else if (dependency instanceof ProjectDependency) {
ProjectDependency projectDependency = (ProjectDependency) dependency;
libraries.addAll(getLibrariesForFileDependencies(
projectDependency.getProjectConfiguration(), scope));
}
}
return libraries;
}
@ -161,7 +175,7 @@ class ProjectLibraries implements Libraries {
@Override
public boolean isUnpackRequired() {
if (ProjectLibraries.this.extension.getRequiresUnpack() != null) {
ModuleVersionIdentifier id = artifact.getModuleVersion().getId();
ModuleVersionIdentifier id = this.artifact.getModuleVersion().getId();
return ProjectLibraries.this.extension.getRequiresUnpack().contains(
id.getGroup() + ":" + id.getName());
}

Loading…
Cancel
Save