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    /**
065     * {@inheritDoc}
066     */
067    public void execute( EnforcerRuleHelper helper )
068        throws EnforcerRuleException
069    {
070        logger = helper.getLog();
071
072        try
073        {
074            MavenProject project = (MavenProject) helper.evaluate( "${project}" );
075
076            if ( project.isExecutionRoot() )
077            {
078                if ( project.getParent() == null )
079                {
080                    // Does it make sense to check something? If yes please make a JIRA ticket for it.
081                    logger.debug( "We have no parent and in the root of a build we don't check anything," );
082                    logger.debug( "because that is the location where we defined maven-enforcer-plugin." );
083                }
084                else
085                {
086                    logger.debug( "We are in the root of the execution and we have a parent." );
087
088                    DistributionManagementCheck check = new DistributionManagementCheck( project );
089                    check.execute( isAllowRepository(), isAllowSnapshotRepository(), isAllowSite() );
090                }
091            }
092            else
093            {
094                logger.debug( "We are in a deeper level." );
095                DistributionManagementCheck check = new DistributionManagementCheck( project );
096                check.execute( isAllowRepository(), isAllowSnapshotRepository(), isAllowSite() );
097
098                if ( !isIgnoreParent() )
099                {
100                    logger.warn( "You have configured not to ignore the parent." );
101                    logger.warn( "This configuration is deprecated and will be ignored." );
102                }
103            }
104        }
105        catch ( ExpressionEvaluationException e )
106        {
107            throw new EnforcerRuleException( e.getMessage(), e );
108        }
109    }
110
111    public boolean isIgnoreParent()
112    {
113        return ignoreParent;
114    }
115
116    public void setIgnoreParent( boolean ignoreParent )
117    {
118        this.ignoreParent = ignoreParent;
119    }
120
121    public boolean isAllowRepository()
122    {
123        return allowRepository;
124    }
125
126    public void setAllowRepository( boolean allowRepository )
127    {
128        this.allowRepository = allowRepository;
129    }
130
131    public boolean isAllowSnapshotRepository()
132    {
133        return allowSnapshotRepository;
134    }
135
136    public void setAllowSnapshotRepository( boolean allowSnapshotRepository )
137    {
138        this.allowSnapshotRepository = allowSnapshotRepository;
139    }
140
141    public boolean isAllowSite()
142    {
143        return allowSite;
144    }
145
146    public void setAllowSite( boolean allowSite )
147    {
148        this.allowSite = allowSite;
149    }
150
151}