Remove recursion from files task
@@ -20,6 +20,8 @@ import com.google.common.base.Preconditions;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.util.ArrayDeque;
+import java.util.Deque;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.Nullable;
@@ -96,26 +98,36 @@ public class FilesTask extends DefaultTask {
}
/**
- * Recursive function for printing out a project's artifacts. Calls itself when it encounters a
- * project dependency.
+ * Collects a project's project dependencies, including all transitive project dependencies.
*
- * @param project the project to list the artifacts for
- * @param projectDependenciesResult the set of project dependencies encountered. When a project
- * dependency is encountered, it is added to this set.
+ * @param project the project to find the project dependencies for
+ * @return the set of project dependencies
*/
- private static void findProjectDependencies(
- Project project, Set<ProjectDependency> projectDependenciesResult) {
+ private static Set<ProjectDependency> findProjectDependencies(Project project) {
+ Set<ProjectDependency> projectDependencies = new HashSet<>();
+ Deque<Project> projects = new ArrayDeque<>();
+ projects.push(project);
+
+ while (!projects.isEmpty()) {
+ Project currentProject = projects.pop();
+
+ // Search through all dependencies
for (Configuration configuration :
- project.getConfigurations().getByName("runtime").getHierarchy()) {
+ currentProject.getConfigurations().getByName("runtime").getHierarchy()) {
for (Dependency dependency : configuration.getDependencies()) {
if (dependency instanceof ProjectDependency) {
- projectDependenciesResult.add((ProjectDependency) dependency);
- findProjectDependencies(
- ((ProjectDependency) dependency).getDependencyProject(), projectDependenciesResult);
+ // If this is a project dependency, save it
+ ProjectDependency projectDependency = (ProjectDependency) dependency;
+ if (!projectDependencies.contains(projectDependency)) {
+ projects.push(projectDependency.getDependencyProject());
+ projectDependencies.add(projectDependency);
}
}
}
}
+ }
+ return projectDependencies;
+ }
@Nullable private JibExtension jibExtension;
@@ -142,8 +154,7 @@ public class FilesTask extends DefaultTask {
}
// Find project dependencies
- Set<ProjectDependency> projectDependencies = new HashSet<>();
- findProjectDependencies(project, projectDependencies);
+ Set<ProjectDependency> projectDependencies = findProjectDependencies(project);
Set<File> projectDependencyJars = new HashSet<>();
for (ProjectDependency projectDependency : projectDependencies) {
