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