This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/master by this push:
new d54b85880f feat: serialize non-thread-safe mojo executions in parallel
builds
d54b85880f is described below
commit d54b85880f067d0097411e7502a186a0fde640cc
Author: B V HITESH SAI <[email protected]>
AuthorDate: Mon Nov 9 22:23:07 2026 +0530
feat: serialize non-thread-safe mojo executions in parallel builds
Fixes #9107
In a parallel reactor build, Maven can execute the same mojo
concurrently for different projects. When a mojo is not declared
@threadSafe, concurrent execution races on plugin state and produces
nondeterministic behavior.
- DefaultBuildPluginManager now acquires a per-implementation-class lock
around non-thread-safe mojo execute() calls; thread-safe mojos are
unaffected and continue to run concurrently
- Warn only on actual lock contention, with an actionable message naming
the mojo, explaining the parallelism impact, and pointing to @threadSafe
- Setting -Dmaven.plugin.nonThreadSafeSerializer=false restores the
previous behavior for compatibility opt-out
- Add Core IT MavenITmng7525NonThreadSafeMojoTest covering serialization,
contention warning, and the opt-out property
- Document the new property in configuring.md
---
.../maven/plugin/DefaultBuildPluginManager.java | 48 +++++++++++++++--
.../it/MavenITmng7525NonThreadSafeMojoTest.java | 60 ++++++++++++++++++++++
.../mng-7525-non-thread-safe-mojo/module-a/pom.xml | 47 +++++++++++++++++
.../mng-7525-non-thread-safe-mojo/module-b/pom.xml | 47 +++++++++++++++++
.../mng-7525-non-thread-safe-mojo/plugin/pom.xml | 57 ++++++++++++++++++++
.../maven/its/mng7525/NonThreadSafeMojo.java | 47 +++++++++++++++++
.../mng-7525-non-thread-safe-mojo/pom.xml | 33 ++++++++++++
src/site/markdown/configuring.md | 12 +++++
8 files changed, 348 insertions(+), 3 deletions(-)
diff --git
a/impl/maven-core/src/main/java/org/apache/maven/plugin/DefaultBuildPluginManager.java
b/impl/maven-core/src/main/java/org/apache/maven/plugin/DefaultBuildPluginManager.java
index 476f2aadcd..51800550b0 100644
---
a/impl/maven-core/src/main/java/org/apache/maven/plugin/DefaultBuildPluginManager.java
+++
b/impl/maven-core/src/main/java/org/apache/maven/plugin/DefaultBuildPluginManager.java
@@ -26,6 +26,8 @@
import java.io.PrintStream;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
import org.apache.maven.api.Project;
@@ -46,6 +48,7 @@
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.RemoteRepository;
+import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// TODO the antrun plugin has its own configurator, the only plugin that does.
might need to think about how that works
@@ -58,10 +61,15 @@
@Singleton
public class DefaultBuildPluginManager implements BuildPluginManager {
+ private static final String NON_THREAD_SAFE_SERIALIZER_PROPERTY =
"maven.plugin.nonThreadSafeSerializer";
+
+ private static final Logger LOGGER =
LoggerFactory.getLogger(DefaultBuildPluginManager.class);
+
private final MavenPluginManager mavenPluginManager;
private final LegacySupport legacySupport;
private final MojoExecutionScope scope;
private final MojoExecutionListener mojoExecutionListener;
+ private final ConcurrentHashMap<Class<?>, ReentrantLock> mojoLocks = new
ConcurrentHashMap<>();
@Inject
public DefaultBuildPluginManager(
@@ -130,6 +138,7 @@ public void executeMojo(MavenSession session, MojoExecution
mojoExecution)
scope.seed(Project.class, sessionV4.getProject(project));
scope.seed(org.apache.maven.api.MojoExecution.class, new
DefaultMojoExecution(sessionV4, mojoExecution));
+ Class<?> mojoLock;
if (mojoDescriptor.isV4Api()) {
// For Maven 4 plugins, register a service so that they can be
directly injected into plugins
Map<Class<? extends Service>, Supplier<? extends Service>>
services = sessionV4.getAllServices();
@@ -138,8 +147,10 @@ public void executeMojo(MavenSession session,
MojoExecution mojoExecution)
org.apache.maven.api.plugin.Mojo mojoV4 =
mavenPluginManager.getConfiguredMojo(
org.apache.maven.api.plugin.Mojo.class, session,
mojoExecution);
mojo = new MojoWrapper(mojoV4);
+ mojoLock = mojoV4.getClass();
} else {
mojo = mavenPluginManager.getConfiguredMojo(Mojo.class,
session, mojoExecution);
+ mojoLock = mojo.getClass();
}
legacySupport.setSession(session);
@@ -149,9 +160,7 @@ public void executeMojo(MavenSession session, MojoExecution
mojoExecution)
// MavenProjectHelper.attachArtifact(..).
try {
MojoExecutionEvent mojoExecutionEvent = new
MojoExecutionEvent(session, project, mojoExecution, mojo);
- mojoExecutionListener.beforeMojoExecution(mojoExecutionEvent);
- mojo.execute();
-
mojoExecutionListener.afterMojoExecutionSuccess(mojoExecutionEvent);
+ executeMojo(mojoDescriptor, mojo, mojoLock,
mojoExecutionEvent);
} catch (ClassCastException | MavenException e) {
// to be processed in the outer catch block
throw e;
@@ -205,6 +214,39 @@ public void executeMojo(MavenSession session,
MojoExecution mojoExecution)
}
}
+ private void executeMojo(
+ MojoDescriptor mojoDescriptor, Mojo mojo, Class<?> mojoLock,
MojoExecutionEvent mojoExecutionEvent)
+ throws MojoExecutionException, MojoFailureException {
+ mojoExecutionListener.beforeMojoExecution(mojoExecutionEvent);
+ if (mojoDescriptor.isThreadSafe() ||
!isNonThreadSafeMojoSerializationEnabled()) {
+ mojo.execute();
+ } else {
+ // Lock ordering is aggregatorLock -> projectLock ->
mojoClassLock. Keep the mojo class lock innermost:
+ // code executed while holding it must not acquire either outer
lock. The concurrent builder follows the
+ // projectLock -> mojoClassLock subset for legacy mojos; Maven 4
mojos are thread-safe and skip this lock.
+ ReentrantLock lock = mojoLocks.computeIfAbsent(mojoLock, key ->
new ReentrantLock());
+ if (!lock.tryLock()) {
+ LOGGER.warn(
+ "Serializing concurrent execution of non-thread-safe
mojo {}. This may reduce build parallelism. "
+ + "Mark the mojo @threadSafe if it is safe for
concurrent use, or disable serialization with "
+ + "-D{}=false.",
+ mojoDescriptor.getId(),
+ NON_THREAD_SAFE_SERIALIZER_PROPERTY);
+ lock.lock();
+ }
+ try {
+ mojo.execute();
+ } finally {
+ lock.unlock();
+ }
+ }
+ mojoExecutionListener.afterMojoExecutionSuccess(mojoExecutionEvent);
+ }
+
+ private boolean isNonThreadSafeMojoSerializationEnabled() {
+ return
Boolean.parseBoolean(System.getProperty(NON_THREAD_SAFE_SERIALIZER_PROPERTY,
"true"));
+ }
+
/**
* TODO pluginDescriptor classRealm and artifacts are set as a side effect
of this
* call, which is not nice.
diff --git
a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7525NonThreadSafeMojoTest.java
b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7525NonThreadSafeMojoTest.java
new file mode 100644
index 0000000000..85ef0c8cf3
--- /dev/null
+++
b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng7525NonThreadSafeMojoTest.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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.apache.maven.it;
+
+import java.nio.file.Path;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * Verifies that parallel reactor builds serialize executions of a mojo that
is not marked as thread-safe.
+ */
+class MavenITmng7525NonThreadSafeMojoTest extends
AbstractMavenIntegrationTestCase {
+
+ @Test
+ void serializesNonThreadSafeMojoExecutions() throws Exception {
+ Path projectDir = extractResources("mng-7525-non-thread-safe-mojo");
+
+ Verifier verifier = newVerifier(projectDir);
+ verifier.execute("install", "-pl", "plugin");
+ verifier.verifyErrorFreeLog();
+
+ verifier = newVerifier(projectDir);
+ verifier.execute("-T2", "validate");
+ verifier.verifyErrorFreeLog();
+ verifier.verifyTextInLog("Serializing concurrent execution of
non-thread-safe mojo");
+ }
+
+ @Test
+ void canDisableNonThreadSafeMojoSerialization() throws Exception {
+ Path projectDir = extractResources("mng-7525-non-thread-safe-mojo");
+
+ Verifier verifier = newVerifier(projectDir);
+ verifier.execute("install", "-pl", "plugin");
+ verifier.verifyErrorFreeLog();
+
+ Verifier disabledVerifier = newVerifier(projectDir);
+ assertThrows(
+ VerificationException.class,
+ () ->
disabledVerifier.execute("-Dmaven.plugin.nonThreadSafeSerializer=false", "-T2",
"validate"));
+ disabledVerifier.verifyTextInLog("non-thread-safe mojo was executed
concurrently");
+ }
+}
diff --git
a/its/core-it-suite/src/test/resources/mng-7525-non-thread-safe-mojo/module-a/pom.xml
b/its/core-it-suite/src/test/resources/mng-7525-non-thread-safe-mojo/module-a/pom.xml
new file mode 100644
index 0000000000..17c5470de8
--- /dev/null
+++
b/its/core-it-suite/src/test/resources/mng-7525-non-thread-safe-mojo/module-a/pom.xml
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you 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
+
+ http://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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.maven.its.mng7525</groupId>
+ <artifactId>mng-7525-parent</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>module-a</artifactId>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.its.mng7525</groupId>
+ <artifactId>mng-7525-plugin</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>check</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git
a/its/core-it-suite/src/test/resources/mng-7525-non-thread-safe-mojo/module-b/pom.xml
b/its/core-it-suite/src/test/resources/mng-7525-non-thread-safe-mojo/module-b/pom.xml
new file mode 100644
index 0000000000..72f1fe02cd
--- /dev/null
+++
b/its/core-it-suite/src/test/resources/mng-7525-non-thread-safe-mojo/module-b/pom.xml
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you 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
+
+ http://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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.maven.its.mng7525</groupId>
+ <artifactId>mng-7525-parent</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>module-b</artifactId>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.its.mng7525</groupId>
+ <artifactId>mng-7525-plugin</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>check</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git
a/its/core-it-suite/src/test/resources/mng-7525-non-thread-safe-mojo/plugin/pom.xml
b/its/core-it-suite/src/test/resources/mng-7525-non-thread-safe-mojo/plugin/pom.xml
new file mode 100644
index 0000000000..d66c847650
--- /dev/null
+++
b/its/core-it-suite/src/test/resources/mng-7525-non-thread-safe-mojo/plugin/pom.xml
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you 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
+
+ http://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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.maven.its.mng7525</groupId>
+ <artifactId>mng-7525-parent</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>mng-7525-plugin</artifactId>
+ <packaging>maven-plugin</packaging>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>3.9.12</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven.plugin-tools</groupId>
+ <artifactId>maven-plugin-annotations</artifactId>
+ <version>3.15.2</version>
+ <scope>provided</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-plugin-plugin</artifactId>
+ <version>3.15.2</version>
+ <configuration>
+ <goalPrefix>mng7525</goalPrefix>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git
a/its/core-it-suite/src/test/resources/mng-7525-non-thread-safe-mojo/plugin/src/main/java/org/apache/maven/its/mng7525/NonThreadSafeMojo.java
b/its/core-it-suite/src/test/resources/mng-7525-non-thread-safe-mojo/plugin/src/main/java/org/apache/maven/its/mng7525/NonThreadSafeMojo.java
new file mode 100644
index 0000000000..1b96cf0806
--- /dev/null
+++
b/its/core-it-suite/src/test/resources/mng-7525-non-thread-safe-mojo/plugin/src/main/java/org/apache/maven/its/mng7525/NonThreadSafeMojo.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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.apache.maven.its.mng7525;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+
+@Mojo(name = "check", defaultPhase = LifecyclePhase.VALIDATE)
+public class NonThreadSafeMojo extends AbstractMojo {
+
+ private static final AtomicInteger EXECUTIONS = new AtomicInteger();
+
+ @Override
+ public void execute() throws MojoExecutionException {
+ if (EXECUTIONS.incrementAndGet() != 1) {
+ throw new MojoExecutionException("non-thread-safe mojo was
executed concurrently");
+ }
+ try {
+ Thread.sleep(250);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new MojoExecutionException("interrupted", e);
+ } finally {
+ EXECUTIONS.decrementAndGet();
+ }
+ }
+}
diff --git
a/its/core-it-suite/src/test/resources/mng-7525-non-thread-safe-mojo/pom.xml
b/its/core-it-suite/src/test/resources/mng-7525-non-thread-safe-mojo/pom.xml
new file mode 100644
index 0000000000..58f6b027e9
--- /dev/null
+++ b/its/core-it-suite/src/test/resources/mng-7525-non-thread-safe-mojo/pom.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you 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
+
+ http://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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.its.mng7525</groupId>
+ <artifactId>mng-7525-parent</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <packaging>pom</packaging>
+
+ <modules>
+ <module>plugin</module>
+ <module>module-a</module>
+ <module>module-b</module>
+ </modules>
+</project>
diff --git a/src/site/markdown/configuring.md b/src/site/markdown/configuring.md
index 0de8320e38..6726fa6894 100644
--- a/src/site/markdown/configuring.md
+++ b/src/site/markdown/configuring.md
@@ -90,6 +90,18 @@ from `${session.rootDirectory}/.mvn/maven-user.properties`.
For the complete list of available Maven configuration properties and options,
see [Maven Configuration Options](configuration.html).
+## Parallel build safety
+
+In a parallel build, Maven serializes concurrent executions of the same legacy
mojo class when it is not marked `@threadSafe`. The lock is shared across all
projects in the reactor, so an execution in one project blocks the same mojo
class in another project even when those projects are otherwise independent.
Maven emits a warning only when an execution actually has to wait, but this
serialization can noticeably reduce effective parallelism in a large
multi-module build.
+
+Some legacy plugins are safe for concurrent use but predate the `@threadSafe`
declaration. As a temporary workaround while such a plugin is updated, the
build owner can disable serialization globally with:
+
+```
+-Dmaven.plugin.nonThreadSafeSerializer=false
+```
+
+Disable this safeguard only when the affected mojos are known to be safe for
concurrent execution. The long-term solution is for the plugin to declare its
safe mojos `@threadSafe` or migrate to the Maven 4 plugin API. Maven 4 mojos
are always considered thread-safe and are not subject to this serialization.
+
## Settings
Settings are configured in the `~/.m2/settings.xml` file (user level),