2
2
3
3
import java .util .Optional ;
4
4
5
+ import lombok .Data ;
6
+ import lombok .EqualsAndHashCode ;
7
+ import org .jetbrains .annotations .NotNull ;
5
8
import org .openrewrite .ExecutionContext ;
6
- import org .openrewrite .maven .AddProperty ;
9
+ import org .openrewrite .Recipe ;
10
+ import org .openrewrite .TreeVisitor ;
7
11
import org .openrewrite .maven .ChangePropertyValue ;
8
12
import org .openrewrite .maven .MavenVisitor ;
9
13
import org .openrewrite .maven .RemoveProperty ;
10
14
import org .openrewrite .xml .AddToTagVisitor ;
11
15
import org .openrewrite .xml .ChangeTagValueVisitor ;
12
16
import org .openrewrite .xml .XPathMatcher ;
13
- import org .openrewrite .xml .format .AutoFormat ;
14
17
import org .openrewrite .xml .tree .Xml ;
15
18
16
19
import static org .openrewrite .xml .AddToTagVisitor .addToTag ;
17
20
18
- public class ChangeMavenCompilerPluginConfiguration extends MavenVisitor <ExecutionContext > {
21
+ @ Data
22
+ @ EqualsAndHashCode (callSuper = true )
23
+ public class ChangeMavenCompilerPluginConfiguration extends Recipe {
19
24
20
25
private static final String GROUP_ID = "org.apache.maven.plugins" ;
21
26
22
27
private static final String ARTIFACT_ID = "maven-compiler-plugin" ;
23
28
24
- private static final XPathMatcher PLUGINS_MATCHER = new XPathMatcher ("/project/build/plugins" );
29
+ private static final XPathMatcher PLUGIN_MATCHER = new XPathMatcher ("/project/build/plugins" );
25
30
26
31
private static final String MAVEN_COMPILER_SOURCE = "${maven.compiler.source}" ;
27
32
@@ -30,69 +35,84 @@ public class ChangeMavenCompilerPluginConfiguration extends MavenVisitor<Executi
30
35
private static final String JAVA_VERSION = "java.version" ;
31
36
32
37
@ Override
33
- public Xml visitTag (Xml .Tag tag , ExecutionContext ctx ) {
34
- Xml .Tag plugins = (Xml .Tag ) super .visitTag (tag , ctx );
35
- if (!PLUGINS_MATCHER .matches (getCursor ())) {
36
- return plugins ;
37
- }
38
- Optional <Xml .Tag > maybePlugin = plugins .getChildren ().stream ()
39
- .filter (plugin -> "plugin" .equals (plugin .getName ())
40
- && GROUP_ID .equals (plugin .getChildValue ("groupId" ).orElse (null ))
41
- && ARTIFACT_ID .equals (plugin .getChildValue ("artifactId" ).orElse (null )))
42
- .findAny ();
43
- if (maybePlugin .isEmpty ()) {
44
- return plugins ;
45
- }
46
-
47
- Xml .Tag plugin = maybePlugin .get ();
48
- Optional <Xml .Tag > maybeConfiguration = maybePlugin .get ().getChild ("configuration" );
49
- if (maybeConfiguration .isPresent ()) {
50
- Xml .Tag configuration = maybeConfiguration .get ();
51
-
52
- String sourceValue = configuration .getChildValue ("source" ).orElse (null );
53
- String targetValue = configuration .getChildValue ("target" ).orElse (null );
38
+ public @ NotNull String getDisplayName () {
39
+ return "null" ;
40
+ }
54
41
55
- if (sourceValue != null && targetValue != null ) {
56
- String sourceLookupValue = sourceValue .startsWith ("${" )
57
- ? super .getResolutionResult ().getPom ().getValue (sourceValue .trim ()) : sourceValue ;
42
+ @ Override
43
+ protected @ NotNull TreeVisitor <?, ExecutionContext > getVisitor () {
44
+ return new ChangeMavenPluginConfigurationVisitor ();
45
+ }
58
46
59
- String targetLookupValue = targetValue .startsWith ("${" )
60
- ? super .getResolutionResult ().getPom ().getValue (targetValue .trim ()) : targetValue ;
47
+ private static class ChangeMavenPluginConfigurationVisitor extends MavenVisitor <ExecutionContext > {
61
48
62
- // If source and target version are same
63
- if (sourceLookupValue != null && sourceLookupValue .equals (targetLookupValue )){
64
- doAfterVisit (new ChangePropertyValue (JAVA_VERSION , sourceLookupValue , true ));
65
- doAfterVisit (new ChangeTagValueVisitor <>(configuration .getChild ("source" ).get (), MAVEN_COMPILER_SOURCE ));
66
- doAfterVisit (new ChangeTagValueVisitor <>(configuration .getChild ("target" ).get (), MAVEN_COMPILER_TARGET ));
49
+ @ Override
50
+ public @ NotNull Xml visitTag (Xml .@ NotNull Tag tag , @ NotNull ExecutionContext ctx ) {
51
+ Xml .Tag plugins = (Xml .Tag ) super .visitTag (tag , ctx );
52
+ if (!PLUGIN_MATCHER .matches (getCursor ())) {
53
+ return plugins ;
54
+ }
55
+ Optional <Xml .Tag > maybePlugin = plugins .getChildren ().stream ()
56
+ .filter (plugin -> "plugin" .equals (plugin .getName ())
57
+ && GROUP_ID .equals (plugin .getChildValue ("groupId" ).orElse (null ))
58
+ && ARTIFACT_ID .equals (plugin .getChildValue ("artifactId" ).orElse (null )))
59
+ .findAny ();
60
+ if (maybePlugin .isEmpty ()) {
61
+ return plugins ;
62
+ }
67
63
68
- if (sourceValue .startsWith ("${" )) {
69
- doAfterVisit (new RemoveProperty (sourceValue .substring (2 , sourceValue .length () - 1 )));
70
- }
71
- if (targetValue .startsWith ("${" )){
72
- doAfterVisit (new RemoveProperty (targetValue .substring (2 , targetValue .length () - 1 )));
64
+ Xml .Tag plugin = maybePlugin .get ();
65
+ Optional <Xml .Tag > maybeConfiguration = maybePlugin .get ().getChild ("configuration" );
66
+ if (maybeConfiguration .isPresent ()) {
67
+ Xml .Tag configuration = maybeConfiguration .get ();
68
+
69
+ String sourceValue = configuration .getChildValue ("source" ).orElse (null );
70
+ String targetValue = configuration .getChildValue ("target" ).orElse (null );
71
+
72
+ if (sourceValue != null && targetValue != null ) {
73
+ String sourceLookupValue = sourceValue .startsWith ("${" )
74
+ ? super .getResolutionResult ().getPom ().getValue (sourceValue .trim ()) : sourceValue ;
75
+
76
+ String targetLookupValue = targetValue .startsWith ("${" )
77
+ ? super .getResolutionResult ().getPom ().getValue (targetValue .trim ()) : targetValue ;
78
+
79
+ // If source and target version are same
80
+ if (sourceLookupValue != null && sourceLookupValue .equals (targetLookupValue )) {
81
+ doAfterVisit (new ChangePropertyValue (JAVA_VERSION , sourceLookupValue , true ));
82
+ doAfterVisit (new ChangeTagValueVisitor <>(configuration .getChild ("source" ).get (),
83
+ MAVEN_COMPILER_SOURCE ));
84
+ doAfterVisit (new ChangeTagValueVisitor <>(configuration .getChild ("target" ).get (),
85
+ MAVEN_COMPILER_TARGET ));
86
+
87
+ if (sourceValue .startsWith ("${" )) {
88
+ doAfterVisit (new RemoveProperty (sourceValue .substring (2 , sourceValue .length () - 1 )));
89
+ }
90
+ if (targetValue .startsWith ("${" )) {
91
+ doAfterVisit (new RemoveProperty (targetValue .substring (2 , targetValue .length () - 1 )));
92
+ }
73
93
}
74
94
}
75
- }
76
95
77
- if (sourceValue == null ){
78
- addToTag (configuration ,
79
- Xml .Tag .build ("<source>" + MAVEN_COMPILER_SOURCE + "</source>\n " )
80
- .withPrefix ("\n " ),
81
- getCursor ());
82
- }
96
+ if (sourceValue == null ) {
97
+ addToTag (configuration ,
98
+ Xml .Tag .build ("<source>" + MAVEN_COMPILER_SOURCE + "</source>\n " ).withPrefix ("\n " ),
99
+ getCursor ());
100
+ }
83
101
84
- if (targetValue == null ){
85
- addToTag (configuration ,
86
- Xml .Tag .build ("<target>" + MAVEN_COMPILER_TARGET + "</target>\n " )
87
- . withPrefix ( " \n " ),
88
- getCursor ());
102
+ if (targetValue == null ) {
103
+ addToTag (configuration ,
104
+ Xml .Tag .build ("<target>" + MAVEN_COMPILER_TARGET + "</target>\n " ). withPrefix ( " \n " ),
105
+ getCursor ());
106
+ }
89
107
}
108
+ else {
109
+ Xml .Tag configurations = Xml .Tag .build ("<configuration>\n <source>" + MAVEN_COMPILER_SOURCE
110
+ + "</source>\n <target>" + MAVEN_COMPILER_TARGET + "</target>\n </configuration>\n " );
111
+ doAfterVisit (new AddToTagVisitor <>(plugin , configurations ));
112
+ }
113
+ return plugins ;
90
114
}
91
- else {
92
- Xml .Tag configurations = Xml .Tag .build ("<configuration>\n <source>" + MAVEN_COMPILER_SOURCE
93
- + "</source>\n <target>" + MAVEN_COMPILER_TARGET + "</target>\n </configuration>\n " );
94
- doAfterVisit (new AddToTagVisitor <>(plugin , configurations ));
95
- }
96
- return plugins ;
115
+
97
116
}
117
+
98
118
}
0 commit comments