Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give explicit property definitions higher precedence than Maven's implicit ones. #4334

Merged
merged 4 commits into from
Jul 17, 2024

Conversation

nguyenhoan
Copy link
Contributor

What's changed?

Exclude Maven built-in property from circular placeholder reference check

What's your motivation?

Error Circular placeholder reference 'XYZ' in property definitions is thrown when a built-in property, such as project.version, is redefined in the root POM and used in a child in multi-module projects.
I would not expect running recipes to fail given that mvn clean verify succeeds.

Attached is a minimum project to reproduce the issue.
Circular-placeholder-reference.zip
You can run any recipe to trigger it. I ran the one for SpringBoot

mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-spring:RELEASE -Drewrite.activeRecipes=org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_2 

Anything in particular you'd like reviewers to focus on?

Anyone you would like to review specifically?

@timtebeek

Have you considered any alternatives or workarounds?

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • rewrite-core/src/test/java/org/openrewrite/RecipeListTest.java
    • lines 35-35
    • lines 66-69
  • rewrite-maven/src/test/java/org/openrewrite/maven/trait/MavenPluginTest.java
    • lines 26-26

Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick work here! I'd expected a unit test using RewriteTest that shows we can now handle a parent and child that use such properties, perhaps very similar to what you already have in your zip file. I can explore that if you don't get to it for a more thorough review later.

@@ -15,6 +15,7 @@
*/
package org.openrewrite.internal;

import org.openrewrite.internal.lang.NonNull;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import org.openrewrite.internal.lang.NonNull;

@@ -160,4 +170,13 @@ private static boolean substringMatch(CharSequence str, int index, CharSequence
}
return true;
}

private static boolean isMavenBuiltInProperty(@NonNull final String originalPlaceholder) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've already annotated the package; that means we likely don't have to repeat ourselves here.

@NonNullApi
package org.openrewrite.internal;
import org.openrewrite.internal.lang.NonNullApi;

Suggested change
private static boolean isMavenBuiltInProperty(@NonNull final String originalPlaceholder) {
private static boolean isMavenBuiltInProperty(final String originalPlaceholder) {

@timtebeek timtebeek added the bug Something isn't working label Jul 16, 2024
@nguyenhoan
Copy link
Contributor Author

That does not sound a like unit test for the changed method. If I wanted to add a unit test, which test class would I add it to?

@timtebeek
Copy link
Contributor

I'd say MavenParserTest is the best place to guarantee that your issue is fixed, and remains fixed with a test similar to this one.

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/4093")
void circularImportDependency() {
rewriteRun(
mavenProject("root",
pomXml(
"""
<project>
<groupId>com.example</groupId>
<artifactId>circular-example-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>circular-example-child</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>circular-example-child</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
""",
spec -> spec.afterRecipe(pomXml -> {
MavenResolutionResult resolution = pomXml.getMarkers().findFirst(MavenResolutionResult.class).orElseThrow();
GroupArtifactVersion gav = resolution.getPom().getDependencyManagement().get(0).getGav();
assertThat(gav.getGroupId()).isEqualTo("junit");
assertThat(gav.getArtifactId()).isEqualTo("junit");
assertThat(gav.getVersion()).isEqualTo("4.13.2");
})
),
mavenProject("circular-example-child",
pomXml(
"""
<project>
<parent>
<groupId>com.example</groupId>
<artifactId>circular-example-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>circular-example-child</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
""",
spec -> spec.afterRecipe(pomXml -> {
MavenResolutionResult resolution = pomXml.getMarkers().findFirst(MavenResolutionResult.class).orElseThrow();
GroupArtifactVersion gav = resolution.getPom().getDependencyManagement().get(0).getGav();
assertThat(gav.getGroupId()).isEqualTo("junit");
assertThat(gav.getArtifactId()).isEqualTo("junit");
assertThat(gav.getVersion()).isEqualTo("4.13.2");
})
)
)
)
);
}

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • rewrite-core/src/test/java/org/openrewrite/RecipeListTest.java
    • lines 35-35
    • lines 66-69
  • rewrite-maven/src/test/java/org/openrewrite/maven/trait/MavenPluginTest.java
    • lines 26-26

@nguyenhoan
Copy link
Contributor Author

Thank for the suggestion! Working on adding a test there helped me find the correct strategy.

@nguyenhoan nguyenhoan changed the title Exclude Maven built-in property from circular placeholder reference check Give explicit property definitions higher precedence than Maven's implicit ones. Jul 17, 2024
Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to see the added test & fix @nguyenhoan !

And I even got the review done in time on the train. ;)

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • rewrite-core/src/test/java/org/openrewrite/RecipeListTest.java
    • lines 35-35
    • lines 66-69
  • rewrite-maven/src/test/java/org/openrewrite/maven/trait/MavenPluginTest.java
    • lines 26-26

@timtebeek timtebeek merged commit a34eacc into openrewrite:main Jul 17, 2024
2 checks passed
@nguyenhoan
Copy link
Contributor Author

Awesome! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants