Skip to content

Commit

Permalink
Delaying execution to the end of multi-module project with Maven para…
Browse files Browse the repository at this point in the history
…llel build (#726)

* Delaying execution to the end of multi-module project

* Clean comments
  • Loading branch information
philippe-granet authored Jan 31, 2024
1 parent 20fbae7 commit 38eb90b
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,18 @@ public class AbstractRewriteDryRunMojo extends AbstractRewriteMojo {
public void execute() throws MojoExecutionException {
if (rewriteSkip) {
getLog().info("Skipping execution");
putState(State.SKIPPED);
return;
}
putState(State.TO_BE_PROCESSED);

// If the plugin is configured to run over all projects (at the end of the build) only proceed if the plugin
// is being run on the last project.
if (!runPerSubmodule && !isLastProjectInReactor()) {
if (!runPerSubmodule && !allProjectsMarked()) {
getLog().info("REWRITE: Delaying execution to the end of multi-module project for "
+ project.getGroupId() + ":"
+ project.getArtifactId()+ ":"
+ project.getVersion());
return;
}

Expand Down Expand Up @@ -140,5 +146,6 @@ public void execute() throws MojoExecutionException {
} else {
getLog().info("Applying recipes would make no changes. No patch file generated.");
}
putState(State.PROCESSED);
}
}
44 changes: 24 additions & 20 deletions src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
Expand Down Expand Up @@ -77,8 +78,31 @@ public abstract class AbstractRewriteMojo extends ConfigurableRewriteMojo {
@Parameter(defaultValue = "${session}", readonly = true)
protected MavenSession mavenSession;

@Parameter(defaultValue = "${plugin}", required = true, readonly = true)
protected PluginDescriptor pluginDescriptor;

private static final String RECIPE_NOT_FOUND_EXCEPTION_MSG = "Could not find recipe '%s' among available recipes";

protected enum State {
SKIPPED,
PROCESSED,
TO_BE_PROCESSED
}
private static final String OPENREWRITE_PROCESSED_MARKER = "openrewrite.processed";

protected void putState(State state) {
getPluginContext().put(OPENREWRITE_PROCESSED_MARKER, state.name());
}

private boolean hasState(MavenProject project) {
Map<String, Object> pluginContext = mavenSession.getPluginContext(pluginDescriptor, project);
return pluginContext.containsKey(OPENREWRITE_PROCESSED_MARKER);
}

protected boolean allProjectsMarked() {
return mavenSession.getProjects().stream().allMatch(this::hasState);
}

protected Environment environment() throws MojoExecutionException {
return environment(getRecipeArtifactCoordinatesClassloader());
}
Expand Down Expand Up @@ -118,26 +142,6 @@ Config getConfig() throws IOException {
return null;
}

/**
* Is this project the last project in the reactor?
*
* @return true if last project (including only project)
*/
protected boolean isLastProjectInReactor() {
List<MavenProject> sortedProjects = mavenSession.getProjectDependencyGraph().getSortedProjects();

MavenProject lastProject = sortedProjects.isEmpty()
? mavenSession.getCurrentProject()
: sortedProjects.get(sortedProjects.size() - 1);

if (getLog().isDebugEnabled()) {
getLog().debug("Current project: '" + mavenSession.getCurrentProject().getName() +
"', Last project to execute based on dependency graph: '" + lastProject.getName() + "'");
}

return mavenSession.getCurrentProject().equals(lastProject);
}

protected Environment environment(@Nullable ClassLoader recipeClassLoader) throws MojoExecutionException {
Environment.Builder env = Environment.builder(project.getProperties());
if (recipeClassLoader == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,18 @@ public class AbstractRewriteRunMojo extends AbstractRewriteMojo {
public void execute() throws MojoExecutionException {
if (rewriteSkip) {
getLog().info("Skipping execution");
putState(State.SKIPPED);
return;
}
putState(State.TO_BE_PROCESSED);

// If the plugin is configured to run over all projects (at the end of the build) only proceed if the plugin
// is being run on the last project.
if (!runPerSubmodule && !isLastProjectInReactor()) {
if (!runPerSubmodule && !allProjectsMarked()) {
getLog().info("REWRITE: Delaying execution to the end of multi-module project for "
+ project.getGroupId() + ":"
+ project.getArtifactId()+ ":"
+ project.getVersion());
return;
}

Expand Down Expand Up @@ -150,6 +156,7 @@ public void execute() throws MojoExecutionException {
throw new RuntimeException("Unable to rewrite source files", e);
}
}
putState(State.PROCESSED);
}

private static void writeAfter(Path root, Result result, ExecutionContext ctx) {
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/org/openrewrite/maven/RewriteRunParallelIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.maven;

import com.soebes.itf.jupiter.extension.*;
import com.soebes.itf.jupiter.maven.MavenExecutionResult;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;

import static com.soebes.itf.extension.assertj.MavenITAssertions.assertThat;

@MavenJupiterExtension
@MavenOption(value = MavenCLIOptions.THREADS, parameter = "2")
@MavenOption(MavenCLIOptions.NO_TRANSFER_PROGRESS)
@MavenOption(MavenCLIExtra.MUTE_PLUGIN_VALIDATION_WARNING)
@DisabledOnOs(OS.WINDOWS)
@MavenGoal("${project.groupId}:${project.artifactId}:${project.version}:run")
@SuppressWarnings("NewClassNamingConvention")
class RewriteRunParallelIT {

@MavenTest
void multi_module_project(MavenExecutionResult result) {
assertThat(result)
.isSuccessful()
.out()
.info()
.anySatisfy(line -> assertThat(line).contains("Delaying execution to the end of multi-module project for org.openrewrite.maven:b:1.0"));

assertThat(result)
.isSuccessful()
.out()
.warn()
.anySatisfy(line -> assertThat(line).contains("org.openrewrite.staticanalysis.SimplifyBooleanExpression"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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.openrewrite.maven</groupId>
<artifactId>multi_module_project</artifactId>
<version>1.0</version>
</parent>

<artifactId>a</artifactId>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>simulate-sleep</id>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<sleep seconds="10"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package sample;

public interface MyInterface {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package sample;

public class SimplifyBooleanSample {
boolean ifNoElse() {
if (isOddMillis()) {
return true;
}
return false;
}

static boolean isOddMillis() {
boolean even = System.currentTimeMillis() % 2 == 0;
if (even == true) {
return false;
}
else {
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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.openrewrite.maven</groupId>
<artifactId>multi_module_project</artifactId>
<version>1.0</version>
</parent>

<artifactId>b</artifactId>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sample;

import java.util.Random;

public class EmptyBlockSample {
int n = sideEffect();

static {
}

int sideEffect() {
return new Random().nextInt();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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>

<groupId>org.openrewrite.maven</groupId>
<artifactId>multi_module_project</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<modules>
<module>a</module>
<module>b</module>
</modules>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<activeRecipes>
<recipe>com.example.RewriteRunIT.CodeCleanup</recipe>
</activeRecipes>
<configLocation>
${maven.multiModuleProjectDirectory}/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/multi_module_project/rewrite.yml
</configLocation>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-static-analysis</artifactId>
<version>1.0.4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
type: specs.openrewrite.org/v1beta/recipe
name: com.example.RewriteRunIT.CodeCleanup
recipeList:
- org.openrewrite.staticanalysis.SimplifyBooleanExpression
- org.openrewrite.staticanalysis.SimplifyBooleanReturn
- org.openrewrite.staticanalysis.UnnecessaryParentheses

0 comments on commit 38eb90b

Please sign in to comment.