|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://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, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.gradle.classpath; |
| 18 | + |
| 19 | +import org.gradle.api.Plugin; |
| 20 | +import org.gradle.api.Project; |
| 21 | +import org.gradle.api.Task; |
| 22 | +import org.gradle.api.artifacts.Configuration; |
| 23 | +import org.gradle.api.artifacts.ConfigurationContainer; |
| 24 | +import org.gradle.api.plugins.JavaBasePlugin; |
| 25 | +import org.gradle.api.tasks.SourceSetContainer; |
| 26 | +import org.gradle.api.tasks.TaskProvider; |
| 27 | +import org.gradle.language.base.plugins.LifecycleBasePlugin; |
| 28 | +import org.springframework.util.StringUtils; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author Andy Wilkinson |
| 32 | + * @author Rob Winch |
| 33 | + */ |
| 34 | +public class CheckClasspathForProhibitedDependenciesPlugin implements Plugin<Project> { |
| 35 | + public static final String CHECK_PROHIBITED_DEPENDENCIES_TASK_NAME = "checkForProhibitedDependencies"; |
| 36 | + |
| 37 | + @Override |
| 38 | + public void apply(Project project) { |
| 39 | + project.getPlugins().withType(JavaBasePlugin.class, javaBasePlugin -> { |
| 40 | + configureProhibitedDependencyChecks(project); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + private void configureProhibitedDependencyChecks(Project project) { |
| 45 | + TaskProvider<Task> checkProhibitedDependencies = project.getTasks().register(CHECK_PROHIBITED_DEPENDENCIES_TASK_NAME, task -> { |
| 46 | + task.setGroup(JavaBasePlugin.VERIFICATION_GROUP); |
| 47 | + task.setDescription("Checks both the compile/runtime classpath of every SourceSet for prohibited dependencies"); |
| 48 | + }); |
| 49 | + project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME, checkTask -> { |
| 50 | + checkTask.dependsOn(checkProhibitedDependencies); |
| 51 | + }); |
| 52 | + SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class); |
| 53 | + sourceSets.all((sourceSet) -> createProhibitedDependenciesChecks(project, |
| 54 | + sourceSet.getCompileClasspathConfigurationName(), sourceSet.getRuntimeClasspathConfigurationName())); |
| 55 | + } |
| 56 | + |
| 57 | + private void createProhibitedDependenciesChecks(Project project, String... configurationNames) { |
| 58 | + ConfigurationContainer configurations = project.getConfigurations(); |
| 59 | + for (String configurationName : configurationNames) { |
| 60 | + Configuration configuration = configurations.getByName(configurationName); |
| 61 | + createProhibitedDependenciesCheck(configuration, project); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private void createProhibitedDependenciesCheck(Configuration classpath, Project project) { |
| 66 | + String taskName = "check" + StringUtils.capitalize(classpath.getName() + "ForProhibitedDependencies"); |
| 67 | + TaskProvider<CheckClasspathForProhibitedDependencies> checkClasspathTask = project.getTasks().register(taskName, |
| 68 | + CheckClasspathForProhibitedDependencies.class, checkClasspath -> { |
| 69 | + checkClasspath.setGroup(LifecycleBasePlugin.CHECK_TASK_NAME); |
| 70 | + checkClasspath.setDescription("Checks " + classpath.getName() + " for prohibited dependencies"); |
| 71 | + checkClasspath.setClasspath(classpath); |
| 72 | + }); |
| 73 | + project.getTasks().named(CHECK_PROHIBITED_DEPENDENCIES_TASK_NAME, checkProhibitedTask -> checkProhibitedTask.dependsOn(checkClasspathTask)); |
| 74 | + } |
| 75 | +} |
0 commit comments