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( 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( '\n' );
55              }
56              sb.append( "This project cannot be a release:" ).append( artifact.getId() );
57              throw new EnforcerRuleException( sb.toString() );
58          }
59          if ( failWhenParentIsRelease )
60          {
61              Artifact parentArtifact = project.getParentArtifact();
62              if ( parentArtifact != null && !parentArtifact.isSnapshot() )
63              {
64                  throw new EnforcerRuleException( "Parent cannot be a release: " + parentArtifact.getId() );
65              }
66          }
67  
68      }
69  
70      private MavenProject getProject( EnforcerRuleHelper helper )
71          throws EnforcerRuleException
72      {
73          try
74          {
75              return (MavenProject) helper.evaluate( "${project}" );
76          }
77          catch ( ExpressionEvaluationException eee )
78          {
79              throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee );
80          }
81      }
82  
83      public boolean isFailWhenParentIsRelease()
84      {
85          return failWhenParentIsRelease;
86      }
87  
88      public void setFailWhenParentIsRelease( boolean failWhenParentIsRelease )
89      {
90          this.failWhenParentIsRelease = failWhenParentIsRelease;
91      }
92  
93  }