1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
32
33
34
35
36
37
38 @Named("banDistributionManagement")
39 public final class BanDistributionManagement extends AbstractStandardEnforcerRule {
40
41
42
43
44 private boolean allowRepository = false;
45
46
47
48
49 private boolean allowSnapshotRepository = false;
50
51
52
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
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 }