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