View Javadoc
1   package org.apache.maven.plugins.enforcer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
24  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
25  import org.apache.maven.project.MavenProject;
26  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
27  
28  /**
29   * This rule checks that the current project is not a release.
30   */
31  public class RequireSnapshotVersion
32      extends AbstractNonCacheableEnforcerRule
33  {
34  
35      /**
36       * Allows this rule to fail when the parent is defined as a release.
37       */
38      private boolean failWhenParentIsRelease = true;
39  
40      @Override
41      public void execute( EnforcerRuleHelper helper )
42          throws EnforcerRuleException
43      {
44  
45          MavenProject project = getProject( false, helper );
46          Artifact artifact = project.getArtifact();
47  
48          if ( !artifact.isSnapshot() )
49          {
50              String message = getMessage();
51              StringBuilder sb = new StringBuilder();
52              if ( message != null )
53              {
54                  sb.append( message ).append( System.lineSeparator() );
55              }
56              sb.append( "This project cannot be a release:" ).append( artifact.getId() );
57              throw new EnforcerRuleException( sb.toString() );
58          }
59          if ( failWhenParentIsRelease && project.hasParent() )
60          {
61              // project.getParentArtifact() does not work here if a "CI Friendly Version" is used (e.g. ${revision})
62              Artifact parentArtifact = getProject( true, helper ).getArtifact();
63              if ( parentArtifact != null && !parentArtifact.isSnapshot() )
64              {
65                  throw new EnforcerRuleException( "Parent cannot be a release: " + parentArtifact.getId() );
66              }
67          }
68  
69      }
70  
71      private MavenProject getProject( boolean parent, EnforcerRuleHelper helper )
72          throws EnforcerRuleException
73      {
74          String expression = parent ? "${project.parent}" : "${project}";
75          try
76          {
77              return (MavenProject) helper.evaluate( expression );
78          }
79          catch ( ExpressionEvaluationException eee )
80          {
81              throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee );
82          }
83      }
84  
85      public boolean isFailWhenParentIsRelease()
86      {
87          return failWhenParentIsRelease;
88      }
89  
90      public void setFailWhenParentIsRelease( boolean failWhenParentIsRelease )
91      {
92          this.failWhenParentIsRelease = failWhenParentIsRelease;
93      }
94  
95  }