Java tutorial
/* * Copyright (c) 2012 - 2015 by Stefan Ferstl <st.ferstl@gmail.com> * * 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.Collections; import org.apache.maven.model.Dependency; import org.apache.maven.model.DependencyManagement; import org.apache.maven.project.MavenProject; import com.github.ferstl.maven.pomenforcers.model.DependencyModel; /** * This enforcer makes sure that all artifacts in your dependency management are * ordered. The ordering can be defined by any combination of <code>scope</code>, * <code>groupId</code> and <code>artifactId</code>. Each of these attributes * may be given a priority. * * <pre> * ### Example * <rules> * <dependencyManagementOrder implementation="com.github.ferstl.maven.pomenforcers.PedanticDependencyManagementOrderEnforcer"> * <!-- order by scope, groupId and artifactId (default) --> * <orderBy>scope,groupId,artifactId</orderBy> * <!-- runtime scope should occur before provided scope --> * <scopePriorities>compile,runtime,provided</scopePriorities> * <!-- all group IDs starting with com.myproject and com.mylibs should occur first --> * <groupIdPriorities>com.myproject,com.mylibs</groupIdPriorities> * <!-- all artifact IDs starting with commons- and utils- should occur first --> * <artifactIdPriorities>commons-,utils-</artifactIdPriorities> * </dependencyManagementOrder> * </rules> * </pre> * * @id {@link PedanticEnforcerRule#DEPENDENCY_MANAGEMENT_ORDER} * @since 1.0.0 */ public class PedanticDependencyManagementOrderEnforcer extends AbstractPedanticDependencyOrderEnforcer { @Override protected PedanticEnforcerRule getDescription() { return PedanticEnforcerRule.DEPENDENCY_MANAGEMENT_ORDER; } @Override protected void accept(PedanticEnforcerVisitor visitor) { visitor.visit(this); } @Override protected Collection<DependencyModel> getDeclaredDependencies() { return getProjectModel().getManagedDependencies(); } @Override protected Collection<Dependency> getMavenDependencies(MavenProject project) { DependencyManagement dependencyManagement = project.getDependencyManagement(); if (dependencyManagement != null) { return dependencyManagement.getDependencies(); } else { return Collections.emptyList(); } } @Override protected void reportError(ErrorReport report, Collection<DependencyModel> resolvedDependencies, Collection<DependencyModel> sortedDependencies) { report.addLine("Your dependency management has to be ordered this way:").emptyLine() .addDiffUsingToString(resolvedDependencies, sortedDependencies, "Actual Order", "Required Order"); } }