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

Migrate from sources to resolve-sources for the maven-dependency-plugin #20

Open
yeikel opened this issue Jun 21, 2024 · 2 comments
Open
Labels
good first issue Good for newcomers recipe

Comments

@yeikel
Copy link

yeikel commented Jun 21, 2024

What problem are you trying to solve?

What precondition(s) should be checked before applying this recipe?

  • It is a maven project using maven-dependency-plugin >= 3.7.0

Describe the situation before applying the recipe

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>${maven-dependency-plugin.version}</version>
  <executions>
    <execution>
      <goals>
        <goal>sources</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Describe the situation after applying the recipe

diff --git a/pom.xml b/pom.xml
index 6f1d54220..fbee73aba 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1027,7 +1027,7 @@
                     <executions>
                         <execution>
                             <goals>
-                                <goal>sources</goal>
+                                <goal>resolve-sources</goal>
                             </goals>
                         </execution>
                     </executions>

Any additional context

https://issues.apache.org/jira/browse/MDEP-941
https://github.com/apache/maven-dependency-plugin/pull/411/files

@timtebeek timtebeek transferred this issue from openrewrite/rewrite Jun 23, 2024
@timtebeek timtebeek moved this to Recipes Wanted in OpenRewrite Jun 23, 2024
@timtebeek timtebeek added recipe good first issue Good for newcomers labels Jun 23, 2024
@timtebeek
Copy link
Contributor

Thanks for the suggestion @yeikel , and the helpful links out to Jira & the associated PR. I've moved this issue to rewrite-apache, as we might want to shift some of the none-core Maven best practices recipes here.

We can use a precondition to limit this to only apply to projects using that particular range of versions, and then within that replace any goals as needed.

@timtebeek
Copy link
Contributor

Did a very brief attempt at getting this fixed; here's a quick test and recipe class:

import org.junit.jupiter.api.Test;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.junit.jupiter.api.Assertions.*;
import static org.openrewrite.java.Assertions.mavenProject;
import static org.openrewrite.maven.Assertions.pomXml;

class SourcesToResolveSourcesTest implements RewriteTest {

    @Override
    public void defaults(RecipeSpec spec) {
        spec.recipe(new SourcesToResolveSources());
    }

    @Test
    void sourcesToResolveSources() {
        rewriteRun(
          mavenProject("foo",
            //language=xml
            pomXml(
              """
                <project>
                    <modelVersion>4.0.0</modelVersion>
                    <groupId>com.mycompany.app</groupId>
                    <artifactId>my-app</artifactId>
                    <version>1</version>
                    <build>
                        <plugins>
                            <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-dependency-plugin</artifactId>
                                <version>3.8.0</version>
                                <executions>
                                    <execution>
                                        <goals>
                                            <goal>sources</goal>
                                        </goals>
                                    </execution>
                                </executions>
                            </plugin>
                        </plugins>
                    </build>
                </project>
                """,

              """
                <project>
                    <modelVersion>4.0.0</modelVersion>
                    <groupId>com.mycompany.app</groupId>
                    <artifactId>my-app</artifactId>
                    <version>1</version>
                    <build>
                        <plugins>
                            <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-dependency-plugin</artifactId>
                                <version>3.8.0</version>
                                <executions>
                                    <execution>
                                        <goals>
                                            <goal>resolve-sources</goal>
                                        </goals>
                                    </execution>
                                </executions>
                            </plugin>
                        </plugins>
                    </build>
                </project>
                """
            ))
        );
    }
}
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.xml.XPathMatcher;
import org.openrewrite.xml.tree.Xml;

public class SourcesToResolveSources extends Recipe {

    @Override
    public String getDisplayName() {
        return "Migrate to `resolve-sources`";
    }

    @Override
    public String getDescription() {
        return "Migrate from `sources` to `resolve-sources` for the `maven-dependency-plugin`.";
    }

    private static final XPathMatcher xPathMatcher = new XPathMatcher("//plugin[artifactId='maven-dependency-plugin']/executions/execution/goals[goal='sources']/goal");

    @Override
    public TreeVisitor<?, ExecutionContext> getVisitor() {
        return new MavenIsoVisitor<ExecutionContext>() {
            @Override
            public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
                // TODO Compare version of plugin to determine if it is necessary to change the goal
                if (xPathMatcher.matches(getCursor())) {
                    return tag.withValue("resolve-sources");
                }
                return super.visitTag(tag, ctx);
            }
        };
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers recipe
Projects
Status: Recipes Wanted
Development

No branches or pull requests

2 participants