76
76
import org .codehaus .plexus .component .configurator .expression .ExpressionEvaluationException ;
77
77
import org .codehaus .plexus .component .configurator .expression .ExpressionEvaluator ;
78
78
import org .codehaus .plexus .util .IOUtil ;
79
- import org .codehaus .plexus .util .ReaderFactory ;
80
79
import org .codehaus .plexus .util .StringUtils ;
80
+ import org .codehaus .plexus .util .xml .XmlStreamReader ;
81
81
import org .codehaus .plexus .util .xml .pull .XmlPullParserException ;
82
82
import org .codehaus .stax2 .XMLInputFactory2 ;
83
83
90
90
* @author Stephen Connolly
91
91
* @since 1.0-alpha-3
92
92
*/
93
- public class PomHelper {
93
+ public final class PomHelper {
94
94
public static final String APACHE_MAVEN_PLUGINS_GROUPID = "org.apache.maven.plugins" ;
95
95
96
+ public static final Pattern PATTERN_PROJECT_PROPERTIES = Pattern .compile ("/project/properties" );
97
+
98
+ public static final Pattern PATTERN_PROJECT_PROFILE = Pattern .compile ("/project/profiles/profile" );
99
+
100
+ public static final Pattern PATTERN_PROJECT_PROFILE_ID = Pattern .compile ("/project/profiles/profile/id" );
101
+
102
+ public static final Pattern PATTERN_PROJECT_VERSION = Pattern .compile ("/project/version" );
103
+
104
+ public static final Pattern PATTERN_PROJECT_PARENT_VERSION = Pattern .compile ("/project/parent/version" );
105
+
106
+ public static final Pattern PATTERN_PROJECT_DEPENDENCY = Pattern .compile ("/project" + "(/profiles/profile)?"
107
+ + "((/dependencyManagement)|(/build(/pluginManagement)?/plugins/plugin))?"
108
+ + "/dependencies/dependency" );
109
+
110
+ public static final Pattern PATTERN_PROJECT_DEPENDENCY_VERSION = Pattern .compile ("/project" + "(/profiles/profile)?"
111
+ + "((/dependencyManagement)|(/build(/pluginManagement)?/plugins/plugin))?"
112
+ + "/dependencies/dependency"
113
+ + "((/groupId)|(/artifactId)|(/version))" );
114
+
115
+ public static final Pattern PATTERN_PROJECT_PLUGIN = Pattern .compile (
116
+ "/project" + "(/profiles/profile)?" + "((/build(/pluginManagement)?)|(/reporting))/plugins/plugin" );
117
+
118
+ public static final Pattern PATTERN_PROJECT_PLUGIN_VERSION = Pattern .compile ("/project" + "(/profiles/profile)?"
119
+ + "((/build(/pluginManagement)?)|(/reporting))/plugins/plugin"
120
+ + "((/groupId)|(/artifactId)|(/version))" );
121
+
122
+ private PomHelper () {
123
+ // utility class
124
+ }
125
+
96
126
/**
97
127
* Gets the raw model before any interpolation what-so-ever.
98
128
*
@@ -173,12 +203,12 @@ public static boolean setPropertyVersion(
173
203
boolean madeReplacement = false ;
174
204
if (profileId == null ) {
175
205
propertyRegex = Pattern .compile ("/project/properties/" + RegexUtils .quote (property ));
176
- matchScopeRegex = Pattern . compile ( "/project/properties" ) ;
206
+ matchScopeRegex = PATTERN_PROJECT_PROPERTIES ;
177
207
projectProfileId = null ;
178
208
} else {
179
209
propertyRegex = Pattern .compile ("/project/profiles/profile/properties/" + RegexUtils .quote (property ));
180
- matchScopeRegex = Pattern . compile ( "/project/profiles/profile" ) ;
181
- projectProfileId = Pattern . compile ( "/project/profiles/profile/id" ) ;
210
+ matchScopeRegex = PATTERN_PROJECT_PROFILE ;
211
+ projectProfileId = PATTERN_PROJECT_PROFILE_ID ;
182
212
}
183
213
184
214
pom .rewind ();
@@ -362,7 +392,6 @@ private void replaceValueInParent() {
362
392
public static String getProjectVersion (final ModifiedPomXMLEventReader pom ) throws XMLStreamException {
363
393
Stack <String > stack = new Stack <>();
364
394
String path = "" ;
365
- final Pattern matchScopeRegex = Pattern .compile ("/project/version" );
366
395
367
396
pom .rewind ();
368
397
@@ -372,12 +401,12 @@ public static String getProjectVersion(final ModifiedPomXMLEventReader pom) thro
372
401
stack .push (path );
373
402
path = path + "/" + event .asStartElement ().getName ().getLocalPart ();
374
403
375
- if (matchScopeRegex .matcher (path ).matches ()) {
404
+ if (PATTERN_PROJECT_VERSION .matcher (path ).matches ()) {
376
405
pom .mark (0 );
377
406
}
378
407
}
379
408
if (event .isEndElement ()) {
380
- if (matchScopeRegex .matcher (path ).matches ()) {
409
+ if (PATTERN_PROJECT_VERSION .matcher (path ).matches ()) {
381
410
pom .mark (1 );
382
411
if (pom .hasMark (0 ) && pom .hasMark (1 )) {
383
412
return pom .getBetween (0 , 1 ).trim ();
@@ -403,9 +432,7 @@ public static boolean setProjectParentVersion(final ModifiedPomXMLEventReader po
403
432
throws XMLStreamException {
404
433
Stack <String > stack = new Stack <>();
405
434
String path = "" ;
406
- final Pattern matchScopeRegex ;
407
435
boolean madeReplacement = false ;
408
- matchScopeRegex = Pattern .compile ("/project/parent/version" );
409
436
410
437
pom .rewind ();
411
438
@@ -415,12 +442,12 @@ public static boolean setProjectParentVersion(final ModifiedPomXMLEventReader po
415
442
stack .push (path );
416
443
path = path + "/" + event .asStartElement ().getName ().getLocalPart ();
417
444
418
- if (matchScopeRegex .matcher (path ).matches ()) {
445
+ if (PATTERN_PROJECT_PARENT_VERSION .matcher (path ).matches ()) {
419
446
pom .mark (0 );
420
447
}
421
448
}
422
449
if (event .isEndElement ()) {
423
- if (matchScopeRegex .matcher (path ).matches ()) {
450
+ if (PATTERN_PROJECT_PARENT_VERSION .matcher (path ).matches ()) {
424
451
pom .mark (1 );
425
452
if (pom .hasMark (0 ) && pom .hasMark (1 )) {
426
453
pom .replaceBetween (0 , 1 , value );
@@ -514,15 +541,6 @@ public static boolean setDependencyVersion(
514
541
boolean haveArtifactId = false ;
515
542
boolean haveOldVersion = false ;
516
543
517
- final Pattern matchScopeRegex = Pattern .compile ("/project" + "(/profiles/profile)?"
518
- + "((/dependencyManagement)|(/build(/pluginManagement)?/plugins/plugin))?"
519
- + "/dependencies/dependency" );
520
-
521
- final Pattern matchTargetRegex = Pattern .compile ("/project" + "(/profiles/profile)?"
522
- + "((/dependencyManagement)|(/build(/pluginManagement)?/plugins/plugin))?"
523
- + "/dependencies/dependency"
524
- + "((/groupId)|(/artifactId)|(/version))" );
525
-
526
544
pom .rewind ();
527
545
528
546
while (pom .hasNext ()) {
@@ -532,7 +550,7 @@ public static boolean setDependencyVersion(
532
550
final String elementName = event .asStartElement ().getName ().getLocalPart ();
533
551
path = path + "/" + elementName ;
534
552
535
- if (matchScopeRegex .matcher (path ).matches ()) {
553
+ if (PATTERN_PROJECT_DEPENDENCY .matcher (path ).matches ()) {
536
554
// we're in a new match scope
537
555
// reset any previous partial matches
538
556
inMatchScope = true ;
@@ -542,7 +560,8 @@ public static boolean setDependencyVersion(
542
560
haveGroupId = false ;
543
561
haveArtifactId = false ;
544
562
haveOldVersion = false ;
545
- } else if (inMatchScope && matchTargetRegex .matcher (path ).matches ()) {
563
+ } else if (inMatchScope
564
+ && PATTERN_PROJECT_DEPENDENCY_VERSION .matcher (path ).matches ()) {
546
565
if ("groupId" .equals (elementName )) {
547
566
haveGroupId =
548
567
groupId .equals (evaluate (pom .getElementText ().trim (), implicitProperties ));
@@ -557,7 +576,7 @@ public static boolean setDependencyVersion(
557
576
}
558
577
}
559
578
if (event .isEndElement ()) {
560
- if (matchTargetRegex .matcher (path ).matches ()
579
+ if (PATTERN_PROJECT_DEPENDENCY_VERSION .matcher (path ).matches ()
561
580
&& "version" .equals (event .asEndElement ().getName ().getLocalPart ())) {
562
581
pom .mark (1 );
563
582
String compressedPomVersion =
@@ -570,7 +589,7 @@ public static boolean setDependencyVersion(
570
589
// fall back to string comparison
571
590
haveOldVersion = compressedOldVersion .equals (compressedPomVersion );
572
591
}
573
- } else if (matchScopeRegex .matcher (path ).matches ()) {
592
+ } else if (PATTERN_PROJECT_DEPENDENCY .matcher (path ).matches ()) {
574
593
if (inMatchScope
575
594
&& pom .hasMark (0 )
576
595
&& pom .hasMark (1 )
@@ -720,22 +739,13 @@ public static boolean setPluginVersion(
720
739
throws XMLStreamException {
721
740
Stack <String > stack = new Stack <>();
722
741
String path = "" ;
723
- final Pattern matchScopeRegex ;
724
- final Pattern matchTargetRegex ;
725
742
boolean inMatchScope = false ;
726
743
boolean madeReplacement = false ;
727
744
boolean haveGroupId = false ;
728
745
boolean needGroupId = groupId != null && !APACHE_MAVEN_PLUGINS_GROUPID .equals (groupId );
729
746
boolean haveArtifactId = false ;
730
747
boolean haveOldVersion = false ;
731
748
732
- matchScopeRegex = Pattern .compile (
733
- "/project" + "(/profiles/profile)?" + "((/build(/pluginManagement)?)|(/reporting))/plugins/plugin" );
734
-
735
- matchTargetRegex = Pattern .compile ("/project" + "(/profiles/profile)?"
736
- + "((/build(/pluginManagement)?)|(/reporting))/plugins/plugin"
737
- + "((/groupId)|(/artifactId)|(/version))" );
738
-
739
749
pom .rewind ();
740
750
741
751
while (pom .hasNext ()) {
@@ -745,7 +755,7 @@ public static boolean setPluginVersion(
745
755
final String elementName = event .asStartElement ().getName ().getLocalPart ();
746
756
path = path + "/" + elementName ;
747
757
748
- if (matchScopeRegex .matcher (path ).matches ()) {
758
+ if (PATTERN_PROJECT_PLUGIN .matcher (path ).matches ()) {
749
759
// we're in a new match scope
750
760
// reset any previous partial matches
751
761
inMatchScope = true ;
@@ -755,7 +765,8 @@ public static boolean setPluginVersion(
755
765
haveGroupId = false ;
756
766
haveArtifactId = false ;
757
767
haveOldVersion = false ;
758
- } else if (inMatchScope && matchTargetRegex .matcher (path ).matches ()) {
768
+ } else if (inMatchScope
769
+ && PATTERN_PROJECT_PLUGIN_VERSION .matcher (path ).matches ()) {
759
770
if ("groupId" .equals (elementName )) {
760
771
haveGroupId = pom .getElementText ().trim ().equals (groupId );
761
772
path = stack .pop ();
@@ -768,7 +779,7 @@ public static boolean setPluginVersion(
768
779
}
769
780
}
770
781
if (event .isEndElement ()) {
771
- if (matchTargetRegex .matcher (path ).matches ()
782
+ if (PATTERN_PROJECT_PLUGIN_VERSION .matcher (path ).matches ()
772
783
&& "version" .equals (event .asEndElement ().getName ().getLocalPart ())) {
773
784
pom .mark (1 );
774
785
@@ -779,7 +790,7 @@ public static boolean setPluginVersion(
779
790
// fall back to string comparison
780
791
haveOldVersion = oldVersion .equals (pom .getBetween (0 , 1 ).trim ());
781
792
}
782
- } else if (matchScopeRegex .matcher (path ).matches ()) {
793
+ } else if (PATTERN_PROJECT_PLUGIN .matcher (path ).matches ()) {
783
794
if (inMatchScope
784
795
&& pom .hasMark (0 )
785
796
&& pom .hasMark (1 )
@@ -1524,7 +1535,7 @@ public static int getReactorParentCount(Map<File, Model> reactor, Model model) {
1524
1535
* @throws java.io.IOException when things go wrong.
1525
1536
*/
1526
1537
public static StringBuilder readXmlFile (File outFile ) throws IOException {
1527
- try (Reader reader = ReaderFactory . newXmlReader (outFile )) {
1538
+ try (Reader reader = new XmlStreamReader (outFile )) {
1528
1539
return new StringBuilder (IOUtil .toString (reader ));
1529
1540
}
1530
1541
}
0 commit comments