openrewrite / rewrite

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

ChangeDependencyClassifier should add explicit version when dependency-with-classifier is not managed #4646

Open rcsilva83 opened 13 hours ago

rcsilva83 commented 13 hours ago

What version of OpenRewrite are you using?

I am using

How are you running OpenRewrite?

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

      <plugin>
        <groupId>org.openrewrite.maven</groupId>
        <artifactId>rewrite-maven-plugin</artifactId>
        <version>5.43.1</version>
        <configuration>
          <activeRecipes>
            <recipe>my.company.rewrite.recipe.MyRecipe</recipe>
          </activeRecipes>
          <exportDatatables>true</exportDatatables>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>my.company.rewrite.recipe</groupId>
            <artifactId>my-rewrite</artifactId>
            <version>1.0.0-SNAPSHOT</version>
          </dependency>
        </dependencies>
      </plugin>

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

type: specs.openrewrite.org/v1beta/recipe
name: my.company.rewrite.recipe.MyRecipe
displayName: My Recipe
description: This is an OpenRewrite recipe
recipeList:
  - org.openrewrite.maven.ChangeDependencyClassifier:
      groupId: com.querydsl
      artifactId: "*"
      newClassifier: jakarta
<?xml version="1.0" encoding="UTF-8"?>
<project
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://maven.apache.org/POM/4.0.0"
  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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.3.5</version>
    <relativePath/>
  </parent>

  <groupId>my.company.myapp</groupId>
  <artifactId>myapp</artifactId>
  <packaging>jar</packaging>
  <name>My App</name>

  <dependencies>
    <dependency>
      <groupId>com.querydsl</groupId>
      <artifactId>querydsl-jpa</artifactId>
    </dependency>
  </dependencies>

</project>

What did you expect to see?

No change or:

<?xml version="1.0" encoding="UTF-8"?>
<project
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://maven.apache.org/POM/4.0.0"
  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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.3.5</version>
    <relativePath/>
  </parent>

  <groupId>my.company.myapp</groupId>
  <artifactId>myapp</artifactId>
  <packaging>jar</packaging>
  <name>My App</name>

  <dependencies>
    <dependency>
      <groupId>com.querydsl</groupId>
      <artifactId>querydsl-jpa</artifactId>
      <version>${querydsl.version}</version>
      <classifier>jakarta</classifier>
    </dependency>
  </dependencies>

</project>

What did you see instead?

<?xml version="1.0" encoding="UTF-8"?>
<project
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://maven.apache.org/POM/4.0.0"
  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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.3.5</version>
    <relativePath/>
  </parent>

  <groupId>my.company.myapp</groupId>
  <artifactId>myapp</artifactId>
  <packaging>jar</packaging>
  <name>My App</name>

  <dependencies>
    <dependency>
      <groupId>com.querydsl</groupId>
      <artifactId>querydsl-jpa</artifactId>
      <classifier>jakarta</classifier>
    </dependency>
  </dependencies>

</project>

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

None

Are you interested in contributing a fix to OpenRewrite?

Yes

timtebeek commented 5 hours ago

Thanks for logging the issue! Am I correct in deducing that when you add a classifier, you want to version to be added when the dependency-with-classifier version is not managed?

rcsilva83 commented 5 hours ago

Sorry, @timtebeek . I didn't explain. It's confusing but Maven requires a <version> tag when you add a <classifier>. I've already filed an issue to them about it.

timtebeek commented 2 hours ago

I think we ought to be able to recognize when there's no matching managed dependency with the same classifier, and if so make the version explicit. Would welcome a draft PR that explores this with a unit test, and from there what would be necessary for a fix.