rewrite-migrate-java icon indicating copy to clipboard operation
rewrite-migrate-java copied to clipboard

Support Spring Boot apps with no explicit version config

Open nmck257 opened this issue 2 years ago • 3 comments

test case:

    @Test
    fun `adds java version property if not present if spring-like parent`() = rewriteRun(
        pomXml("""
            <?xml version="1.0" encoding="UTF-8"?>
            <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
              <modelVersion>4.0.0</modelVersion>
              <groupId>org.sample</groupId>
              <artifactId>sample</artifactId>
              <version>1.0.0</version>
              
              <parent>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.7.0</version>
              </parent>
              
            </project>
        """.trimIndent(),
            """
            <?xml version="1.0" encoding="UTF-8"?>
            <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
              <modelVersion>4.0.0</modelVersion>
              <groupId>org.sample</groupId>
              <artifactId>sample</artifactId>
              <version>1.0.0</version>
              
              <parent>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.7.0</version>
              </parent>
              <properties>
                <java.version>11</java.version>
              </properties>
              
            </project>
        """.trimIndent()
        ) // fails with no change
    )

Spring Boot declares a java.version property, pre-wired into the maven-compiler-plugin config. This is a convenient, common pattern to manage a project's Java version, and our recipes support updating that property if present.

But, because Spring Boot provides a default value (1.8) for java.version, it's feasible for a Spring Boot app to have no explicit configuration for its Java version in its pom.xml. And in that case, the upgrade recipe will do everything but actually update the build's Java version. The test case above shows this.

The tricky part here is deciding how generic the solution should be -- eg:

  • do we just check for the "hardcoded" spring-boot-starter-parent parent, or identify arbitrary (non-project) parents which declare java.version
  • should we validate that project pom isn't already managing its java version a different way (ie no explicit maven-compiler-plugin config)
  • should we enforce an opinion that projects with spring-like parents should only use the java.version property to manage the Java version

nmck257 avatar Dec 16 '22 18:12 nmck257

I think maybe we could predicate this on if the java.version property exists in the resolved Maven model.

If java.version exists and java.version is less than the desired value, either add/update the property.

tkvangorder avatar Dec 19 '22 16:12 tkvangorder

Yes, but, I'm afraid that might spam java.version properties across multi-module projects -- can we easily check (within a visitor) whether a pom's parent is also a project file?

nmck257 avatar Dec 19 '22 20:12 nmck257

Good point, I think we would only want to add the java.version to poms where the parent is either not present or not also in the list of source files. I think this is doable.

tkvangorder avatar Dec 22 '22 19:12 tkvangorder