001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.enforcer.rules.dependency;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023
024import java.util.HashSet;
025import java.util.Set;
026
027import org.apache.maven.artifact.Artifact;
028import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
029import org.apache.maven.enforcer.rules.utils.ArtifactUtils;
030import org.apache.maven.execution.MavenSession;
031
032import static java.util.Optional.ofNullable;
033import static org.apache.maven.enforcer.rules.utils.ArtifactUtils.matchDependencyArtifact;
034
035/**
036 * This rule checks that no snapshots are included.
037 *
038 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
039 */
040@Named("requireReleaseDeps")
041public final class RequireReleaseDeps extends BannedDependenciesBase {
042
043    /**
044     * Allows this rule to execute only when this project is a release.
045     */
046    private boolean onlyWhenRelease = false;
047
048    /**
049     * Allows this rule to fail when the parent is defined as a snapshot.
050     */
051    private boolean failWhenParentIsSnapshot = true;
052
053    @Inject
054    public RequireReleaseDeps(MavenSession session, ResolverUtil resolverUtil) {
055        super(session, resolverUtil);
056    }
057
058    // Override parent to allow optional ignore of this rule.
059    @Override
060    public void execute() throws EnforcerRuleException {
061        boolean callSuper;
062        if (onlyWhenRelease) {
063            // only call super if this project is a release
064            callSuper = !getSession().getCurrentProject().getArtifact().isSnapshot();
065        } else {
066            callSuper = true;
067        }
068
069        if (callSuper) {
070            super.execute();
071            if (failWhenParentIsSnapshot) {
072
073                Artifact parentArtifact = getSession().getCurrentProject().getParentArtifact();
074
075                if (parentArtifact != null) {
076                    Set<Artifact> singletonArtifact = new HashSet<>();
077                    singletonArtifact.add(parentArtifact);
078                    Set<Artifact> artifacts = filterArtifacts(singletonArtifact);
079                    parentArtifact = ofNullable(artifacts)
080                            .flatMap(s -> s.stream().findFirst())
081                            .orElse(null);
082                }
083
084                if (parentArtifact != null && parentArtifact.isSnapshot()) {
085                    throw new EnforcerRuleException("Parent Cannot be a snapshot: " + parentArtifact.getId());
086                }
087            }
088        }
089    }
090
091    @Override
092    protected String getErrorMessage() {
093        return "is not a release dependency";
094    }
095
096    @Override
097    protected boolean validate(Artifact artifact) {
098        // only check isSnapshot() if the artifact does not match (excludes minus includes)
099        // otherwise true
100        return (matchDependencyArtifact(artifact, getExcludes()) && !matchDependencyArtifact(artifact, getIncludes()))
101                || !artifact.isSnapshot();
102    }
103
104    /**
105     * Filter the dependency artifacts according to the includes and excludes
106     * If includes and excludes are both null, the original set is returned.
107     *
108     * @param dependencies the list of dependencies to filter
109     * @return the resulting set of dependencies
110     */
111    private Set<Artifact> filterArtifacts(Set<Artifact> dependencies) throws EnforcerRuleException {
112        if (getIncludes() != null) {
113            dependencies = ArtifactUtils.filterDependencyArtifacts(dependencies, getIncludes());
114        }
115
116        if (dependencies != null && getExcludes() != null) {
117            ofNullable(ArtifactUtils.filterDependencyArtifacts(dependencies, getExcludes()))
118                    .ifPresent(dependencies::removeAll);
119        }
120
121        return dependencies;
122    }
123
124    public void setOnlyWhenRelease(boolean onlyWhenRelease) {
125        this.onlyWhenRelease = onlyWhenRelease;
126    }
127
128    public void setFailWhenParentIsSnapshot(boolean failWhenParentIsSnapshot) {
129        this.failWhenParentIsSnapshot = failWhenParentIsSnapshot;
130    }
131
132    @Override
133    public String toString() {
134        return String.format(
135                "RequireReleaseDeps[message=%s, excludes=%s, includes=%s, searchTransitive=%b, onlyWhenRelease=%b, failWhenParentIsSnapshot=%b]",
136                getMessage(),
137                getExcludes(),
138                getIncludes(),
139                isSearchTransitive(),
140                onlyWhenRelease,
141                failWhenParentIsSnapshot);
142    }
143}