openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.15k stars 322 forks source link

Rewrite-maven: `AddDependency` Recipe and required value for `onlyIfUsing` parameter for Java projects #1767

Closed ferblaca closed 2 years ago

ferblaca commented 2 years ago

I am creating a recipe to add a dependency to a project based on the value of a configuration property.

However, adding the recipe AddDependency to the pipeline forces me to give value to the onlyOfUsing parameter to finally add the dependency, which as far as I can see, examines if the project uses certain Java classes (imports). By example:

this.doNext(new AddDependency("com.fasterxml.jackson.core", "jackson-core",
                "2.13.x", null, null, false,
                "java.util.List", null, null, false, null));

Does it make sense to make this parameter (onlyOfUsing) required?

As I indicated above, if a use case is simply to add a dependency, it would not be necessary in all cases to check for the use of certain Java classes, just to add the dependency.

I am using version 7.22.0

pway99 commented 2 years ago

HI @ferblaca,

There is another option besides relaxing the AddDependency recipe.

Check out the rewrite-migrate-java AddJaxbRuntime recipe. It leverages Recipe#visit for determining if the JAXB dependency should be added and then uses the AddDependencyVisitor for adding the dependency to the appropriate pom.

ferblaca commented 2 years ago

Hi @pway99 !!! thank you very much for responding!

I tried to add the dependency as you said using AddDependencyVisitor and avoid using the onlyIfUsing parameter. However, although it seems to work correctly, it doesn't end up adding the dependency in the pom.xml.

You can see the recipe I have created here.

Basically I look for a value in a yaml, and if I find it then I add a dependency:

        if (yamlEncValueFound.get()) {
            ListUtils.map(before, sourceFile -> {
                if (OpenRewriteUtils.isMavenSource(sourceFile)) {
                    // Add recipe to add the jasypt stater
                    final AddDependencyVisitor addDependencyVisitor =
                            new AddDependencyVisitor("org.jasypt", "jasypt", "1.9.3", null,
                                    Scope.Compile.name(), false, null, null, null, null);
                    final Xml visit = addDependencyVisitor.visit(sourceFile, ctx);
                    return (SourceFile) visit;
                }
                return sourceFile;
            });
        }

I've created a test to verify that it's not making the change in pom.xml here.

I've also tried running it in an end application and it doesn't make the change to pom.xml either.

Any ideas? thanks

pway99 commented 2 years ago

Hi @ferblaca, Thanks so much for the test project I am so glad were able to help here are a few suggestions. :)

In your recipe

RewriteTest was recently added to the framework and it makes testing multi-source configurations a breeze here is an example.

import com.ferblaca.openrewrite.recipes.AddJasyptDependencyMaven
import org.junit.jupiter.api.Test
import org.openrewrite.test.RewriteTest

class AddJasyptDependencyMavenTest : RewriteTest {

    @Test
    fun `modified pom adding jackson dependency When almost once value yml property starts with ENC`() = rewriteRun(
        { spec -> spec.recipe(AddJasyptDependencyMaven())},
        mavenProject("myproject",
            yaml(
                """
                  app:
                  common:
                    jasypt:
                      enabled: true
                      pass: pass
                      test:
                        password: ENC(CQiimDOpYhCBXZFC6DfCDNhRlewoKRY4)
                """
            ),
            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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
                    <modelVersion>4.0.0</modelVersion>
                    <parent>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-parent</artifactId>
                        <version>2.6.5</version>
                        <relativePath/>
                    </parent>
                    <groupId>com.example.openrewrite.test</groupId>
                    <artifactId>demoOpenrewrite</artifactId>
                    <version>0.0.1-SNAPSHOT</version>
                    <name>demoOpenRewrite</name>
                    <properties>
                        <java.version>11</java.version>
                    </properties>
                    <dependencies/>
                </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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
                    <modelVersion>4.0.0</modelVersion>
                    <parent>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-parent</artifactId>
                        <version>2.6.5</version>
                        <relativePath/>
                    </parent>
                    <groupId>com.example.openrewrite.test</groupId>
                    <artifactId>demoOpenrewrite</artifactId>
                    <version>0.0.1-SNAPSHOT</version>
                    <name>demoOpenRewrite</name>
                    <properties>
                        <java.version>11</java.version>
                    </properties>
                    <dependencies>
                        <dependency>
                            <groupId>org.jasypt</groupId>
                            <artifactId>jasypt</artifactId>
                            <version>1.9.3</version>
                        </dependency>
                    </dependencies>
                </project>
                """.trimIndent()
            )
        )
    )
}
ferblaca commented 2 years ago

Beginner's mistake... thank you very much for the clarification @pway99 !!!