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     * Allow using a repository entry in the distributionManagement area.
043     */
044    private boolean allowRepository = false;
045
046    /**
047     * Allow snapshotRepository entry in the distributionManagement area.
048     */
049    private boolean allowSnapshotRepository = false;
050
051    /**
052     * Allow site entry in the distributionManagement area.
053     */
054    private boolean allowSite = false;
055
056    @Override
057    public void execute( EnforcerRuleHelper helper )
058        throws EnforcerRuleException
059    {
060        Log logger = helper.getLog();
061
062        try
063        {
064            MavenProject project = (MavenProject) helper.evaluate( "${project}" );
065
066            if ( project.isExecutionRoot() )
067            {
068                if ( project.getParent() == null )
069                {
070                    // Does it make sense to check something? If yes please make a JIRA ticket for it.
071                    logger.debug( "We have no parent and in the root of a build we don't check anything," );
072                    logger.debug( "because that is the location where we defined maven-enforcer-plugin." );
073                }
074                else
075                {
076                    logger.debug( "We are in the root of the execution and we have a parent." );
077
078                    DistributionManagementCheck check = new DistributionManagementCheck( project );
079                    check.execute( isAllowRepository(), isAllowSnapshotRepository(), isAllowSite() );
080                }
081            }
082            else
083            {
084                logger.debug( "We are in a deeper level." );
085                DistributionManagementCheck check = new DistributionManagementCheck( project );
086                check.execute( isAllowRepository(), isAllowSnapshotRepository(), isAllowSite() );
087
088            }
089        }
090        catch ( ExpressionEvaluationException e )
091        {
092            throw new EnforcerRuleException( e.getMessage(), e );
093        }
094    }
095
096    public boolean isAllowRepository()
097    {
098        return allowRepository;
099    }
100
101    public void setAllowRepository( boolean allowRepository )
102    {
103        this.allowRepository = allowRepository;
104    }
105
106    public boolean isAllowSnapshotRepository()
107    {
108        return allowSnapshotRepository;
109    }
110
111    public void setAllowSnapshotRepository( boolean allowSnapshotRepository )
112    {
113        this.allowSnapshotRepository = allowSnapshotRepository;
114    }
115
116    public boolean isAllowSite()
117    {
118        return allowSite;
119    }
120
121    public void setAllowSite( boolean allowSite )
122    {
123        this.allowSite = allowSite;
124    }
125
126}