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  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
28  import org.apache.maven.project.MavenProject;
29  
30  /**
31   * This rule checks that the current project is not a snapshot.
32   *
33   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
34   */
35  @Named("requireReleaseVersion")
36  public final class RequireReleaseVersion extends AbstractStandardEnforcerRule {
37  
38      private final MavenProject project;
39  
40      /**
41       * Allows this rule to fail when the parent is defined as a snapshot.
42       */
43      private boolean failWhenParentIsSnapshot = true;
44  
45      @Inject
46      public RequireReleaseVersion(MavenProject project) {
47          this.project = Objects.requireNonNull(project);
48      }
49  
50      @Override
51      public void execute() throws EnforcerRuleException {
52  
53          if (project.getArtifact().isSnapshot()) {
54              String message = getMessage();
55              StringBuilder buf = new StringBuilder();
56              if (message != null) {
57                  buf.append(message).append(System.lineSeparator());
58              }
59              buf.append("This project cannot be a snapshot:")
60                      .append(project.getArtifact().getId());
61              throw new EnforcerRuleException(buf.toString());
62          }
63          if (failWhenParentIsSnapshot) {
64              Artifact parentArtifact = project.getParentArtifact();
65              if (parentArtifact != null && parentArtifact.isSnapshot()) {
66                  throw new EnforcerRuleException("Parent Cannot be a snapshot: " + parentArtifact.getId());
67              }
68          }
69      }
70  
71      public void setFailWhenParentIsSnapshot(boolean failWhenParentIsSnapshot) {
72          this.failWhenParentIsSnapshot = failWhenParentIsSnapshot;
73      }
74  
75      @Override
76      public String toString() {
77          return String.format("RequireReleaseVersion[failWhenParentIsSnapshot=%b]", failWhenParentIsSnapshot);
78      }
79  }