|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC. |
| 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 com.google.common.base.Preconditions; |
| 20 | +import java.io.File; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.Set; |
| 25 | +import javax.annotation.Nullable; |
| 26 | +import org.gradle.api.DefaultTask; |
| 27 | +import org.gradle.api.Project; |
| 28 | +import org.gradle.api.artifacts.Configuration; |
| 29 | +import org.gradle.api.artifacts.Dependency; |
| 30 | +import org.gradle.api.artifacts.ProjectDependency; |
| 31 | +import org.gradle.api.artifacts.PublishArtifact; |
| 32 | +import org.gradle.api.initialization.Settings; |
| 33 | +import org.gradle.api.plugins.JavaPluginConvention; |
| 34 | +import org.gradle.api.tasks.SourceSet; |
| 35 | +import org.gradle.api.tasks.TaskAction; |
| 36 | + |
| 37 | +/** |
| 38 | + * Print out changing source dependencies on a project. |
| 39 | + * |
| 40 | + * <p>Expected use: {@code ./gradlew _jibSkaffoldFiles -q} or {@code ./gradlew |
| 41 | + * :<subproject>:_jibSkaffoldFiles -q} |
| 42 | + */ |
| 43 | +public class FilesTask extends DefaultTask { |
| 44 | + |
| 45 | + /** |
| 46 | + * Prints the locations of a project's build.gradle, settings.gradle, and gradle.properties. |
| 47 | + * |
| 48 | + * @param project the project |
| 49 | + */ |
| 50 | + private static void printGradleFiles(Project project) { |
| 51 | + Path projectPath = project.getProjectDir().toPath(); |
| 52 | + |
| 53 | + // Print build.gradle |
| 54 | + System.out.println(project.getBuildFile()); |
| 55 | + |
| 56 | + // Print settings.gradle |
| 57 | + if (project.getGradle().getStartParameter().getSettingsFile() != null) { |
| 58 | + System.out.println(project.getGradle().getStartParameter().getSettingsFile()); |
| 59 | + } else if (Files.exists(projectPath.resolve(Settings.DEFAULT_SETTINGS_FILE))) { |
| 60 | + System.out.println(projectPath.resolve(Settings.DEFAULT_SETTINGS_FILE)); |
| 61 | + } |
| 62 | + |
| 63 | + // Print gradle.properties |
| 64 | + if (Files.exists(projectPath.resolve("gradle.properties"))) { |
| 65 | + System.out.println(projectPath.resolve("gradle.properties")); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Prints build files, sources, and resources associated with a project. |
| 71 | + * |
| 72 | + * @param project the project |
| 73 | + */ |
| 74 | + private static void printProjectFiles(Project project) { |
| 75 | + JavaPluginConvention javaConvention = |
| 76 | + project.getConvention().getPlugin(JavaPluginConvention.class); |
| 77 | + SourceSet mainSourceSet = |
| 78 | + javaConvention.getSourceSets().findByName(SourceSet.MAIN_SOURCE_SET_NAME); |
| 79 | + if (mainSourceSet == null) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + // Print build config, settings, etc. |
| 84 | + printGradleFiles(project); |
| 85 | + |
| 86 | + // Print sources + resources |
| 87 | + mainSourceSet |
| 88 | + .getAllSource() |
| 89 | + .getSourceDirectories() |
| 90 | + .forEach( |
| 91 | + sourceDirectory -> { |
| 92 | + if (sourceDirectory.exists()) { |
| 93 | + System.out.println(sourceDirectory); |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Recursive function for printing out a project's artifacts. Calls itself when it encounters a |
| 100 | + * project dependency. |
| 101 | + * |
| 102 | + * @param project the project to list the artifacts for |
| 103 | + * @param projectDependenciesResult the set of project dependencies encountered. When a project |
| 104 | + * dependency is encountered, it is added to this set. |
| 105 | + */ |
| 106 | + private static void findProjectDependencies( |
| 107 | + Project project, Set<ProjectDependency> projectDependenciesResult) { |
| 108 | + for (Configuration configuration : |
| 109 | + project.getConfigurations().getByName("runtime").getHierarchy()) { |
| 110 | + for (Dependency dependency : configuration.getDependencies()) { |
| 111 | + if (dependency instanceof ProjectDependency) { |
| 112 | + projectDependenciesResult.add((ProjectDependency) dependency); |
| 113 | + findProjectDependencies( |
| 114 | + ((ProjectDependency) dependency).getDependencyProject(), projectDependenciesResult); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Nullable private JibExtension jibExtension; |
| 121 | + |
| 122 | + public FilesTask setJibExtension(JibExtension jibExtension) { |
| 123 | + this.jibExtension = jibExtension; |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + @TaskAction |
| 128 | + public void listFiles() { |
| 129 | + Preconditions.checkNotNull(jibExtension); |
| 130 | + Project project = getProject(); |
| 131 | + |
| 132 | + // If this is not the root project, print the root project's build.gradle and settings.gradle |
| 133 | + if (project != project.getRootProject()) { |
| 134 | + printGradleFiles(project.getRootProject()); |
| 135 | + } |
| 136 | + |
| 137 | + printProjectFiles(project); |
| 138 | + |
| 139 | + // Print extra layer |
| 140 | + if (Files.exists(jibExtension.getExtraDirectoryPath())) { |
| 141 | + System.out.println(jibExtension.getExtraDirectoryPath()); |
| 142 | + } |
| 143 | + |
| 144 | + // Find project dependencies |
| 145 | + Set<ProjectDependency> projectDependencies = new HashSet<>(); |
| 146 | + findProjectDependencies(project, projectDependencies); |
| 147 | + |
| 148 | + Set<File> projectDependencyJars = new HashSet<>(); |
| 149 | + for (ProjectDependency projectDependency : projectDependencies) { |
| 150 | + printProjectFiles(projectDependency.getDependencyProject()); |
| 151 | + |
| 152 | + // Keep track of project dependency jars for filtering out later |
| 153 | + String configurationName = projectDependency.getTargetConfiguration(); |
| 154 | + if (configurationName == null) { |
| 155 | + configurationName = "default"; |
| 156 | + } |
| 157 | + Project dependencyProject = projectDependency.getDependencyProject(); |
| 158 | + for (Configuration targetConfiguration : |
| 159 | + dependencyProject.getConfigurations().getByName(configurationName).getHierarchy()) { |
| 160 | + for (PublishArtifact artifact : targetConfiguration.getArtifacts()) { |
| 161 | + projectDependencyJars.add(artifact.getFile()); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // Print out SNAPSHOT, non-project dependency jars |
| 167 | + for (File file : project.getConfigurations().getByName("runtime")) { |
| 168 | + if (!projectDependencyJars.contains(file) && file.toString().contains("SNAPSHOT")) { |
| 169 | + System.out.println(file); |
| 170 | + projectDependencyJars.add(file); // Add to set to avoid printing the same files twice |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments