All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ferstl.maven.pomenforcers.PedanticPluginConfigurationEnforcer Maven / Gradle / Ivy

Go to download

The Pedantic POM Enforcers consist of serveral Maven enforcer rules that help you keep your project setup consistent and organized.

There is a newer version: 2.2.0
Show newest version
/*
 * Copyright (c) 2013 by Stefan Ferstl 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.ferstl.maven.pomenforcers;

import java.util.Collection;
import java.util.List;

import com.github.ferstl.maven.pomenforcers.model.PluginModel;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;

import static com.github.ferstl.maven.pomenforcers.ErrorReport.toList;

/**
 * Enforces that plugin versions, configurations and dependencies are defined in the
 * <pluginManagement> section. Plugin <executions> can still
 * be configured in the <plugins> section if this enforcer is active.
 * 
 * ### Example
 *     <rules>
 *       <pluginConfiguration implementation="com.github.ferstl.maven.pomenforcers.PedanticPluginConfigurationEnforcer">
 *         <!-- all plugin versions have to be defined in plugin managment -->
 *         <manageVersions>true</manageVersions>
 *         <!-- plugin configuration (except execution configuration) has to be defined in plugin management. -->
 *         <manageConfigurations>true</anageConfigurations>
 *         <!-- plugin dependencies may be defined in the <plugins> section. -->
 *         <manageDependencies>false</manageDependencies>
 *       </pluginConfiguration>
 *     </rules>
 * 
* @id {@link PedanticEnforcerRule#PLUGIN_CONFIGURATION} */ public class PedanticPluginConfigurationEnforcer extends AbstractPedanticEnforcer { private boolean manageVersions = true; private boolean manageConfigurations = true; private boolean manageDependencies = true; /** * Enforces plugin versions to be defined in <pluginManagement>. * @param manageVersions Enforces plugin versions to be defined in <pluginManagement>. * @configParam * @default true */ public void setManageVersions(boolean manageVersions) { this.manageVersions = manageVersions; } /** * Enforces plugin configuration to be defined in <pluginManagement>. * @param manageConfigurations Enforces plugin configuration to be defined in <pluginManagement>. * @configParam * @default true */ public void setManageConfigurations(boolean manageConfigurations) { this.manageConfigurations = manageConfigurations; } /** * Enforces plugin dependencies to be defined in <pluginManagement>. * @param manageDependencies Enforces plugin <dependencies> to be defined in * <pluginManagement>. * @configParam * @default true */ public void setManageDependencies(boolean manageDependencies) { this.manageDependencies = manageDependencies; } @Override protected PedanticEnforcerRule getDescription() { return PedanticEnforcerRule.PLUGIN_CONFIGURATION; } @Override protected void accept(PedanticEnforcerVisitor visitor) { visitor.visit(this); } @Override protected void doEnforce(ErrorReport report) { if (this.manageVersions) { enforceManagedVersions(report); } if (this.manageConfigurations) { enforceManagedConfiguration(report); } if (this.manageDependencies) { enforceManagedDependencies(report); } } private void enforceManagedVersions(ErrorReport report) { Collection versionedPlugins = searchForPlugins(PluginPredicate.WITH_VERSION); if (!versionedPlugins.isEmpty()) { report.addLine("Plugin versions have to be declared in :") .addLine(toList(versionedPlugins)); } } private void enforceManagedConfiguration(ErrorReport report) { Collection configuredPlugins = searchForPlugins(PluginPredicate.WITH_CONFIGURATION); if (!configuredPlugins.isEmpty()) { report.addLine("Use to configure these plugins or configure them for a specific :") .addLine(toList(configuredPlugins)); } } private void enforceManagedDependencies(ErrorReport report) { Collection pluginsWithDependencies = searchForPlugins(PluginPredicate.WITH_DEPENDENCIES); if (!pluginsWithDependencies.isEmpty()) { report.addLine("Use to configure plugin dependencies:") .addLine(toList(pluginsWithDependencies)); } } private Collection searchForPlugins(Predicate predicate) { List plugins = getProjectModel().getPlugins(); return Collections2.filter(plugins, predicate); } static enum PluginPredicate implements Predicate { WITH_DEPENDENCIES { @Override public boolean apply(PluginModel input) { return !input.getDependencies().isEmpty(); } }, WITH_CONFIGURATION { @Override public boolean apply(PluginModel input) { return input.isConfigured(); } }, WITH_VERSION { @Override public boolean apply(PluginModel input) { return input.getVersion() != null; } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy