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;
026import java.util.function.Predicate;
027
028import org.apache.maven.artifact.Artifact;
029import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
030import org.apache.maven.enforcer.rules.utils.ArtifactUtils;
031import org.apache.maven.execution.MavenSession;
032
033import static java.util.Optional.ofNullable;
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    private Predicate<Artifact> shouldExclude;
054    private Predicate<Artifact> shouldInclude;
055
056    @Inject
057    public RequireReleaseDeps(MavenSession session, ResolverUtil resolverUtil) {
058        super(session, resolverUtil);
059    }
060
061    // Override parent to allow optional ignore of this rule.
062    @Override
063    public void execute() throws EnforcerRuleException {
064        boolean callSuper;
065        if (onlyWhenRelease) {
066            // only call super if this project is a release
067            callSuper = !getSession().getCurrentProject().getArtifact().isSnapshot();
068        } else {
069            callSuper = true;
070        }
071
072        if (callSuper) {
073            shouldExclude = ArtifactUtils.prepareDependencyArtifactMatcher(getExcludes());
074            shouldInclude = ArtifactUtils.prepareDependencyArtifactMatcher(getIncludes());
075            super.execute();
076            if (failWhenParentIsSnapshot) {
077
078                Artifact parentArtifact = getSession().getCurrentProject().getParentArtifact();
079
080                if (parentArtifact != null) {
081                    Set<Artifact> singletonArtifact = new HashSet<>();
082                    singletonArtifact.add(parentArtifact);
083                    Set<Artifact> artifacts = filterArtifacts(singletonArtifact);
084                    parentArtifact = ofNullable(artifacts)
085                            .flatMap(s -> s.stream().findFirst())
086                            .orElse(null);
087                }
088
089                if (parentArtifact != null && parentArtifact.isSnapshot()) {
090                    throw new EnforcerRuleException("Parent Cannot be a snapshot: " + parentArtifact.getId());
091                }
092            }
093        }
094    }
095
096    @Override
097    protected String getErrorMessage() {
098        return "is not a release dependency";
099    }
100
101    @Override
102    protected boolean validate(Artifact artifact) {
103        // only check isSnapshot() if the artifact does not match (excludes minus includes)
104        // otherwise true
105        return shouldExclude.test(artifact) && !shouldInclude.test(artifact) || !artifact.isSnapshot();
106    }
107
108    /**
109     * Filter the dependency artifacts according to the includes and excludes
110     * If includes and excludes are both null, the original set is returned.
111     *
112     * @param dependencies the list of dependencies to filter
113     * @return the resulting set of dependencies
114     */
115    private Set<Artifact> filterArtifacts(Set<Artifact> dependencies) throws EnforcerRuleException {
116        if (getIncludes() != null) {
117            dependencies = ArtifactUtils.filterDependencyArtifacts(dependencies, getIncludes());
118        }
119
120        if (dependencies != null && getExcludes() != null) {
121            ofNullable(ArtifactUtils.filterDependencyArtifacts(dependencies, getExcludes()))
122                    .ifPresent(dependencies::removeAll);
123        }
124
125        return dependencies;
126    }
127
128    public void setOnlyWhenRelease(boolean onlyWhenRelease) {
129        this.onlyWhenRelease = onlyWhenRelease;
130    }
131
132    public void setFailWhenParentIsSnapshot(boolean failWhenParentIsSnapshot) {
133        this.failWhenParentIsSnapshot = failWhenParentIsSnapshot;
134    }
135
136    @Override
137    public String toString() {
138        return String.format(
139                "RequireReleaseDeps[message=%s, excludes=%s, includes=%s, searchTransitive=%b, onlyWhenRelease=%b, failWhenParentIsSnapshot=%b]",
140                getMessage(),
141                getExcludes(),
142                getIncludes(),
143                isSearchTransitive(),
144                onlyWhenRelease,
145                failWhenParentIsSnapshot);
146    }
147}