com.liferay.gradle.plugins.dependency.checker.DependencyCheckerPlugin Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.liferay.gradle.plugins.dependency.checker Show documentation
Show all versions of com.liferay.gradle.plugins.dependency.checker Show documentation
The Dependency Checker Gradle plugin lets you warn users if a specific configuration dependency is not the latest one available from the Maven central repository. The plugin eventually fails the build if the dependency age (the difference between the timestamp of the current version and the latest version) is above a predetermined threshold.
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.gradle.plugins.dependency.checker;
import com.liferay.gradle.util.GradleUtil;
import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.ResolvableDependencies;
import org.gradle.api.artifacts.component.ComponentIdentifier;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
import org.gradle.api.artifacts.result.ResolutionResult;
import org.gradle.api.artifacts.result.ResolvedComponentResult;
/**
* @author Andrea Di Giorgi
*/
public class DependencyCheckerPlugin implements Plugin {
public static final String PLUGIN_NAME = "dependencyChecker";
@Override
public void apply(Project project) {
final DependencyCheckerExtension dependencyCheckerExtension =
GradleUtil.addExtension(
project, PLUGIN_NAME, DependencyCheckerExtension.class);
ConfigurationContainer configurationContainer =
project.getConfigurations();
configurationContainer.all(
new Action() {
@Override
public void execute(Configuration configuration) {
_configureConfiguration(
configuration, dependencyCheckerExtension);
}
});
}
private void _checkConfiguration(
Configuration configuration, ResolutionResult resolutionResult,
DependencyCheckerExtension dependencyCheckerExtension) {
for (ResolvedComponentResult resolvedComponentResult :
resolutionResult.getAllComponents()) {
ComponentIdentifier componentIdentifier =
resolvedComponentResult.getId();
if (componentIdentifier instanceof ModuleComponentIdentifier) {
ModuleComponentIdentifier moduleComponentIdentifier =
(ModuleComponentIdentifier)componentIdentifier;
dependencyCheckerExtension.check(
configuration.getName(),
moduleComponentIdentifier.getGroup(),
moduleComponentIdentifier.getModule(),
moduleComponentIdentifier.getVersion());
}
}
}
private void _configureConfiguration(
final Configuration configuration,
final DependencyCheckerExtension dependencyCheckerExtension) {
ResolvableDependencies resolvableDependencies =
configuration.getIncoming();
resolvableDependencies.afterResolve(
new Action() {
@Override
public void execute(
ResolvableDependencies resolvableDependencies) {
_checkConfiguration(
configuration,
resolvableDependencies.getResolutionResult(),
dependencyCheckerExtension);
}
});
}
}