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     * @see {@link #setFailWhenParentIsSnapshot(boolean)}
043     * @see {@link #isFailWhenParentIsSnapshot()}
044     */
045    private boolean failWhenParentIsSnapshot = true;
046
047    @Override
048    public void execute( EnforcerRuleHelper theHelper )
049        throws EnforcerRuleException
050    {
051
052        MavenProject project = getProject( theHelper );
053
054        if ( project.getArtifact().isSnapshot() )
055        {
056            String message = getMessage();
057            StringBuffer buf = new StringBuffer();
058            if ( message != null )
059            {
060                buf.append( message ).append( '\n' );
061            }
062            buf.append( "This project cannot be a snapshot:" ).append( project.getArtifact().getId() );
063            throw new EnforcerRuleException( buf.toString() );
064        }
065        if ( failWhenParentIsSnapshot )
066        {
067            Artifact parentArtifact = project.getParentArtifact();
068            if ( parentArtifact != null && parentArtifact.isSnapshot() )
069            {
070                throw new EnforcerRuleException( "Parent Cannot be a snapshot: " + parentArtifact.getId() );
071            }
072        }
073
074    }
075
076    /**
077     * @param helper
078     * @return The evaluated {@link MavenProject}.
079     * @throws EnforcerRuleException
080     */
081    private MavenProject getProject( EnforcerRuleHelper helper )
082        throws EnforcerRuleException
083    {
084        try
085        {
086            return (MavenProject) helper.evaluate( "${project}" );
087        }
088        catch ( ExpressionEvaluationException eee )
089        {
090            throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee );
091        }
092    }
093
094    public final boolean isFailWhenParentIsSnapshot()
095    {
096        return failWhenParentIsSnapshot;
097    }
098
099    public final void setFailWhenParentIsSnapshot( boolean failWhenParentIsSnapshot )
100    {
101        this.failWhenParentIsSnapshot = failWhenParentIsSnapshot;
102    }
103}