@@ -69,6 +69,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
6969 return tree ;
7070 }
7171
72+ // if we find more than one project pom, it has to be multi-module
7273 boolean isMultiModule = tree .getMarkers ()
7374 .findFirst (MavenResolutionResult .class )
7475 .map (MavenResolutionResult ::getProjectPoms )
@@ -79,70 +80,72 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
7980 // maybe add the plugin to //build/pluginManagement/plugins or //build/plugins is not present yet
8081 tree = new AddPluginVisitor (isMultiModule , MAVEN_COMPILER_PLUGIN_GROUP_ID , MAVEN_COMPILER_PLUGIN_ARTIFACT_ID , null , "<configuration><annotationProcessorPaths/></configuration>" , null , null , null ).visit (tree , ctx );
8182
82- return new AddAnnotationProcessorPath (isMultiModule ).visit (tree , ctx );
83- }
84-
85- class AddAnnotationProcessorPath extends MavenIsoVisitor <ExecutionContext > {
86- private final boolean addToManaged ;
83+ // configure the plugin adding process
84+ return new MavenIsoVisitor <ExecutionContext >() {
85+ @ Override
86+ public Xml .Tag visitTag (Xml .Tag tag , ExecutionContext ctx ) {
87+ Xml .Tag plugins = super .visitTag (tag , ctx );
88+ plugins = (Xml .Tag ) new MavenPlugin .Matcher (isMultiModule , MAVEN_COMPILER_PLUGIN_GROUP_ID , MAVEN_COMPILER_PLUGIN_ARTIFACT_ID ).asVisitor (plugin -> {
89+
90+ MavenResolutionResult mrr = getResolutionResult ();
91+ AtomicReference <TreeVisitor <?, ExecutionContext >> maybePropertyUpdate = new AtomicReference <>();
92+ Xml .Tag modifiedPlugin = new XmlIsoVisitor <ExecutionContext >() {
93+ @ Override
94+ public Xml .Tag visitTag (Xml .Tag tag , ExecutionContext ctx ) {
95+ Xml .Tag tg = super .visitTag (tag , ctx );
96+
97+ if (!"annotationProcessorPaths" .equals (tg .getName ())) {
98+ return tg ;
99+ }
87100
88- AddAnnotationProcessorPath (boolean addToManaged ) {
89- this .addToManaged = addToManaged ;
90- }
101+ // iterate the children (annotation processor paths) and try to update the version
102+ for (int i = 0 ; i < tg .getChildren ().size (); i ++) {
103+ Xml .Tag child = tg .getChildren ().get (i );
104+ if (!groupId .equals (child .getChildValue ("groupId" ).orElse (null )) ||
105+ !artifactId .equals (child .getChildValue ("artifactId" ).orElse (null ))) {
106+ continue ;
107+ }
91108
92- @ Override
93- public Xml .Tag visitTag (Xml .Tag tag , ExecutionContext ctx ) {
94- Xml .Tag plugins = super .visitTag (tag , ctx );
95- plugins = (Xml .Tag ) new MavenPlugin .Matcher (addToManaged ,MAVEN_COMPILER_PLUGIN_GROUP_ID , MAVEN_COMPILER_PLUGIN_ARTIFACT_ID ).asVisitor (plugin -> {
96-
97- MavenResolutionResult mrr = getResolutionResult ();
98- AtomicReference <TreeVisitor <?, ExecutionContext >> afterVisitor = new AtomicReference <>();
99- Xml .Tag modifiedPlugin = new XmlIsoVisitor <ExecutionContext >() {
100- @ Override
101- public Xml .Tag visitTag (Xml .Tag tag , ExecutionContext ctx ) {
102- Xml .Tag tg = super .visitTag (tag , ctx );
103- if (!"annotationProcessorPaths" .equals (tg .getName ())) {
104- return tg ;
105- }
106- for (int i = 0 ; i < tg .getChildren ().size (); i ++) {
107- Xml .Tag child = tg .getChildren ().get (i );
108- if (!groupId .equals (child .getChildValue ("groupId" ).orElse (null )) ||
109- !artifactId .equals (child .getChildValue ("artifactId" ).orElse (null ))) {
110- continue ;
111- }
112- if (!version .equals (child .getChildValue ("version" ).orElse (null ))) {
113- String oldVersion = child .getChildValue ("version" ).orElse ("" );
114- boolean oldVersionUsesProperty = oldVersion .startsWith ("${" );
115- String lookupVersion = oldVersionUsesProperty ?
116- mrr .getPom ().getValue (oldVersion .trim ()) :
117- oldVersion ;
118- VersionComparator comparator = Semver .validate (lookupVersion , null ).getValue ();
119- if (comparator .compare (version , lookupVersion ) > 0 ) {
120- if (oldVersionUsesProperty ) {
121- afterVisitor .set (new ChangePropertyValue (oldVersion , version , null , null ).getVisitor ());
122- } else {
123- List <Xml .Tag > tags = tg .getChildren ();
124- tags .set (i , child .withChildValue ("version" , version ));
125- return tg .withContent (tags );
109+ if (!version .equals (child .getChildValue ("version" ).orElse (null ))) {
110+ String oldVersion = child .getChildValue ("version" ).orElse ("" );
111+ boolean oldVersionUsesProperty = oldVersion .startsWith ("${" );
112+ String lookupVersion = oldVersionUsesProperty ? mrr .getPom ().getValue (oldVersion .trim ()) : oldVersion ;
113+ VersionComparator comparator = Semver .validate (lookupVersion , null ).getValue ();
114+ if (comparator .compare (version , lookupVersion ) > 0 ) {
115+ if (oldVersionUsesProperty ) {
116+ // a maven property is used here, update in properties section later
117+ maybePropertyUpdate .set (new ChangePropertyValue (oldVersion , version , null , null ).getVisitor ());
118+ } else {
119+ // update the paths version directly
120+ List <Xml .Tag > tags = tg .getChildren ();
121+ tags .set (i , child .withChildValue ("version" , version ));
122+ return tg .withContent (tags );
123+ }
126124 }
127125 }
126+
127+ return tg ;
128128 }
129- return tg ;
129+
130+ // not found so we add it
131+ return tg .withContent (ListUtils .concat (tg .getChildren (), Xml .Tag .build (String .format (
132+ "<path>\n <groupId>%s</groupId>\n <artifactId>%s</artifactId>\n <version>%s</version>\n </path>" ,
133+ groupId , artifactId , version ))));
130134 }
131- return tg .withContent (ListUtils .concat (tg .getChildren (), Xml .Tag .build (String .format (
132- "<path>\n <groupId>%s</groupId>\n <artifactId>%s</artifactId>\n <version>%s</version>\n </path>" ,
133- groupId , artifactId , version ))));
135+ }.visitTag (plugin .getTree (), ctx );
136+
137+ if (maybePropertyUpdate .get () != null ) {
138+ doAfterVisit (maybePropertyUpdate .get ());
134139 }
135- }.visitTag (plugin .getTree (), ctx );
136- if (afterVisitor .get () != null ) {
137- doAfterVisit (afterVisitor .get ());
140+
141+ return modifiedPlugin ;
142+ }).visitNonNull (plugins , 0 );
143+ if (plugins != tag ) {
144+ plugins = autoFormat (plugins , ctx );
138145 }
139- return modifiedPlugin ;
140- }).visitNonNull (plugins , 0 );
141- if (plugins != tag ) {
142- plugins = autoFormat (plugins , ctx );
146+ return plugins ;
143147 }
144- return plugins ;
145- }
148+ }.visit (tree , ctx );
146149 }
147150 };
148151 }
0 commit comments