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       * Allow using a repository entry in the distributionManagement area.
43       */
44      private boolean allowRepository = false;
45  
46      /**
47       * Allow snapshotRepository entry in the distributionManagement area.
48       */
49      private boolean allowSnapshotRepository = false;
50  
51      /**
52       * Allow site entry in the distributionManagement area.
53       */
54      private boolean allowSite = false;
55  
56      private Log logger;
57  
58      @Override
59      public void execute( EnforcerRuleHelper helper )
60          throws EnforcerRuleException
61      {
62          logger = helper.getLog();
63  
64          try
65          {
66              MavenProject project = (MavenProject) helper.evaluate( "${project}" );
67  
68              if ( project.isExecutionRoot() )
69              {
70                  if ( project.getParent() == null )
71                  {
72                      // Does it make sense to check something? If yes please make a JIRA ticket for it.
73                      logger.debug( "We have no parent and in the root of a build we don't check anything," );
74                      logger.debug( "because that is the location where we defined maven-enforcer-plugin." );
75                  }
76                  else
77                  {
78                      logger.debug( "We are in the root of the execution and we have a parent." );
79  
80                      DistributionManagementCheck check = new DistributionManagementCheck( project );
81                      check.execute( isAllowRepository(), isAllowSnapshotRepository(), isAllowSite() );
82                  }
83              }
84              else
85              {
86                  logger.debug( "We are in a deeper level." );
87                  DistributionManagementCheck check = new DistributionManagementCheck( project );
88                  check.execute( isAllowRepository(), isAllowSnapshotRepository(), isAllowSite() );
89  
90              }
91          }
92          catch ( ExpressionEvaluationException e )
93          {
94              throw new EnforcerRuleException( e.getMessage(), e );
95          }
96      }
97  
98      public boolean isAllowRepository()
99      {
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 }