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      @Override
48      public void execute( EnforcerRuleHelper theHelper )
49          throws EnforcerRuleException
50      {
51  
52          MavenProject project = getProject( theHelper );
53  
54          if ( project.getArtifact().isSnapshot() )
55          {
56              String message = getMessage();
57              StringBuffer buf = new StringBuffer();
58              if ( message != null )
59              {
60                  buf.append( message ).append( '\n' );
61              }
62              buf.append( "This project cannot be a snapshot:" ).append( project.getArtifact().getId() );
63              throw new EnforcerRuleException( buf.toString() );
64          }
65          if ( failWhenParentIsSnapshot )
66          {
67              Artifact parentArtifact = project.getParentArtifact();
68              if ( parentArtifact != null && parentArtifact.isSnapshot() )
69              {
70                  throw new EnforcerRuleException( "Parent Cannot be a snapshot: " + parentArtifact.getId() );
71              }
72          }
73  
74      }
75  
76      /**
77       * @param helper
78       * @return The evaluated {@link MavenProject}.
79       * @throws EnforcerRuleException
80       */
81      private MavenProject getProject( EnforcerRuleHelper helper )
82          throws EnforcerRuleException
83      {
84          try
85          {
86              return (MavenProject) helper.evaluate( "${project}" );
87          }
88          catch ( ExpressionEvaluationException eee )
89          {
90              throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee );
91          }
92      }
93  
94      public final boolean isFailWhenParentIsSnapshot()
95      {
96          return failWhenParentIsSnapshot;
97      }
98  
99      public final void setFailWhenParentIsSnapshot( boolean failWhenParentIsSnapshot )
100     {
101         this.failWhenParentIsSnapshot = failWhenParentIsSnapshot;
102     }
103 }