001package org.apache.maven.plugins.enforcer;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.artifact.Artifact;
023import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
024import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
025import org.apache.maven.project.MavenProject;
026import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
027
028/**
029 * This rule checks that the current project is not a snapshot.
030 *
031 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
032 */
033public class RequireReleaseVersion
034    extends AbstractNonCacheableEnforcerRule
035{
036
037    /**
038     * Allows this rule to fail when the parent is defined as a snapshot.
039     *
040     * @parameter
041     * 
042     * @deprecated the visibility will be reduced to private with the next major version
043     * @see {@link #setFailWhenParentIsSnapshot(boolean)}
044     * @see {@link #isFailWhenParentIsSnapshot()}
045     */
046    public boolean failWhenParentIsSnapshot = true;
047
048    /*
049     * (non-Javadoc)
050     *
051     * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper)
052     */
053    public void execute( EnforcerRuleHelper theHelper )
054        throws EnforcerRuleException
055    {
056
057        MavenProject project = getProject( theHelper );
058
059        if ( project.getArtifact().isSnapshot() )
060        {
061            String message = getMessage();
062            StringBuffer buf = new StringBuffer();
063            if ( message != null )
064            {
065                buf.append( message ).append( '\n' );
066            }
067            buf.append( "This project cannot be a snapshot:" ).append( project.getArtifact().getId() );
068            throw new EnforcerRuleException( buf.toString() );
069        }
070        if ( failWhenParentIsSnapshot )
071        {
072            Artifact parentArtifact = project.getParentArtifact();
073            if ( parentArtifact != null && parentArtifact.isSnapshot() )
074            {
075                throw new EnforcerRuleException( "Parent Cannot be a snapshot: " + parentArtifact.getId() );
076            }
077        }
078
079    }
080
081    /**
082     * @param helper
083     * @return
084     * @throws EnforcerRuleException
085     */
086    private MavenProject getProject( EnforcerRuleHelper helper )
087        throws EnforcerRuleException
088    {
089        try
090        {
091            return (MavenProject) helper.evaluate( "${project}" );
092        }
093        catch ( ExpressionEvaluationException eee )
094        {
095            throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee );
096        }
097    }
098
099    public final boolean isFailWhenParentIsSnapshot()
100    {
101        return failWhenParentIsSnapshot;
102    }
103
104    public final void setFailWhenParentIsSnapshot( boolean failWhenParentIsSnapshot )
105    {
106        this.failWhenParentIsSnapshot = failWhenParentIsSnapshot;
107    }
108}