|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.tools.jib.gradle; |
| 18 | + |
| 19 | +import java.nio.file.Path; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.function.Consumer; |
| 24 | +import java.util.stream.Collectors; |
| 25 | +import org.gradle.api.Project; |
| 26 | +import org.gradle.api.artifacts.Configuration; |
| 27 | +import org.gradle.api.artifacts.ResolvedDependency; |
| 28 | +import org.gradle.api.tasks.SourceSet; |
| 29 | + |
| 30 | +public class GradleProjectDependencies { |
| 31 | + |
| 32 | + Map<String, Set<Path>> getDependencies(Project project, SourceSet sourceSet) { |
| 33 | + Map<String, Set<Path>> dependencyMap = new HashMap<>(); |
| 34 | + |
| 35 | + Consumer<ResolvedDependency> handleDep = |
| 36 | + resolvedDependency -> { |
| 37 | + Set<Path> artifactSet = |
| 38 | + resolvedDependency |
| 39 | + .getModuleArtifacts() |
| 40 | + .stream() |
| 41 | + .map(resolvedArtifact -> resolvedArtifact.getFile().toPath()) |
| 42 | + .collect(Collectors.toSet()); |
| 43 | + dependencyMap.put(resolvedDependency.getName(), artifactSet); |
| 44 | + }; |
| 45 | + Consumer<ResolvedDependency> recursiveDependencyConsumer = |
| 46 | + resolvedDependency -> { |
| 47 | + handleDep.accept(resolvedDependency); |
| 48 | + resolvedDependency.getChildren().forEach(handleDep); |
| 49 | + }; |
| 50 | + |
| 51 | + Configuration runtimeClasspathConfiguration = |
| 52 | + project.getConfigurations().getByName(sourceSet.getRuntimeClasspathConfigurationName()); |
| 53 | + runtimeClasspathConfiguration |
| 54 | + .getResolvedConfiguration() |
| 55 | + .getFirstLevelModuleDependencies() |
| 56 | + .forEach(recursiveDependencyConsumer); |
| 57 | + |
| 58 | + return dependencyMap; |
| 59 | + } |
| 60 | +} |
0 commit comments