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

io.streamnative.dependencies.BanRootManagedDependencies Maven / Gradle / Ivy

There is a newer version: 1.4
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 io.streamnative.dependencies;

import static org.apache.maven.artifact.Artifact.SCOPE_PROVIDED;
import static org.apache.maven.artifact.Artifact.SCOPE_TEST;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.maven.enforcer.rule.api.AbstractEnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;

@Named("banRootManagedDependencies")
public final class BanRootManagedDependencies extends AbstractEnforcerRule {
    private final ResolverUtil resolverUtil;

    private static final String ARTIFACT_SPLITTER = ":";

    private static final String PREMANAGED_VERSION = "premanaged.version";

    /**
     * Scope to exclude.
     */
    private List excludedScopes = Arrays.asList(SCOPE_TEST, SCOPE_PROVIDED);

    private List includes;

    private Set rootManagedDependencyInfos = new LinkedHashSet<>();

    @Inject
    public BanRootManagedDependencies(ResolverUtil resolverUtil) {
        this.resolverUtil = Objects.requireNonNull(resolverUtil);
    }

    @Override
    public void execute() throws EnforcerRuleException {
        DependencyNode node = resolverUtil.resolveTransitiveDependenciesVerbose(excludedScopes);
        getLog().debug(() -> resolverUtil.dumpTree(node));

        LinkedList toTraverseDependencies = new LinkedList<>();
        LinkedList toCompareDependencies = new LinkedList<>();
        Set toCompareScopes = new LinkedHashSet<>();

        for (DependencyNode dependencyNode : node.getChildren()) {
            boolean isInclude = false;
            for (String include : includes) {
                String groupIdRegex = include.split(ARTIFACT_SPLITTER)[0];
                String artifactIdRegex = include.split(ARTIFACT_SPLITTER)[1];
                if (dependencyNode.getArtifact().getGroupId().matches(groupIdRegex)
                        && dependencyNode.getArtifact().getArtifactId().matches(artifactIdRegex)) {
                    isInclude = true;
                    toTraverseDependencies.add(dependencyNode);
                }
            }
            if (!isInclude) {
                toCompareDependencies.add(dependencyNode);
            }
        }

        while (!toCompareDependencies.isEmpty()) {
            DependencyNode dependency = toCompareDependencies.poll();
            if (dependency != null) {
                toCompareScopes.add(
                        dependency.getArtifact().getGroupId() + ":" + dependency.getArtifact().getArtifactId());
                if (dependency.getChildren() != null) {
                    toCompareDependencies.addAll(dependency.getChildren());
                }
            }
        }

        while (!toTraverseDependencies.isEmpty()) {
            DependencyNode dependency = toTraverseDependencies.poll();
            if (DependencyManagerUtils.getPremanagedVersion(dependency) != null) {
                if (toCompareScopes.contains(
                        dependency.getArtifact().getGroupId() + ":" + dependency.getArtifact().getArtifactId())) {
                    String rootManagedDependencyInfo = dependency.getArtifact().getGroupId()
                            + ARTIFACT_SPLITTER
                            + dependency.getArtifact().getArtifactId()
                            + ARTIFACT_SPLITTER
                            + dependency.getArtifact().getVersion()
                            + "(version managed "
                            + dependency.getData().get(PREMANAGED_VERSION)
                            + ")";
                    rootManagedDependencyInfos.add(rootManagedDependencyInfo);
                }
            }
            if (dependency.getChildren() != null) {
                toTraverseDependencies.addAll(dependency.getChildren());
            }
        }

        if (!rootManagedDependencyInfos.isEmpty()) {
            StringBuilder errorMsg = new StringBuilder();
            for (String info : rootManagedDependencyInfos) {
                errorMsg.append(info).append("\n");
            }
            throw new EnforcerRuleException(errorMsg.toString());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy