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 release.
030 */
031public class RequireSnapshotVersion
032    extends AbstractNonCacheableEnforcerRule
033{
034
035    /**
036     * Allows this rule to fail when the parent is defined as a release.
037     */
038    private boolean failWhenParentIsRelease = true;
039
040    @Override
041    public void execute( EnforcerRuleHelper helper )
042        throws EnforcerRuleException
043    {
044
045        MavenProject project = getProject( false, helper );
046        Artifact artifact = project.getArtifact();
047
048        if ( !artifact.isSnapshot() )
049        {
050            String message = getMessage();
051            StringBuilder sb = new StringBuilder();
052            if ( message != null )
053            {
054                sb.append( message ).append( System.lineSeparator() );
055            }
056            sb.append( "This project cannot be a release:" ).append( artifact.getId() );
057            throw new EnforcerRuleException( sb.toString() );
058        }
059        if ( failWhenParentIsRelease && project.hasParent() )
060        {
061            // project.getParentArtifact() does not work here if a "CI Friendly Version" is used (e.g. ${revision})
062            Artifact parentArtifact = getProject( true, helper ).getArtifact();
063            if ( parentArtifact != null && !parentArtifact.isSnapshot() )
064            {
065                throw new EnforcerRuleException( "Parent cannot be a release: " + parentArtifact.getId() );
066            }
067        }
068
069    }
070
071    private MavenProject getProject( boolean parent, EnforcerRuleHelper helper )
072        throws EnforcerRuleException
073    {
074        String expression = parent ? "${project.parent}" : "${project}";
075        try
076        {
077            return (MavenProject) helper.evaluate( expression );
078        }
079        catch ( ExpressionEvaluationException eee )
080        {
081            throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee );
082        }
083    }
084
085    public boolean isFailWhenParentIsRelease()
086    {
087        return failWhenParentIsRelease;
088    }
089
090    public void setFailWhenParentIsRelease( boolean failWhenParentIsRelease )
091    {
092        this.failWhenParentIsRelease = failWhenParentIsRelease;
093    }
094
095}