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