24
24
import java .util .ArrayList ;
25
25
import java .util .Arrays ;
26
26
import java .util .Collection ;
27
+ import java .util .Collections ;
27
28
import java .util .HashMap ;
28
29
import java .util .HashSet ;
29
30
import java .util .List ;
39
40
import org .apache .maven .artifact .resolver .ArtifactNotFoundException ;
40
41
import org .apache .maven .artifact .versioning .InvalidVersionSpecificationException ;
41
42
import org .apache .maven .artifact .versioning .VersionRange ;
43
+ import org .apache .maven .enforcer .rule .api .EnforcerRuleError ;
42
44
import org .apache .maven .enforcer .rule .api .EnforcerRuleException ;
43
45
import org .apache .maven .enforcer .rules .utils .EnforcerRuleUtils ;
44
46
import org .apache .maven .enforcer .rules .utils .ExpressionEvaluator ;
50
52
import org .apache .maven .lifecycle .mapping .LifecycleMapping ;
51
53
import org .apache .maven .model .BuildBase ;
52
54
import org .apache .maven .model .Model ;
55
+ import org .apache .maven .model .ModelBase ;
53
56
import org .apache .maven .model .Plugin ;
57
+ import org .apache .maven .model .PluginConfiguration ;
58
+ import org .apache .maven .model .PluginContainer ;
54
59
import org .apache .maven .model .Profile ;
55
60
import org .apache .maven .model .ReportPlugin ;
61
+ import org .apache .maven .model .Reporting ;
56
62
import org .apache .maven .plugin .InvalidPluginException ;
57
- import org .apache .maven .plugin .MojoExecutionException ;
58
63
import org .apache .maven .plugin .PluginManager ;
59
64
import org .apache .maven .plugin .PluginManagerException ;
60
65
import org .apache .maven .plugin .PluginNotFoundException ;
72
77
import org .eclipse .aether .resolution .ArtifactRequest ;
73
78
import org .eclipse .aether .resolution .ArtifactResolutionException ;
74
79
80
+ import static java .util .Optional .ofNullable ;
81
+
75
82
/**
76
83
* This rule will enforce that all plugins specified in the poms have a version declared.
77
84
*
@@ -127,6 +134,7 @@ public final class RequirePluginVersions extends AbstractStandardEnforcerRule {
127
134
/**
128
135
* Same as unCheckedPlugins but as a comma list to better support properties. Sample form:
129
136
* <code>group:artifactId,group2:artifactId2</code>
137
+ *
130
138
* @since 1.0-beta-1
131
139
*/
132
140
private String unCheckedPluginList ;
@@ -145,9 +153,6 @@ public final class RequirePluginVersions extends AbstractStandardEnforcerRule {
145
153
146
154
private final RepositorySystem repositorySystem ;
147
155
148
- /** The local. */
149
- private ArtifactRepository local ;
150
-
151
156
/** The session. */
152
157
private final MavenSession session ;
153
158
@@ -195,7 +200,6 @@ public void execute() throws EnforcerRuleException {
195
200
// get the various expressions out of the helper.
196
201
197
202
lifecycles = defaultLifeCycles .getLifeCycles ();
198
- local = session .getLocalRepository ();
199
203
200
204
// get all the plugins that are bound to the specified lifecycles
201
205
Set <Plugin > allPlugins = getBoundPlugins (project , phases );
@@ -236,18 +240,20 @@ public void execute() throws EnforcerRuleException {
236
240
if (!failures .isEmpty ()) {
237
241
handleMessagesToTheUser (project , failures );
238
242
}
239
- } catch (Exception e ) {
243
+ } catch (PluginNotFoundException | LifecycleExecutionException e ) {
240
244
throw new EnforcerRuleException (e .getLocalizedMessage (), e );
241
245
}
242
246
}
243
247
244
248
private void handleMessagesToTheUser (MavenProject project , List <Plugin > failures ) throws EnforcerRuleException {
245
249
StringBuilder newMsg = new StringBuilder ();
246
- newMsg .append ("Some plugins are missing valid versions or depend on Maven "
247
- + runtimeInformation .getMavenVersion () + " defaults:" );
250
+ newMsg .append ("Some plugins are missing valid versions or depend on Maven " );
251
+ newMsg .append (runtimeInformation .getMavenVersion ());
252
+ newMsg .append (" defaults" );
248
253
handleBanMessages (newMsg );
249
- newMsg .append (" \n " );
254
+ newMsg .append (System . lineSeparator () );
250
255
for (Plugin plugin : failures ) {
256
+ newMsg .append (" " );
251
257
newMsg .append (plugin .getGroupId ());
252
258
newMsg .append (":" );
253
259
newMsg .append (plugin .getArtifactId ());
@@ -280,7 +286,7 @@ private void handleMessagesToTheUser(MavenProject project, List<Plugin> failures
280
286
getLog ().debug ("Exception while determining plugin Version " + e .getMessage ());
281
287
newMsg .append (". Unable to determine the plugin version." );
282
288
}
283
- newMsg .append (" \n " );
289
+ newMsg .append (System . lineSeparator () );
284
290
}
285
291
String message = getMessage ();
286
292
if (StringUtils .isNotEmpty (message )) {
@@ -292,17 +298,24 @@ private void handleMessagesToTheUser(MavenProject project, List<Plugin> failures
292
298
293
299
private void handleBanMessages (StringBuilder newMsg ) {
294
300
if (banLatest || banRelease || banSnapshots || banTimestamps ) {
295
- newMsg . append ( " (" );
301
+ List < String > banList = new ArrayList <>( );
296
302
if (banLatest ) {
297
- newMsg . append ("LATEST " );
303
+ banList . add ("LATEST" );
298
304
}
299
305
if (banRelease ) {
300
- newMsg .append ("RELEASE " );
306
+ banList .add ("RELEASE" );
307
+ }
308
+ if (banSnapshots ) {
309
+ banList .add ("SNAPSHOT" );
310
+ if (banTimestamps ) {
311
+ banList .add ("TIMESTAMP SNAPSHOT" );
312
+ }
301
313
}
302
- if (banSnapshots || banTimestamps ) {
303
- newMsg .append ("SNAPSHOT " );
314
+ if (!banList .isEmpty ()) {
315
+ newMsg .append (" (" );
316
+ newMsg .append (String .join (", " , banList ));
317
+ newMsg .append (" as plugin version are not allowed)" );
304
318
}
305
- newMsg .append ("are not allowed)" );
306
319
}
307
320
}
308
321
@@ -312,10 +325,9 @@ private void handleBanMessages(StringBuilder newMsg) {
312
325
* @param uncheckedPlugins
313
326
* @param plugins
314
327
* @return The plugins which have been removed.
315
- * @throws MojoExecutionException
316
328
*/
317
329
Set <Plugin > removeUncheckedPlugins (Collection <String > uncheckedPlugins , Set <Plugin > plugins )
318
- throws MojoExecutionException {
330
+ throws EnforcerRuleError {
319
331
if (uncheckedPlugins != null && !uncheckedPlugins .isEmpty ()) {
320
332
for (String pluginKey : uncheckedPlugins ) {
321
333
Plugin plugin = parsePluginString (pluginKey , "UncheckedPlugins" );
@@ -328,7 +340,7 @@ Set<Plugin> removeUncheckedPlugins(Collection<String> uncheckedPlugins, Set<Plug
328
340
/**
329
341
* Combines the old Collection with the new comma separated list.
330
342
*
331
- * @param uncheckedPlugins a new collections
343
+ * @param uncheckedPlugins a new collections
332
344
* @param uncheckedPluginsList a list to merge
333
345
* @return List of unchecked plugins.
334
346
*/
@@ -354,10 +366,9 @@ public Collection<String> combineUncheckedPlugins(
354
366
* @param existing the existing
355
367
* @param additional the additional
356
368
* @return the sets the
357
- * @throws MojoExecutionException the mojo execution exception
369
+ * @throws EnforcerRuleError the enforcer error
358
370
*/
359
- public Set <Plugin > addAdditionalPlugins (Set <Plugin > existing , List <String > additional )
360
- throws MojoExecutionException {
371
+ public Set <Plugin > addAdditionalPlugins (Set <Plugin > existing , List <String > additional ) throws EnforcerRuleError {
361
372
if (additional != null ) {
362
373
for (String pluginString : additional ) {
363
374
Plugin plugin = parsePluginString (pluginString , "AdditionalPlugins" );
@@ -377,11 +388,10 @@ public Set<Plugin> addAdditionalPlugins(Set<Plugin> existing, List<String> addit
377
388
* Helper method to parse and inject a Plugin.
378
389
*
379
390
* @param pluginString a plugin description to parse
380
- * @param field a source of pluginString
391
+ * @param field a source of pluginString
381
392
* @return the prepared plugin
382
- * @throws MojoExecutionException in case of problems
383
393
*/
384
- private Plugin parsePluginString (String pluginString , String field ) throws MojoExecutionException {
394
+ private Plugin parsePluginString (String pluginString , String field ) throws EnforcerRuleError {
385
395
if (pluginString != null ) {
386
396
String [] pluginStrings = pluginString .split (":" );
387
397
if (pluginStrings .length == 2 ) {
@@ -391,10 +401,10 @@ private Plugin parsePluginString(String pluginString, String field) throws MojoE
391
401
392
402
return plugin ;
393
403
} else {
394
- throw new MojoExecutionException ("Invalid " + field + " string: " + pluginString );
404
+ throw new EnforcerRuleError ("Invalid " + field + " string: " + pluginString );
395
405
}
396
406
} else {
397
- throw new MojoExecutionException ("Invalid " + field + " string: " + pluginString );
407
+ throw new EnforcerRuleError ("Invalid " + field + " string: " + pluginString );
398
408
}
399
409
}
400
410
@@ -813,42 +823,62 @@ private List<PluginWrapper> getAllPluginEntries(MavenProject project) {
813
823
}
814
824
815
825
private void addPluginsInProfiles (List <PluginWrapper > plugins , Model model ) {
816
- List <Profile > profiles = model . getProfiles ( );
826
+ List <Profile > profiles = ofNullable ( model ). map ( Model :: getProfiles ). orElseGet ( Collections :: emptyList );
817
827
for (Profile profile : profiles ) {
818
- getProfilePlugins (plugins , model , profile );
819
- getProfileReportingPlugins (plugins , model , profile );
820
- getProfilePluginManagementPlugins (plugins , model , profile );
828
+ getProfilePlugins (plugins , profile );
829
+ getProfileReportingPlugins (plugins , profile );
830
+ getProfilePluginManagementPlugins (plugins , profile );
821
831
}
822
832
}
823
833
824
- private void getProfilePluginManagementPlugins (List <PluginWrapper > plugins , Model model , Profile profile ) {
825
- List <Plugin > modelPlugins = profile .getBuild ().getPluginManagement ().getPlugins ();
834
+ private void getProfilePluginManagementPlugins (List <PluginWrapper > plugins , Profile profile ) {
835
+ List <Plugin > modelPlugins = ofNullable (profile )
836
+ .map (Profile ::getBuild )
837
+ .map (PluginConfiguration ::getPluginManagement )
838
+ .map (PluginContainer ::getPlugins )
839
+ .orElseGet (Collections ::emptyList );
826
840
plugins .addAll (PluginWrapper .addAll (utils .resolvePlugins (modelPlugins ), banMavenDefaults ));
827
841
}
828
842
829
- private void getProfileReportingPlugins (List <PluginWrapper > plugins , Model model , Profile profile ) {
830
- List <ReportPlugin > modelReportPlugins = profile .getReporting ().getPlugins ();
843
+ private void getProfileReportingPlugins (List <PluginWrapper > plugins , Profile profile ) {
844
+ List <ReportPlugin > modelReportPlugins = ofNullable (profile )
845
+ .map (ModelBase ::getReporting )
846
+ .map (Reporting ::getPlugins )
847
+ .orElseGet (Collections ::emptyList );
831
848
// add the reporting plugins
832
849
plugins .addAll (PluginWrapper .addAll (utils .resolveReportPlugins (modelReportPlugins ), banMavenDefaults ));
833
850
}
834
851
835
- private void getProfilePlugins (List <PluginWrapper > plugins , Model model , Profile profile ) {
836
- List <Plugin > modelPlugins = profile .getBuild ().getPlugins ();
852
+ private void getProfilePlugins (List <PluginWrapper > plugins , Profile profile ) {
853
+ List <Plugin > modelPlugins = ofNullable (profile )
854
+ .map (Profile ::getBuild )
855
+ .map (PluginContainer ::getPlugins )
856
+ .orElseGet (Collections ::emptyList );
837
857
plugins .addAll (PluginWrapper .addAll (utils .resolvePlugins (modelPlugins ), banMavenDefaults ));
838
858
}
839
859
840
860
private void getPlugins (List <PluginWrapper > plugins , Model model ) {
841
- List <Plugin > modelPlugins = model .getBuild ().getPlugins ();
861
+ List <Plugin > modelPlugins = ofNullable (model )
862
+ .map (Model ::getBuild )
863
+ .map (PluginContainer ::getPlugins )
864
+ .orElseGet (Collections ::emptyList );
842
865
plugins .addAll (PluginWrapper .addAll (utils .resolvePlugins (modelPlugins ), banMavenDefaults ));
843
866
}
844
867
845
868
private void getPluginManagementPlugins (List <PluginWrapper > plugins , Model model ) {
846
- List <Plugin > modelPlugins = model .getBuild ().getPluginManagement ().getPlugins ();
869
+ List <Plugin > modelPlugins = ofNullable (model )
870
+ .map (Model ::getBuild )
871
+ .map (PluginConfiguration ::getPluginManagement )
872
+ .map (PluginContainer ::getPlugins )
873
+ .orElseGet (Collections ::emptyList );
847
874
plugins .addAll (PluginWrapper .addAll (utils .resolvePlugins (modelPlugins ), banMavenDefaults ));
848
875
}
849
876
850
877
private void getReportingPlugins (List <PluginWrapper > plugins , Model model ) {
851
- List <ReportPlugin > modelReportPlugins = model .getReporting ().getPlugins ();
878
+ List <ReportPlugin > modelReportPlugins = ofNullable (model )
879
+ .map (ModelBase ::getReporting )
880
+ .map (Reporting ::getPlugins )
881
+ .orElseGet (Collections ::emptyList );
852
882
// add the reporting plugins
853
883
plugins .addAll (PluginWrapper .addAll (utils .resolveReportPlugins (modelReportPlugins ), banMavenDefaults ));
854
884
}
0 commit comments