17
17
18
18
import lombok .EqualsAndHashCode ;
19
19
import lombok .Value ;
20
- import org .openrewrite .ExecutionContext ;
21
20
import org .openrewrite .Option ;
22
21
import org .openrewrite .Recipe ;
23
- import org .openrewrite .TreeVisitor ;
24
- import org .openrewrite .maven .AddProperty ;
25
- import org .openrewrite .maven .MavenIsoVisitor ;
26
- import org .openrewrite .xml .XPathMatcher ;
27
- import org .openrewrite .xml .tree .Xml ;
28
22
29
- import java .util .Arrays ;
30
23
import java .util .List ;
31
- import java .util .Map ;
32
- import java .util .stream .Collectors ;
33
24
25
+ import static java .util .Collections .singletonList ;
26
+
27
+ /**
28
+ * @deprecated in favor of {@link org.openrewrite.maven.UpdateMavenProjectPropertyJavaVersion}
29
+ */
34
30
@ Value
35
31
@ EqualsAndHashCode (callSuper = false )
32
+ @ Deprecated
36
33
public class UpdateMavenProjectPropertyJavaVersion extends Recipe {
37
34
38
- private static final List <String > JAVA_VERSION_PROPERTIES = Arrays .asList (
39
- "java.version" ,
40
- "jdk.version" ,
41
- "javaVersion" ,
42
- "jdkVersion" ,
43
- "maven.compiler.source" ,
44
- "maven.compiler.target" ,
45
- "maven.compiler.release" ,
46
- "release.version" );
47
-
48
- private static final List <XPathMatcher > JAVA_VERSION_XPATH_MATCHERS =
49
- JAVA_VERSION_PROPERTIES .stream ()
50
- .map (property -> "/project/properties/" + property )
51
- .map (XPathMatcher ::new ).collect (Collectors .toList ());
52
-
53
- private static final XPathMatcher PLUGINS_MATCHER = new XPathMatcher ("/project/build//plugins" );
54
-
55
35
@ Option (displayName = "Java version" ,
56
36
description = "The Java version to upgrade to." ,
57
37
example = "11" )
@@ -66,79 +46,19 @@ public String getDisplayName() {
66
46
public String getDescription () {
67
47
//language=markdown
68
48
return "The Java version is determined by several project properties, including:\n \n " +
69
- " * `java.version`\n " +
70
- " * `jdk.version`\n " +
71
- " * `javaVersion`\n " +
72
- " * `jdkVersion`\n " +
73
- " * `maven.compiler.source`\n " +
74
- " * `maven.compiler.target`\n " +
75
- " * `maven.compiler.release`\n " +
76
- " * `release.version`\n \n " +
77
- "If none of these properties are in use and the maven compiler plugin is not otherwise configured, adds the `maven.compiler.release` property." ;
49
+ " * `java.version`\n " +
50
+ " * `jdk.version`\n " +
51
+ " * `javaVersion`\n " +
52
+ " * `jdkVersion`\n " +
53
+ " * `maven.compiler.source`\n " +
54
+ " * `maven.compiler.target`\n " +
55
+ " * `maven.compiler.release`\n " +
56
+ " * `release.version`\n \n " +
57
+ "If none of these properties are in use and the maven compiler plugin is not otherwise configured, adds the `maven.compiler.release` property." ;
78
58
}
79
59
80
60
@ Override
81
- public TreeVisitor <?, ExecutionContext > getVisitor () {
82
- return new MavenIsoVisitor <ExecutionContext >() {
83
- boolean compilerPluginConfiguredExplicitly ;
84
-
85
- @ Override
86
- public Xml .Document visitDocument (Xml .Document document , ExecutionContext ctx ) {
87
- // Update properties already defined in the current pom
88
- Xml .Document d = super .visitDocument (document , ctx );
89
-
90
- // Return early if the parent appears to be within the current repository, as properties defined there will be updated
91
- if (getResolutionResult ().parentPomIsProjectPom ()) {
92
- return d ;
93
- }
94
-
95
- // Otherwise override remote parent's properties locally
96
- Map <String , String > currentProperties = getResolutionResult ().getPom ().getProperties ();
97
- boolean foundProperty = false ;
98
- for (String property : JAVA_VERSION_PROPERTIES ) {
99
- String propertyValue = currentProperties .get (property );
100
- if (propertyValue != null ) {
101
- foundProperty = true ;
102
- try {
103
- if (Float .parseFloat (propertyValue ) < version ) {
104
- d = (Xml .Document ) new AddProperty (property , String .valueOf (version ), null , false )
105
- .getVisitor ()
106
- .visitNonNull (d , ctx );
107
- maybeUpdateModel ();
108
- }
109
- } catch (NumberFormatException ex ) {
110
- // either an expression or something else, don't touch
111
- }
112
- }
113
- }
114
-
115
- // When none of the relevant properties are explicitly configured Maven defaults to Java 8
116
- // The release option was added in 9
117
- // If no properties have yet been updated then set release explicitly
118
- if (!foundProperty && version >= 9 && !compilerPluginConfiguredExplicitly ) {
119
- d = (Xml .Document ) new AddProperty ("maven.compiler.release" , String .valueOf (version ), null , false )
120
- .getVisitor ()
121
- .visitNonNull (d , ctx );
122
- maybeUpdateModel ();
123
- }
124
-
125
- return d ;
126
- }
127
-
128
- @ Override
129
- public Xml .Tag visitTag (Xml .Tag tag , ExecutionContext ctx ) {
130
- Xml .Tag t = super .visitTag (tag , ctx );
131
- if (isPluginTag ("org.apache.maven.plugins" , "maven-compiler-plugin" )) {
132
- t .getChild ("configuration" ).ifPresent (compilerPluginConfig -> {
133
- if (compilerPluginConfig .getChildValue ("source" ).isPresent () ||
134
- compilerPluginConfig .getChildValue ("target" ).isPresent () ||
135
- compilerPluginConfig .getChildValue ("release" ).isPresent ()) {
136
- compilerPluginConfiguredExplicitly = true ;
137
- }
138
- });
139
- }
140
- return t ;
141
- }
142
- };
61
+ public List <Recipe > getRecipeList () {
62
+ return singletonList (new org .openrewrite .maven .UpdateMavenProjectPropertyJavaVersion (version ));
143
63
}
144
64
}
0 commit comments