openrewrite / rewrite-maven-plugin

OpenRewrite's Maven plugin.
https://openrewrite.github.io/rewrite-maven-plugin/plugin-info.html
Apache License 2.0
138 stars 71 forks source link

Later build phases depending on earlier rewrite:runNoFork not supported in multi module Maven project #851

Closed runeflobakk closed 2 weeks ago

runeflobakk commented 2 weeks 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.

I want to rewrite source code in an early phase of the build (process-sources), in order to have the rewritten code participate in the usual compiling phase. This seems to work when building only the single module the way I would intuitively set it up: by binding runNoFork to a process-sources in my POM:

<plugin>
    <groupId>org.openrewrite.maven</groupId>
    <artifactId>rewrite-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>transform-to-jakarta-sources</id>
            <phase>process-sources</phase>
            <goals>
                <goal>runNoFork</goal>
            </goals>
            <configuration>
                <activeRecipes>
                    <recipe>org.openrewrite.java.migrate.jakarta.JavaxXmlBindMigrationToJakartaXmlBind</recipe>
                </activeRecipes>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.openrewrite.recipe</groupId>
            <artifactId>rewrite-migrate-java</artifactId>
            <version>2.23.0</version>
        </dependency>
    </dependencies>
</plugin>

Everything appears to work really well when working with the Maven module in question, I get the source files rewritten as they should, but when running the entire multiproject, I get the following logged at the stage where I have configured rewrite-maven-plugin to execute:

[INFO] --- rewrite:5.39.2:runNoFork (transform-to-jakarta-sources) @ my-rewriting-maven-module ---
[INFO] REWRITE: Delaying execution to the end of multi-module project for group.id:my-rewriting-maven-module:1.0-SNAPSHOT

And then further goals in the build fail because they depend on the code being rewritten.

I suspect my use-case is very similar to #755 , though I do not depend on the build-helper-maven-plugin. I use maven-dependency-plugin:unpack on an existing sources artifact, unpacking to src/main/java, and then wanted to run runNoFork on the unpacked sources for them to participate in the rest of the build, like ordinary Java source files.

Is this related to the existing issues with running in multimodule projects? I am not sure why the plugin insists on delaying the execution, but I also found #720 which indicates this is by design. I have not found any configuration option to force the rewriting happening during the phase the plugin execution is bound to. Maybe that is what I am missing? :)

Attempt with earlier version

I did try downgrading to plugin v5.20.0 (together with rewrite-migrate-java v2.7.0) just to see if the version before #720 was released in 5.21.0 would give me the behavior I wanted. It seemed however to behave the same; the sources are rewritten when building only the single module, and no rewriting happens with a full multi module build. Only observable difference is the plugin does not log the "Delaying execution" message.

Are you interested in contributing a fix to OpenRewrite?

Sure, I can give it a go, if this seems to be a viable contribution to OpenRewrite :)

I hope it is OK to remove some sections in the issue template, as I intended the above to sufficiently describe the issue. I would be happy to provide a more self-contained reproducing example if you need it :)

timtebeek commented 2 weeks ago

Hi @runeflobakk ; thanks for detailed write up! Must say I hadn't come across that type of usage of the rewrite-maven-plugin before; good to hear you already came this far with your use case.

The one thing I'm missing from your description above is if you had already considered or tried the -Drewrite.runPerSubmodule=true option. It sounds like that might be what you're after to not wait until all modules are parsed.

https://github.com/openrewrite/rewrite-maven-plugin/blob/056a5a285d33417d662e983612308b85cdd69245/src/main/java/org/openrewrite/maven/AbstractRewriteDryRunMojo.java#L58-L66

Does that help you there? Otherwise we'd indeed be open to a fix if you're able to provide one.

runeflobakk commented 2 weeks ago

@timtebeek Ooooh, that was spot-on! Thank you, it works perfectly! I must say my first experience trying out OpenRewrite is ridiculously good! 😄👍

timtebeek commented 2 weeks ago

@timtebeek Ooooh, that was spot-on! Thank you, it works perfectly! I must say may first experience trying out OpenRewrite is ridiculously good! 😄👍

Great to hear that got you past this hurdle, and glad to have try out the project. Thanks for the kind words, and perhaps see you at JavaBin in October!