View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.enforcer.rules;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  
24  import java.util.Objects;
25  
26  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
27  import org.apache.maven.model.DistributionManagement;
28  import org.apache.maven.project.MavenProject;
29  
30  /**
31   * This rule will check if a pom contains a <code>distributionManagement</code> part. This should be by best practice
32   * only defined once. It could happen that you like to check the parent as well. This can be activated by using the
33   * <code>ignoreParent</code> which is by default turned off (<code>true</code>) which means not to check the parent.
34   *
35   * @author Karl Heinz Marbaise
36   * @since 1.4
37   */
38  @Named("banDistributionManagement")
39  public final class BanDistributionManagement extends AbstractStandardEnforcerRule {
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 final MavenProject project;
57  
58      @Inject
59      public BanDistributionManagement(MavenProject project) {
60          this.project = Objects.requireNonNull(project);
61      }
62  
63      @Override
64      public void execute() throws EnforcerRuleException {
65  
66          if (project.isExecutionRoot()) {
67              if (project.getParent() == null) {
68                  // Does it make sense to check something? If yes please make a JIRA ticket for it.
69                  getLog().debug("We have no parent and in the root of a build we don't check anything,");
70                  getLog().debug("because that is the location where we defined maven-enforcer-plugin.");
71              } else {
72                  getLog().debug("We are in the root of the execution and we have a parent.");
73  
74                  DistributionManagementCheck check = new DistributionManagementCheck(project);
75                  check.execute(isAllowRepository(), isAllowSnapshotRepository(), isAllowSite());
76              }
77          } else {
78              getLog().debug("We are in a deeper level.");
79              DistributionManagementCheck check = new DistributionManagementCheck(project);
80              check.execute(isAllowRepository(), isAllowSnapshotRepository(), isAllowSite());
81          }
82      }
83  
84      public boolean isAllowRepository() {
85          return allowRepository;
86      }
87  
88      public void setAllowRepository(boolean allowRepository) {
89          this.allowRepository = allowRepository;
90      }
91  
92      public boolean isAllowSnapshotRepository() {
93          return allowSnapshotRepository;
94      }
95  
96      public void setAllowSnapshotRepository(boolean allowSnapshotRepository) {
97          this.allowSnapshotRepository = allowSnapshotRepository;
98      }
99  
100     public boolean isAllowSite() {
101         return allowSite;
102     }
103 
104     public void setAllowSite(boolean allowSite) {
105         this.allowSite = allowSite;
106     }
107 
108     private static class DistributionManagementCheck {
109         private DistributionManagement distributionManagement;
110 
111         DistributionManagementCheck(MavenProject project) {
112             this.distributionManagement = project.getOriginalModel().getDistributionManagement();
113         }
114 
115         public void execute(boolean isAllowRepository, boolean isAllowSnapshotRepository, boolean isAllowSite)
116                 throws EnforcerRuleException {
117             if (hasDistributionManagement()) {
118                 if (!isAllowRepository && hasRepository()) {
119                     throw new EnforcerRuleException("You have defined a repository in distributionManagement.");
120                 } else if (!isAllowSnapshotRepository && hasSnapshotRepository()) {
121                     throw new EnforcerRuleException("You have defined a snapshotRepository in distributionManagement.");
122                 } else if (!isAllowSite && hasSite()) {
123                     throw new EnforcerRuleException("You have defined a site in distributionManagement.");
124                 }
125             }
126         }
127 
128         private boolean hasRepository() {
129             return getDistributionManagement().getRepository() != null;
130         }
131 
132         public DistributionManagement getDistributionManagement() {
133             return distributionManagement;
134         }
135 
136         public void setDistributionManagement(DistributionManagement distributionManagement) {
137             this.distributionManagement = distributionManagement;
138         }
139 
140         private boolean hasSnapshotRepository() {
141             return getDistributionManagement().getSnapshotRepository() != null;
142         }
143 
144         private boolean hasSite() {
145             return getDistributionManagement().getSite() != null;
146         }
147 
148         private boolean hasDistributionManagement() {
149             return getDistributionManagement() != null;
150         }
151     }
152 
153     @Override
154     public String toString() {
155         return String.format(
156                 "BanDistributionManagement[allowRepository=%b, allowSnapshotRepository=%b, allowSite=%b]",
157                 allowRepository, allowSnapshotRepository, allowSite);
158     }
159 }