2323import org .openrewrite .TreeVisitor ;
2424import org .openrewrite .maven .AddProperty ;
2525import org .openrewrite .maven .MavenIsoVisitor ;
26+ import org .openrewrite .maven .tree .MavenResolutionResult ;
2627import org .openrewrite .xml .XPathMatcher ;
2728import org .openrewrite .xml .tree .Xml ;
2829
@@ -48,6 +49,8 @@ public class UpdateMavenProjectPropertyJavaVersion extends Recipe {
4849 .map (property -> "/project/properties/" + property )
4950 .map (XPathMatcher ::new ).collect (Collectors .toList ());
5051
52+ private static final XPathMatcher PLUGINS_MATCHER = new XPathMatcher ("/project/build//plugins" );
53+
5154 @ Option (displayName = "Java version" ,
5255 description = "The Java version to upgrade to." ,
5356 example = "11" )
@@ -70,13 +73,14 @@ public String getDescription() {
7073 " * `maven.compiler.target`\n " +
7174 " * `maven.compiler.release`\n " +
7275 " * `release.version`\n \n " +
73- " These project properties are not added if they are not currently set, but only updated in place ." ;
76+ "If none of these properties are in use and the maven compiler plugin is not otherwise configured adds the `maven.compiler.release` property ." ;
7477 }
7578
7679 @ Override
7780 public TreeVisitor <?, ExecutionContext > getVisitor () {
7881 return new MavenIsoVisitor <ExecutionContext >() {
7982 final Set <String > propertiesExplicitlyReferenced = new HashSet <>();
83+ boolean compilerPluginConfiguredExplicitly ;
8084
8185 @ Override
8286 public Xml .Document visitDocument (Xml .Document document , ExecutionContext ctx ) {
@@ -87,34 +91,52 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
8791 Optional <String > pathToLocalParent = d .getRoot ().getChild ("parent" )
8892 .flatMap (parent -> parent .getChild ("relativePath" ))
8993 .flatMap (Xml .Tag ::getValue );
90- if (pathToLocalParent .isPresent ()) {
94+ if (pathToLocalParent .isPresent ()) {
9195 return d ;
9296 }
9397
9498 // Otherwise override remote parent's properties locally
95- Map <String , String > currentProperties = getResolutionResult ().getPom ().getRequested ().getProperties ();
99+ MavenResolutionResult mrr = getResolutionResult ();
100+ Map <String , String > currentProperties = mrr .getPom ().getRequested ().getProperties ();
96101 for (String property : JAVA_VERSION_PROPERTIES ) {
97- if (currentProperties .containsKey (property ) || !propertiesExplicitlyReferenced .contains (property )) {
102+ if (currentProperties .containsKey (property ) || !propertiesExplicitlyReferenced .contains (property )) {
98103 continue ;
99104 }
100105 d = (Xml .Document ) new AddProperty (property , String .valueOf (version ), null , false )
101106 .getVisitor ()
102107 .visitNonNull (d , ctx );
103108 }
104109
110+ // When none of the relevant properties are explicitly configured Maven defaults to Java 8
111+ // The release option was added in 9
112+ // If no properties have yet been updated then set release explicitly
113+ if (version >= 9 &&
114+ !compilerPluginConfiguredExplicitly &&
115+ currentProperties .keySet ()
116+ .stream ()
117+ .noneMatch (JAVA_VERSION_PROPERTIES ::contains )) {
118+ d = (Xml .Document ) new AddProperty ("maven.compiler.release" , String .valueOf (version ), null , false )
119+ .getVisitor ()
120+ .visitNonNull (d , ctx );
121+ HashMap <String , String > updatedProps = new HashMap <>(currentProperties );
122+ updatedProps .put ("maven.compiler.release" , version .toString ());
123+ mrr = mrr .withPom (mrr .getPom ().withRequested (mrr .getPom ().getRequested ().withProperties (updatedProps )));
124+
125+ d = d .withMarkers (d .getMarkers ().setByType (mrr ));
126+ }
105127 return d ;
106128 }
107129
108130 @ Override
109131 public Xml .Tag visitTag (Xml .Tag tag , ExecutionContext ctx ) {
110- tag = super .visitTag (tag , ctx );
111- Optional <String > s = tag .getValue ()
132+ Xml . Tag t = super .visitTag (tag , ctx );
133+ Optional <String > s = t .getValue ()
112134 .map (it -> it .replace ("${" , "" ).replace ("}" , "" ).trim ())
113135 .filter (JAVA_VERSION_PROPERTIES ::contains );
114- if (s .isPresent ()) {
136+ if (s .isPresent ()) {
115137 propertiesExplicitlyReferenced .add (s .get ());
116138 } else if (JAVA_VERSION_XPATH_MATCHERS .stream ().anyMatch (matcher -> matcher .matches (getCursor ()))) {
117- Optional <Float > maybeVersion = tag .getValue ().flatMap (
139+ Optional <Float > maybeVersion = t .getValue ().flatMap (
118140 value -> {
119141 try {
120142 return Optional .of (Float .parseFloat (value ));
@@ -125,16 +147,36 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
125147 );
126148
127149 if (!maybeVersion .isPresent ()) {
128- return tag ;
150+ return t ;
129151 }
130152 float currentVersion = maybeVersion .get ();
131153 if (currentVersion >= version ) {
132- return tag ;
154+ return t ;
155+ }
156+ return t .withValue (String .valueOf (version ));
157+ } else if (PLUGINS_MATCHER .matches (getCursor ())) {
158+ Optional <Xml .Tag > maybeCompilerPlugin = t .getChildren ().stream ()
159+ .filter (plugin ->
160+ "plugin" .equals (plugin .getName ()) &&
161+ "org.apache.maven.plugins" .equals (plugin .getChildValue ("groupId" ).orElse ("org.apache.maven.plugins" )) &&
162+ "maven-compiler-plugin" .equals (plugin .getChildValue ("artifactId" ).orElse (null )))
163+ .findAny ();
164+ Optional <Xml .Tag > maybeCompilerPluginConfig = maybeCompilerPlugin
165+ .flatMap (it -> it .getChild ("configuration" ));
166+ if (!maybeCompilerPluginConfig .isPresent ()) {
167+ return t ;
168+ }
169+ Xml .Tag compilerPluginConfig = maybeCompilerPluginConfig .get ();
170+ Optional <String > source = compilerPluginConfig .getChildValue ("source" );
171+ Optional <String > target = compilerPluginConfig .getChildValue ("target" );
172+ Optional <String > release = compilerPluginConfig .getChildValue ("release" );
173+ if (source .isPresent ()
174+ || target .isPresent ()
175+ || release .isPresent ()) {
176+ compilerPluginConfiguredExplicitly = true ;
133177 }
134- return tag .withValue (String .valueOf (version ));
135178 }
136-
137- return tag ;
179+ return t ;
138180 }
139181 };
140182 }
0 commit comments