openrewrite / rewrite-migrate-java

OpenRewrite recipes for migrating to newer versions of Java.
Apache License 2.0
98 stars 67 forks source link

Support Spring Boot apps with no explicit version config #155

Open nmck257 opened 1 year ago

nmck257 commented 1 year ago

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:

tkvangorder commented 1 year ago

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.

nmck257 commented 1 year ago

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?

tkvangorder commented 1 year ago

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.