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.enforcer.rule.api.EnforcerRuleException;
023import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
024import org.apache.maven.plugin.logging.Log;
025import org.apache.maven.plugins.enforcer.utils.DistributionManagementCheck;
026import org.apache.maven.project.MavenProject;
027import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
028
029/**
030 * This rule will check if a pom contains a <code>distributionManagement</code> part. This should be by best practice
031 * only defined once. It could happen that you like to check the parent as well. This can be activated by using the
032 * <code>ignoreParent</code> which is by default turned off (<code>true</code>) which means not to check the parent.
033 * 
034 * @author Karl Heinz Marbaise
035 * @since 1.4
036 */
037public class BanDistributionManagement
038    extends AbstractNonCacheableEnforcerRule
039{
040
041    /**
042     * If we turn on the <code>ignoreParent</code> the parent will be ignored.
043     * @deprecated
044     */
045    private boolean ignoreParent = true;
046
047    /**
048     * Allow using a repository entry in the distributionManagement area.
049     */
050    private boolean allowRepository = false;
051
052    /**
053     * Allow snapshotRepository entry in the distributionManagement area.
054     */
055    private boolean allowSnapshotRepository = false;
056
057    /**
058     * Allow site entry in the distributionManagement area.
059     */
060    private boolean allowSite = false;
061
062    private Log logger;
063
064    @Override
065    public void execute( EnforcerRuleHelper helper )
066        throws EnforcerRuleException
067    {
068        logger = helper.getLog();
069
070        try
071        {
072            MavenProject project = (MavenProject) helper.evaluate( "${project}" );
073
074            if ( project.isExecutionRoot() )
075            {
076                if ( project.getParent() == null )
077                {
078                    // Does it make sense to check something? If yes please make a JIRA ticket for it.
079                    logger.debug( "We have no parent and in the root of a build we don't check anything," );
080                    logger.debug( "because that is the location where we defined maven-enforcer-plugin." );
081                }
082                else
083                {
084                    logger.debug( "We are in the root of the execution and we have a parent." );
085
086                    DistributionManagementCheck check = new DistributionManagementCheck( project );
087                    check.execute( isAllowRepository(), isAllowSnapshotRepository(), isAllowSite() );
088                }
089            }
090            else
091            {
092                logger.debug( "We are in a deeper level." );
093                DistributionManagementCheck check = new DistributionManagementCheck( project );
094                check.execute( isAllowRepository(), isAllowSnapshotRepository(), isAllowSite() );
095
096                if ( !isIgnoreParent() )
097                {
098                    logger.warn( "You have configured not to ignore the parent." );
099                    logger.warn( "This configuration is deprecated and will be ignored." );
100                }
101            }
102        }
103        catch ( ExpressionEvaluationException e )
104        {
105            throw new EnforcerRuleException( e.getMessage(), e );
106        }
107    }
108
109    public boolean isIgnoreParent()
110    {
111        return ignoreParent;
112    }
113
114    public void setIgnoreParent( boolean ignoreParent )
115    {
116        this.ignoreParent = ignoreParent;
117    }
118
119    public boolean isAllowRepository()
120    {
121        return allowRepository;
122    }
123
124    public void setAllowRepository( boolean allowRepository )
125    {
126        this.allowRepository = allowRepository;
127    }
128
129    public boolean isAllowSnapshotRepository()
130    {
131        return allowSnapshotRepository;
132    }
133
134    public void setAllowSnapshotRepository( boolean allowSnapshotRepository )
135    {
136        this.allowSnapshotRepository = allowSnapshotRepository;
137    }
138
139    public boolean isAllowSite()
140    {
141        return allowSite;
142    }
143
144    public void setAllowSite( boolean allowSite )
145    {
146        this.allowSite = allowSite;
147    }
148
149}