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;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  
24  import java.util.Objects;
25  import java.util.Optional;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
29  import org.apache.maven.project.MavenProject;
30  
31  /**
32   * This rule checks that the current project is not a release.
33   */
34  @Named("requireSnapshotVersion")
35  public final class RequireSnapshotVersion extends AbstractStandardEnforcerRule {
36  
37      /**
38       * Allows this rule to fail when the parent is defined as a release.
39       */
40      private boolean failWhenParentIsRelease = true;
41  
42      private final MavenProject project;
43  
44      @Inject
45      public RequireSnapshotVersion(MavenProject project) {
46          this.project = Objects.requireNonNull(project);
47      }
48  
49      @Override
50      public void execute() throws EnforcerRuleException {
51  
52          Artifact artifact = project.getArtifact();
53  
54          if (!artifact.isSnapshot()) {
55              String message = getMessage();
56              StringBuilder sb = new StringBuilder();
57              if (message != null) {
58                  sb.append(message).append(System.lineSeparator());
59              }
60              sb.append("This project cannot be a release:").append(artifact.getId());
61              throw new EnforcerRuleException(sb.toString());
62          }
63          if (failWhenParentIsRelease && project.hasParent()) {
64              Artifact parentArtifact = Optional.ofNullable(project.getParent())
65                      .map(MavenProject::getArtifact)
66                      .orElse(null);
67              if (parentArtifact != null && !parentArtifact.isSnapshot()) {
68                  throw new EnforcerRuleException("Parent cannot be a release: " + parentArtifact.getId());
69              }
70          }
71      }
72  
73      public void setFailWhenParentIsRelease(boolean failWhenParentIsRelease) {
74          this.failWhenParentIsRelease = failWhenParentIsRelease;
75      }
76  
77      @Override
78      public String toString() {
79          return String.format(
80                  "RequireSnapshotVersion[message=%s, failWhenParentIsRelease=%b]",
81                  getMessage(), failWhenParentIsRelease);
82      }
83  }