1 | |
package org.apache.maven.plugins.enforcer; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Collections; |
26 | |
import java.util.List; |
27 | |
|
28 | |
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; |
29 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionException; |
30 | |
import org.apache.maven.enforcer.rule.api.EnforcerRuleException; |
31 | |
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; |
32 | |
import org.apache.maven.model.Model; |
33 | |
import org.apache.maven.model.Repository; |
34 | |
import org.apache.maven.plugins.enforcer.utils.EnforcerRuleUtils; |
35 | |
import org.apache.maven.project.MavenProject; |
36 | |
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; |
37 | |
import org.codehaus.plexus.util.StringUtils; |
38 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class RequireNoRepositories |
46 | |
extends AbstractNonCacheableEnforcerRule |
47 | |
{ |
48 | |
|
49 | |
|
50 | |
|
51 | 0 | public boolean banRepositories = true; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | public boolean banPluginRepositories = true; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 0 | public List<String> allowedRepositories = Collections.emptyList(); |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 0 | public List<String> allowedPluginRepositories = Collections.emptyList(); |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | 0 | public boolean allowSnapshotRepositories = false; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | 0 | public boolean allowSnapshotPluginRepositories = false; |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public void execute( EnforcerRuleHelper helper ) |
84 | |
throws EnforcerRuleException |
85 | |
{ |
86 | 0 | EnforcerRuleUtils utils = new EnforcerRuleUtils( helper ); |
87 | |
|
88 | |
MavenProject project; |
89 | |
try |
90 | |
{ |
91 | 0 | project = (MavenProject) helper.evaluate( "${project}" ); |
92 | |
|
93 | 0 | List<Model> models = |
94 | |
utils.getModelsRecursively( project.getGroupId(), project.getArtifactId(), project.getVersion(), |
95 | |
new File( project.getBasedir(), "pom.xml" ) ); |
96 | 0 | List<Model> badModels = new ArrayList<Model>(); |
97 | |
|
98 | 0 | StringBuffer newMsg = new StringBuffer(); |
99 | 0 | newMsg.append( "Some poms have repositories defined:\n" ); |
100 | |
|
101 | 0 | for ( Model model : models ) |
102 | |
{ |
103 | 0 | if ( banRepositories ) |
104 | |
{ |
105 | |
@SuppressWarnings( "unchecked" ) |
106 | 0 | List<Repository> repos = model.getRepositories(); |
107 | 0 | if ( repos != null && !repos.isEmpty() ) |
108 | |
{ |
109 | 0 | List<String> bannedRepos = |
110 | |
findBannedRepositories( repos, allowedRepositories, allowSnapshotRepositories ); |
111 | 0 | if ( !bannedRepos.isEmpty() ) |
112 | |
{ |
113 | 0 | badModels.add( model ); |
114 | 0 | newMsg.append( |
115 | |
model.getGroupId() + ":" + model.getArtifactId() + " version:" + model.getVersion() |
116 | |
+ " has repositories " + bannedRepos ); |
117 | |
} |
118 | |
} |
119 | |
} |
120 | 0 | if ( banPluginRepositories ) |
121 | |
{ |
122 | |
@SuppressWarnings( "unchecked" ) |
123 | 0 | List<Repository> repos = model.getPluginRepositories(); |
124 | 0 | if ( repos != null && !repos.isEmpty() ) |
125 | |
{ |
126 | 0 | List<String> bannedRepos = |
127 | |
findBannedRepositories( repos, allowedPluginRepositories, allowSnapshotPluginRepositories ); |
128 | 0 | if ( !bannedRepos.isEmpty() ) |
129 | |
{ |
130 | 0 | badModels.add( model ); |
131 | 0 | newMsg.append( |
132 | |
model.getGroupId() + ":" + model.getArtifactId() + " version:" + model.getVersion() |
133 | |
+ " has plugin repositories " + bannedRepos ); |
134 | |
} |
135 | |
} |
136 | 0 | } |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | 0 | if ( !badModels.isEmpty() ) |
142 | |
{ |
143 | 0 | if ( StringUtils.isNotEmpty( message ) ) |
144 | |
{ |
145 | 0 | newMsg.append( message ); |
146 | |
} |
147 | |
|
148 | 0 | throw new EnforcerRuleException( newMsg.toString() ); |
149 | |
} |
150 | |
|
151 | |
} |
152 | 0 | catch ( ExpressionEvaluationException e ) |
153 | |
{ |
154 | 0 | throw new EnforcerRuleException( e.getLocalizedMessage() ); |
155 | |
} |
156 | 0 | catch ( ArtifactResolutionException e ) |
157 | |
{ |
158 | 0 | throw new EnforcerRuleException( e.getLocalizedMessage() ); |
159 | |
} |
160 | 0 | catch ( ArtifactNotFoundException e ) |
161 | |
{ |
162 | 0 | throw new EnforcerRuleException( e.getLocalizedMessage() ); |
163 | |
} |
164 | 0 | catch ( IOException e ) |
165 | |
{ |
166 | 0 | throw new EnforcerRuleException( e.getLocalizedMessage() ); |
167 | |
} |
168 | 0 | catch ( XmlPullParserException e ) |
169 | |
{ |
170 | 0 | throw new EnforcerRuleException( e.getLocalizedMessage() ); |
171 | 0 | } |
172 | 0 | } |
173 | |
|
174 | |
private static List<String> findBannedRepositories( List<Repository> repos, List<String> allowedRepos, boolean allowSnapshots ) |
175 | |
{ |
176 | 0 | List<String> bannedRepos = new ArrayList<String>( allowedRepos.size() ); |
177 | 0 | for ( Repository r : repos ) |
178 | |
{ |
179 | 0 | if ( !allowedRepos.contains( r.getId() ) ) |
180 | |
{ |
181 | 0 | if ( !allowSnapshots || r.getReleases().isEnabled() ) |
182 | |
{ |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | 0 | bannedRepos.add( r.getId() ); |
188 | |
} |
189 | |
} |
190 | |
} |
191 | 0 | return bannedRepos; |
192 | |
} |
193 | |
} |