View Javadoc
1   package org.apache.maven.plugins.enforcer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
23  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
24  import org.apache.maven.plugin.logging.Log;
25  import org.apache.maven.plugins.enforcer.utils.DistributionManagementCheck;
26  import org.apache.maven.project.MavenProject;
27  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
28  
29  /**
30   * This rule will check if a pom contains a <code>distributionManagement</code> part. This should be by best practice
31   * only defined once. It could happen that you like to check the parent as well. This can be activated by using the
32   * <code>ignoreParent</code> which is by default turned off (<code>true</code>) which means not to check the parent.
33   * 
34   * @author Karl Heinz Marbaise
35   * @since 1.4
36   */
37  public class BanDistributionManagement
38      extends AbstractNonCacheableEnforcerRule
39  {
40  
41      /**
42       * If we turn on the <code>ignoreParent</code> the parent will be ignored.
43       */
44      private boolean ignoreParent = true;
45  
46      /**
47       * Allow using a repository entry in the distributionManagement area.
48       */
49      private boolean allowRepository = false;
50  
51      /**
52       * Allow snapshotRepository entry in the distributionManagement area.
53       */
54      private boolean allowSnapshotRepository = false;
55  
56      /**
57       * Allow site entry in the distributionManagement area.
58       */
59      private boolean allowSite = false;
60  
61      /**
62       * Execute checking only in root of project.
63       */
64      private boolean executeRootOnly = false;
65  
66      private Log logger;
67  
68      /**
69       * {@inheritDoc}
70       */
71      public void execute( EnforcerRuleHelper helper )
72          throws EnforcerRuleException
73      {
74          logger = helper.getLog();
75  
76          try
77          {
78              MavenProject project = (MavenProject) helper.evaluate( "${project}" );
79  
80              if ( !project.isExecutionRoot() && !executeRootOnly )
81              {
82  
83                  logger.debug( "distributionManagement: " + project.getDistributionManagement() );
84  
85                  DistributionManagementCheck check = new DistributionManagementCheck( project );
86  
87                  check.execute( isAllowRepository(), isAllowSnapshotRepository(), isAllowSite() );
88  
89                  if ( !isIgnoreParent() && ( project.getParent() != null ) )
90                  {
91                      DistributionManagementCheck checkParent = new DistributionManagementCheck( project.getParent() );
92                      checkParent.execute( isAllowRepository(), isAllowSnapshotRepository(), isAllowSite() );
93                  }
94  
95              }
96          }
97          catch ( ExpressionEvaluationException e )
98          {
99              throw new EnforcerRuleException( e.getMessage(), e );
100         }
101     }
102 
103     public boolean isIgnoreParent()
104     {
105         return ignoreParent;
106     }
107 
108     public void setIgnoreParent( boolean ignoreParent )
109     {
110         this.ignoreParent = ignoreParent;
111     }
112 
113     public boolean isAllowRepository()
114     {
115         return allowRepository;
116     }
117 
118     public void setAllowRepository( boolean allowRepository )
119     {
120         this.allowRepository = allowRepository;
121     }
122 
123     public boolean isAllowSnapshotRepository()
124     {
125         return allowSnapshotRepository;
126     }
127 
128     public void setAllowSnapshotRepository( boolean allowSnapshotRepository )
129     {
130         this.allowSnapshotRepository = allowSnapshotRepository;
131     }
132 
133     public boolean isAllowSite()
134     {
135         return allowSite;
136     }
137 
138     public void setAllowSite( boolean allowSite )
139     {
140         this.allowSite = allowSite;
141     }
142 
143 }