View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.enforcer.rules.dependency;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  
24  import java.util.HashSet;
25  import java.util.Set;
26  import java.util.function.Predicate;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
30  import org.apache.maven.enforcer.rules.utils.ArtifactUtils;
31  import org.apache.maven.execution.MavenSession;
32  
33  import static java.util.Optional.ofNullable;
34  
35  /**
36   * This rule checks that no snapshots are included.
37   *
38   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
39   */
40  @Named("requireReleaseDeps")
41  public final class RequireReleaseDeps extends BannedDependenciesBase {
42  
43      /**
44       * Allows this rule to execute only when this project is a release.
45       */
46      private boolean onlyWhenRelease = false;
47  
48      /**
49       * Allows this rule to fail when the parent is defined as a snapshot.
50       */
51      private boolean failWhenParentIsSnapshot = true;
52  
53      private Predicate<Artifact> shouldExclude;
54      private Predicate<Artifact> shouldInclude;
55  
56      @Inject
57      public RequireReleaseDeps(MavenSession session, ResolverUtil resolverUtil) {
58          super(session, resolverUtil);
59      }
60  
61      // Override parent to allow optional ignore of this rule.
62      @Override
63      public void execute() throws EnforcerRuleException {
64          boolean callSuper;
65          if (onlyWhenRelease) {
66              // only call super if this project is a release
67              callSuper = !getSession().getCurrentProject().getArtifact().isSnapshot();
68          } else {
69              callSuper = true;
70          }
71  
72          if (callSuper) {
73              shouldExclude = ArtifactUtils.prepareDependencyArtifactMatcher(getExcludes());
74              shouldInclude = ArtifactUtils.prepareDependencyArtifactMatcher(getIncludes());
75              super.execute();
76              if (failWhenParentIsSnapshot) {
77  
78                  Artifact parentArtifact = getSession().getCurrentProject().getParentArtifact();
79  
80                  if (parentArtifact != null) {
81                      Set<Artifact> singletonArtifact = new HashSet<>();
82                      singletonArtifact.add(parentArtifact);
83                      Set<Artifact> artifacts = filterArtifacts(singletonArtifact);
84                      parentArtifact = ofNullable(artifacts)
85                              .flatMap(s -> s.stream().findFirst())
86                              .orElse(null);
87                  }
88  
89                  if (parentArtifact != null && parentArtifact.isSnapshot()) {
90                      throw new EnforcerRuleException("Parent Cannot be a snapshot: " + parentArtifact.getId());
91                  }
92              }
93          }
94      }
95  
96      @Override
97      protected String getErrorMessage() {
98          return "is not a release dependency";
99      }
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 }