com.github.ferstl.maven.pomenforcers.PedanticPluginManagementOrderEnforcer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pedantic-pom-enforcers Show documentation
Show all versions of pedantic-pom-enforcers Show documentation
The Pedantic POM Enforcers consist of serveral Maven enforcer rules that help you keep your
project setup consistent and organized.
/*
* Copyright (c) 2012 - 2015 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.LinkedHashSet;
import java.util.Set;
import org.apache.maven.model.Plugin;
import org.apache.maven.project.MavenProject;
import com.github.ferstl.maven.pomenforcers.model.PluginElement;
import com.github.ferstl.maven.pomenforcers.model.PluginModel;
import com.github.ferstl.maven.pomenforcers.model.functions.PluginMatcher;
import com.github.ferstl.maven.pomenforcers.priority.CompoundPriorityOrdering;
import com.github.ferstl.maven.pomenforcers.util.CommaSeparatorUtils;
import com.github.ferstl.maven.pomenforcers.util.EnforcerRuleUtils;
import com.google.common.collect.BiMap;
import com.google.common.collect.Sets;
import static com.github.ferstl.maven.pomenforcers.model.PluginElement.ARTIFACT_ID;
import static com.github.ferstl.maven.pomenforcers.model.PluginElement.GROUP_ID;
import static com.github.ferstl.maven.pomenforcers.model.PluginElement.stringToPluginElement;
/**
* This enforcer makes sure that all plugins in your plugin management section
* are ordered. The ordering can be defined by any combination of groupId and
* artifactId. Each of these attributes may be given a priority.
*
* ### Example
* <rules>
* <pluginManagementOrder implementation="com.github.ferstl.maven.pomenforcers.PedanticPluginManagementOrderEnforcer">
* <!-- order by groupId and artifactId (default) -->
* <orderBy>groupId,artifactId</orderBy>
* <!-- all group IDs starting with com.myproject.plugins and com.myproject.testplugins should occur first -->
* <groupIdPriorities>com.myproject.plugins,com.myproject.testplugins</groupIdPriorities>
* <!-- all artifact IDs starting with mytest and myintegrationtest should occur first -->
* <artifactIdPriorities>mytest-,myintegrationtest-</artifactIdPriorities>
* </pluginManagementOrder>
* </rules>
*
* @id {@link PedanticEnforcerRule#PLUGIN_MANAGEMENT_ORDER}
* @since 1.0.0
*/
public class PedanticPluginManagementOrderEnforcer extends AbstractPedanticEnforcer {
private final CompoundPriorityOrdering pluginOrdering;
public PedanticPluginManagementOrderEnforcer() {
this.pluginOrdering = CompoundPriorityOrdering.orderBy(GROUP_ID, ARTIFACT_ID);
}
/**
* Comma-separated list of plugin elements that defines the ordering.
* @param pluginElements Comma-separated list of plugin elements that defines the ordering.
* @configParam
* @default groupId,artifactId
* @since 1.0.0
*/
public void setOrderBy(String pluginElements) {
Set orderBy = Sets.newLinkedHashSet();
CommaSeparatorUtils.splitAndAddToCollection(pluginElements, orderBy, stringToPluginElement());
this.pluginOrdering.redefineOrderBy(orderBy);
}
/**
* Comma-separated list of group IDs that should be listed first in the
* plugins declaration. All group IDs that start with
* any of the prioritized group IDs in the given list, are required to be
* located first in the dependencies section.
*
* @param groupIds Comma separated list of group IDs.
* @configParam
* @default n/a
* @since 1.0.0
*/
public void setGroupIdPriorities(String groupIds) {
LinkedHashSet groupIdPriorities = Sets.newLinkedHashSet();
CommaSeparatorUtils.splitAndAddToCollection(groupIds, groupIdPriorities);
this.pluginOrdering.setPriorities(PluginElement.GROUP_ID, groupIdPriorities);
}
/**
* Comma-separated list of artifact IDs that should be listed first in the
* plugins declaration. All artifact IDs that start with
* any of the prioritized IDs in the given list, are required to be located
* first in the dependencies section.
*
* @param artifactIds Comma separated list of artifact IDs.
* @configParam
* @default n/a
* @since 1.0.0
*/
public void setArtifactIdPriorities(String artifactIds) {
LinkedHashSet artifactIdPriorities = Sets.newLinkedHashSet();
CommaSeparatorUtils.splitAndAddToCollection(artifactIds, artifactIdPriorities);
this.pluginOrdering.setPriorities(PluginElement.ARTIFACT_ID, artifactIdPriorities);
}
@Override
protected PedanticEnforcerRule getDescription() {
return PedanticEnforcerRule.PLUGIN_MANAGEMENT_ORDER;
}
@Override
protected void accept(PedanticEnforcerVisitor visitor) {
visitor.visit(this);
}
@Override
protected void doEnforce(ErrorReport report) {
MavenProject project = EnforcerRuleUtils.getMavenProject(getHelper());
Collection declaredManagedPlugins = getProjectModel().getManagedPlugins();
Collection managedPlugins = project.getPluginManagement().getPlugins();
BiMap matchedPlugins = matchPlugins(declaredManagedPlugins, managedPlugins);
Set resolvedPlugins = matchedPlugins.keySet();
if (!this.pluginOrdering.isOrdered(resolvedPlugins)) {
Collection sortedPlugins = this.pluginOrdering.immutableSortedCopy(resolvedPlugins);
report.addLine("Your plugin management has to be ordered this way:")
.emptyLine()
.addDiffUsingToString(resolvedPlugins, sortedPlugins, "Actual Order", "Required Order");
}
}
private BiMap matchPlugins(Collection subset, Collection superset) {
return new PluginMatcher(getHelper()).match(superset, subset);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy