Skip to content

resolvePropertiesInYaml does not respect Maven user properties (-D...) #1039

@famod

Description

@famod

What version of OpenRewrite are you using?

Maven plugin v6.18.0

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project.

<plugin>
  <groupId>org.openrewrite.maven</groupId>
  <artifactId>rewrite-maven-plugin</artifactId>
  <version>6.18.0</version>
  <configuration>
    <failOnInvalidActiveRecipes>true</failOnInvalidActiveRecipes>
  </configuration>
</plugin>

What is the smallest, simplest way to reproduce the problem?

Command line:

mvn rewrite:runNoFork -Drewrite.activeRecipes=Example -DnewValue=new

rewrite.yml:

type: specs.openrewrite.org/v1beta/recipe
name: Example
displayName: Example
recipeList:
  - org.openrewrite.maven.AddProperty:
      key: someProperty
      value: ${newValue}

What did you expect to see?

    <properties>
        <someProperty>new</someProperty>
    </properties>

What did you see instead?

    <properties>
        <someProperty>${newValue}</someProperty>
    </properties>

What is the full stack trace of any errors you encountered?

n/a

Are you interested in contributing a fix to OpenRewrite?

Yes, I already have a local fix but I have yet to take a look at how to write a test for it.

Patch:

diff --git a/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java b/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java
index 567a5ac..60ecc84 100644
--- a/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java
+++ b/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java
@@ -40,6 +40,7 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.*;
+import java.util.Map.Entry;
 
 import static java.util.Collections.sort;
 
@@ -107,7 +108,20 @@ public abstract class AbstractRewriteMojo extends ConfigurableRewriteMojo {
     }
 
     protected Environment environment(@Nullable ClassLoader recipeClassLoader) throws MojoExecutionException {
-        Properties propertiesToResolve = resolvePropertiesInYaml ? project.getProperties() : new Properties();
+        Properties propertiesToResolve;
+        if (resolvePropertiesInYaml) {
+            Properties userProperties = mavenSession.getUserProperties();
+            if (userProperties.isEmpty()) {
+                propertiesToResolve = project.getProperties();
+            } else {
+                propertiesToResolve = new Properties(project.getProperties());
+                for (Entry<Object, Object> entry : userProperties.entrySet()) {
+                    propertiesToResolve.put(entry.getKey(), entry.getValue());
+                }
+            }
+        } else {
+            propertiesToResolve = new Properties();
+        }
         Environment.Builder env = Environment.builder(propertiesToResolve);
         if (recipeClassLoader == null) {
             env.scanRuntimeClasspath()

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions