001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.enforcer.rules;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023
024import java.util.Objects;
025
026import org.apache.maven.artifact.Artifact;
027import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
028import org.apache.maven.project.MavenProject;
029
030/**
031 * This rule checks that the current project is not a snapshot.
032 *
033 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
034 */
035@Named("requireReleaseVersion")
036public final class RequireReleaseVersion extends AbstractStandardEnforcerRule {
037
038    private final MavenProject project;
039
040    /**
041     * Allows this rule to fail when the parent is defined as a snapshot.
042     */
043    private boolean failWhenParentIsSnapshot = true;
044
045    @Inject
046    public RequireReleaseVersion(MavenProject project) {
047        this.project = Objects.requireNonNull(project);
048    }
049
050    @Override
051    public void execute() throws EnforcerRuleException {
052
053        if (project.getArtifact().isSnapshot()) {
054            String message = getMessage();
055            StringBuilder buf = new StringBuilder();
056            if (message != null) {
057                buf.append(message).append(System.lineSeparator());
058            }
059            buf.append("This project cannot be a snapshot:")
060                    .append(project.getArtifact().getId());
061            throw new EnforcerRuleException(buf.toString());
062        }
063        if (failWhenParentIsSnapshot) {
064            Artifact parentArtifact = project.getParentArtifact();
065            if (parentArtifact != null && parentArtifact.isSnapshot()) {
066                throw new EnforcerRuleException("Parent Cannot be a snapshot: " + parentArtifact.getId());
067            }
068        }
069    }
070
071    public void setFailWhenParentIsSnapshot(boolean failWhenParentIsSnapshot) {
072        this.failWhenParentIsSnapshot = failWhenParentIsSnapshot;
073    }
074
075    @Override
076    public String toString() {
077        return String.format("RequireReleaseVersion[failWhenParentIsSnapshot=%b]", failWhenParentIsSnapshot);
078    }
079}