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 snapshot.
30   *
31   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
32   */
33  public class RequireReleaseVersion
34      extends AbstractNonCacheableEnforcerRule
35  {
36  
37      /**
38       * Allows this rule to fail when the parent is defined as a snapshot.
39       *
40       * @parameter
41       * 
42       * @see {@link #setFailWhenParentIsSnapshot(boolean)}
43       * @see {@link #isFailWhenParentIsSnapshot()}
44       */
45      private boolean failWhenParentIsSnapshot = true;
46  
47      /*
48       * (non-Javadoc)
49       *
50       * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper)
51       */
52      public void execute( EnforcerRuleHelper theHelper )
53          throws EnforcerRuleException
54      {
55  
56          MavenProject project = getProject( theHelper );
57  
58          if ( project.getArtifact().isSnapshot() )
59          {
60              String message = getMessage();
61              StringBuffer buf = new StringBuffer();
62              if ( message != null )
63              {
64                  buf.append( message ).append( '\n' );
65              }
66              buf.append( "This project cannot be a snapshot:" ).append( project.getArtifact().getId() );
67              throw new EnforcerRuleException( buf.toString() );
68          }
69          if ( failWhenParentIsSnapshot )
70          {
71              Artifact parentArtifact = project.getParentArtifact();
72              if ( parentArtifact != null && parentArtifact.isSnapshot() )
73              {
74                  throw new EnforcerRuleException( "Parent Cannot be a snapshot: " + parentArtifact.getId() );
75              }
76          }
77  
78      }
79  
80      /**
81       * @param helper
82       * @return
83       * @throws EnforcerRuleException
84       */
85      private MavenProject getProject( EnforcerRuleHelper helper )
86          throws EnforcerRuleException
87      {
88          try
89          {
90              return (MavenProject) helper.evaluate( "${project}" );
91          }
92          catch ( ExpressionEvaluationException eee )
93          {
94              throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee );
95          }
96      }
97  
98      public final boolean isFailWhenParentIsSnapshot()
99      {
100         return failWhenParentIsSnapshot;
101     }
102 
103     public final void setFailWhenParentIsSnapshot( boolean failWhenParentIsSnapshot )
104     {
105         this.failWhenParentIsSnapshot = failWhenParentIsSnapshot;
106     }
107 }