You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2026/08/31 11:45:20 UTC
(maven) branch fix/toolchain-plugin-inherited-source-level created (now 3a3c8918eb)
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a change to branch fix/toolchain-plugin-inherited-source-level
in repository https://gitbox.apache.org/repos/asf/maven.git
at 3a3c8918eb Fix ToolchainPluginStrategy to detect inherited source levels from parent POMs
This branch includes the following new commits:
new 3a3c8918eb Fix ToolchainPluginStrategy to detect inherited source levels from parent POMs
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(maven) 01/01: Fix ToolchainPluginStrategy to detect inherited source levels from parent POMs
Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch fix/toolchain-plugin-inherited-source-level
in repository https://gitbox.apache.org/repos/asf/maven.git
commit 3a3c8918ebc7c1977402660f1c3448e1ec4912bb
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Mon Aug 31 13:45:04 2026 +0200
Fix ToolchainPluginStrategy to detect inherited source levels from parent POMs
When a project inherits --source/--release from a remote parent POM
(e.g. org.apache.sling:sling-parent setting --source 6), mvnup's
ToolchainPluginStrategy failed to detect it because detectSourceLevel()
only inspected the local POM XML DOM. This caused mvnup to skip adding
the toolchains plugin, leading to Maven 4 build failures.
Changes:
- Share the Maven 4 API Session across all strategy instances (DCL
singleton) so the Session's RequestCache deduplicates effective model
builds across PluginUpgradeStrategy, ToolchainPluginStrategy, and
CompatibilityFixStrategy
- Route buildEffectiveModel() through InternalSession.request() to
leverage the RequestCache
- Add effective model fallback in ToolchainPluginStrategy: when local
POM has no source level, resolve the effective model to pick up
inherited compiler configuration from parent POMs
- Add 8 new tests for effective model source level detection
Co-Authored-By: Claude Opus 4.6 <no...@anthropic.com>
---
.../mvnup/goals/AbstractUpgradeStrategy.java | 29 ++++--
.../mvnup/goals/ToolchainPluginStrategy.java | 112 ++++++++++++++++++++
.../mvnup/goals/ToolchainPluginStrategyTest.java | 113 +++++++++++++++++++++
3 files changed, 248 insertions(+), 6 deletions(-)
diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/AbstractUpgradeStrategy.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/AbstractUpgradeStrategy.java
index 708fb35988..cbb1bbe996 100644
--- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/AbstractUpgradeStrategy.java
+++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/AbstractUpgradeStrategy.java
@@ -45,6 +45,7 @@
import org.apache.maven.api.services.RepositoryFactory;
import org.apache.maven.api.services.Sources;
import org.apache.maven.cling.invoker.mvnup.UpgradeContext;
+import org.apache.maven.impl.InternalSession;
import org.apache.maven.impl.standalone.ApiRunner;
import org.codehaus.plexus.components.secdispatcher.Dispatcher;
import org.codehaus.plexus.components.secdispatcher.internal.dispatchers.LegacyDispatcher;
@@ -70,7 +71,12 @@
*/
public abstract class AbstractUpgradeStrategy implements UpgradeStrategy {
- private Session session;
+ /**
+ * Shared Maven 4 API Session across all strategy instances.
+ * Sharing the session enables the Session's {@link org.apache.maven.api.cache.RequestCache}
+ * to deduplicate effective model builds across strategies.
+ */
+ private static volatile Session sharedSession;
/**
* Template method that handles common logging and error handling.
@@ -212,10 +218,17 @@ public static Set<Coordinates> computeAllArtifactCoordinates(UpgradeContext cont
}
protected Session getSession() {
- if (session == null) {
- session = createMaven4Session();
+ Session s = sharedSession;
+ if (s == null) {
+ synchronized (AbstractUpgradeStrategy.class) {
+ s = sharedSession;
+ if (s == null) {
+ s = createMaven4Session();
+ sharedSession = s;
+ }
+ }
}
- return session;
+ return s;
}
private Session createMaven4Session() {
@@ -294,7 +307,6 @@ protected void cleanupTempDirectory(Path tempDir) {
protected org.apache.maven.api.model.Model buildEffectiveModel(Path pomPath) {
Session session = getSession();
- ModelBuilder modelBuilder = session.getService(ModelBuilder.class);
ModelBuilderRequest request = ModelBuilderRequest.builder()
.session(session)
@@ -303,7 +315,12 @@ protected org.apache.maven.api.model.Model buildEffectiveModel(Path pomPath) {
.recursive(false)
.build();
- ModelBuilderResult result = modelBuilder.newSession().build(request);
+ ModelBuilderResult result = InternalSession.from(session)
+ .request(
+ request,
+ req -> session.getService(ModelBuilder.class)
+ .newSession()
+ .build(req));
return result.getEffectiveModel();
}
diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategy.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategy.java
index 9db93a51a2..01e7edc483 100644
--- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategy.java
+++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategy.java
@@ -20,6 +20,7 @@
import java.nio.file.Path;
import java.util.HashSet;
+import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -29,6 +30,11 @@
import org.apache.maven.api.di.Named;
import org.apache.maven.api.di.Priority;
import org.apache.maven.api.di.Singleton;
+import org.apache.maven.api.model.Build;
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.model.Plugin;
+import org.apache.maven.api.model.PluginManagement;
+import org.apache.maven.api.xml.XmlNode;
import org.apache.maven.cling.invoker.mvnup.UpgradeContext;
import org.apache.maven.impl.JdkSourceLevelSupport;
@@ -131,6 +137,11 @@ protected UpgradeResult doApply(UpgradeContext context, Map<Path, Document> pomM
try {
int sourceLevel = detectSourceLevel(pomDocument);
+ if (sourceLevel <= 0) {
+ // Local POM doesn't declare a source level — check inherited config
+ // from parent POMs (e.g. org.apache.sling:sling-parent setting --source 6)
+ sourceLevel = detectSourceLevelFromEffectiveModel(context, pomPath);
+ }
if (sourceLevel <= 0) {
context.success("No source level configured");
continue;
@@ -261,6 +272,107 @@ private int detectFromPluginSection(Element pluginsElement) {
return -1;
}
+ /**
+ * Detects the project's required source level from the effective (fully-resolved) model.
+ * This resolves inherited configuration from parent POMs on Maven Central.
+ *
+ * <p>The Session's request cache ensures that effective model builds are deduplicated
+ * across strategies — if {@code PluginUpgradeStrategy} already built the effective model
+ * for this POM, the cached result is returned immediately.
+ *
+ * @param context the upgrade context for logging
+ * @param pomPath the path to the POM file
+ * @return the source level as a major version, or {@code -1} if none is configured
+ */
+ int detectSourceLevelFromEffectiveModel(UpgradeContext context, Path pomPath) {
+ try {
+ Model effectiveModel = buildEffectiveModel(pomPath);
+ return detectSourceLevelFromModel(effectiveModel);
+ } catch (Exception e) {
+ context.debug("Failed to build effective model for " + pomPath + ": " + e.getMessage());
+ return -1;
+ }
+ }
+
+ /**
+ * Extracts the source level from a resolved effective model by checking properties
+ * and compiler plugin configuration.
+ *
+ * @param effectiveModel the fully-resolved effective model
+ * @return the source level as a major version, or {@code -1} if none is configured
+ */
+ int detectSourceLevelFromModel(Model effectiveModel) {
+ Map<String, String> props = effectiveModel.getProperties();
+
+ // Check maven.compiler.release property (takes precedence)
+ String release = props.get(MAVEN_COMPILER_RELEASE);
+ if (release != null && !release.isBlank()) {
+ int level = JdkSourceLevelSupport.normalizeSourceLevel(release.trim());
+ if (level > 0) {
+ return level;
+ }
+ }
+
+ // Check maven.compiler.source property
+ String source = props.get(MAVEN_COMPILER_SOURCE);
+ if (source != null && !source.isBlank()) {
+ int level = JdkSourceLevelSupport.normalizeSourceLevel(source.trim());
+ if (level > 0) {
+ return level;
+ }
+ }
+
+ // Check compiler plugin configuration from resolved build
+ Build build = effectiveModel.getBuild();
+ if (build != null) {
+ int level = detectFromEffectivePlugins(build.getPlugins());
+ if (level > 0) {
+ return level;
+ }
+ PluginManagement pm = build.getPluginManagement();
+ if (pm != null) {
+ level = detectFromEffectivePlugins(pm.getPlugins());
+ if (level > 0) {
+ return level;
+ }
+ }
+ }
+
+ return -1;
+ }
+
+ private int detectFromEffectivePlugins(List<Plugin> plugins) {
+ for (Plugin plugin : plugins) {
+ if (MAVEN_COMPILER_PLUGIN.equals(plugin.getArtifactId())) {
+ XmlNode config = plugin.getConfiguration();
+ if (config != null) {
+ // <release> takes precedence
+ XmlNode releaseNode = config.child("release");
+ if (releaseNode != null
+ && releaseNode.value() != null
+ && !releaseNode.value().isBlank()) {
+ int level = JdkSourceLevelSupport.normalizeSourceLevel(
+ releaseNode.value().trim());
+ if (level > 0) {
+ return level;
+ }
+ }
+ XmlNode sourceNode = config.child("source");
+ if (sourceNode != null
+ && sourceNode.value() != null
+ && !sourceNode.value().isBlank()) {
+ int level = JdkSourceLevelSupport.normalizeSourceLevel(
+ sourceNode.value().trim());
+ if (level > 0) {
+ return level;
+ }
+ }
+ }
+ }
+ }
+ return -1;
+ }
+
/**
* Checks whether the POM already has the {@code maven-toolchains-plugin}
* configured with the {@code select-jdk-toolchain} goal.
diff --git a/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategyTest.java b/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategyTest.java
index 7414d262a1..94ed750dcf 100644
--- a/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategyTest.java
+++ b/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategyTest.java
@@ -20,9 +20,15 @@
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.List;
import java.util.Map;
import eu.maveniverse.domtrip.Document;
+import org.apache.maven.api.model.Build;
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.model.Plugin;
+import org.apache.maven.api.model.PluginManagement;
+import org.apache.maven.api.xml.XmlNode;
import org.apache.maven.cling.invoker.mvnup.UpgradeContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
@@ -528,4 +534,111 @@ int getRunningJdkMajor() {
assertEquals(0, result.modifiedPoms().size());
}
}
+
+ @Nested
+ @DisplayName("Effective model source level detection")
+ class EffectiveModelSourceLevelTests {
+
+ private ToolchainPluginStrategy strategy;
+
+ @BeforeEach
+ void setUp() {
+ strategy = new ToolchainPluginStrategy();
+ }
+
+ @Test
+ @DisplayName("should detect source level from effective model maven.compiler.release property")
+ void detectFromEffectiveRelease() {
+ Model model = Model.newBuilder()
+ .properties(Map.of("maven.compiler.release", "6"))
+ .build();
+ assertEquals(6, strategy.detectSourceLevelFromModel(model));
+ }
+
+ @Test
+ @DisplayName("should detect source level from effective model maven.compiler.source property")
+ void detectFromEffectiveSource() {
+ Model model = Model.newBuilder()
+ .properties(Map.of("maven.compiler.source", "1.6"))
+ .build();
+ assertEquals(6, strategy.detectSourceLevelFromModel(model));
+ }
+
+ @Test
+ @DisplayName("effective model release takes precedence over source")
+ void effectiveReleaseTakesPrecedence() {
+ Model model = Model.newBuilder()
+ .properties(Map.of("maven.compiler.release", "7", "maven.compiler.source", "6"))
+ .build();
+ assertEquals(7, strategy.detectSourceLevelFromModel(model));
+ }
+
+ @Test
+ @DisplayName("should detect from effective model compiler plugin release config")
+ void detectFromEffectivePluginRelease() {
+ XmlNode config = XmlNode.newInstance("configuration", List.of(XmlNode.newInstance("release", "6")));
+ Plugin compilerPlugin = Plugin.newBuilder()
+ .artifactId("maven-compiler-plugin")
+ .configuration(config)
+ .build();
+ Model model = Model.newBuilder()
+ .build(Build.newBuilder().plugins(List.of(compilerPlugin)).build())
+ .build();
+ assertEquals(6, strategy.detectSourceLevelFromModel(model));
+ }
+
+ @Test
+ @DisplayName("should detect from effective model compiler plugin source config")
+ void detectFromEffectivePluginSource() {
+ XmlNode config = XmlNode.newInstance("configuration", List.of(XmlNode.newInstance("source", "1.5")));
+ Plugin compilerPlugin = Plugin.newBuilder()
+ .artifactId("maven-compiler-plugin")
+ .configuration(config)
+ .build();
+ Model model = Model.newBuilder()
+ .build(Build.newBuilder().plugins(List.of(compilerPlugin)).build())
+ .build();
+ assertEquals(5, strategy.detectSourceLevelFromModel(model));
+ }
+
+ @Test
+ @DisplayName("should detect from effective model pluginManagement")
+ void detectFromEffectivePluginManagement() {
+ XmlNode config = XmlNode.newInstance("configuration", List.of(XmlNode.newInstance("release", "8")));
+ Plugin compilerPlugin = Plugin.newBuilder()
+ .artifactId("maven-compiler-plugin")
+ .configuration(config)
+ .build();
+ Model model = Model.newBuilder()
+ .build(Build.newBuilder()
+ .pluginManagement(PluginManagement.newBuilder()
+ .plugins(List.of(compilerPlugin))
+ .build())
+ .build())
+ .build();
+ assertEquals(8, strategy.detectSourceLevelFromModel(model));
+ }
+
+ @Test
+ @DisplayName("should return -1 from effective model with no source level")
+ void noSourceLevelInEffectiveModel() {
+ Model model = Model.newBuilder().build();
+ assertEquals(-1, strategy.detectSourceLevelFromModel(model));
+ }
+
+ @Test
+ @DisplayName("properties take precedence over plugin config in effective model")
+ void propertiesTakePrecedenceInEffectiveModel() {
+ XmlNode config = XmlNode.newInstance("configuration", List.of(XmlNode.newInstance("release", "6")));
+ Plugin compilerPlugin = Plugin.newBuilder()
+ .artifactId("maven-compiler-plugin")
+ .configuration(config)
+ .build();
+ Model model = Model.newBuilder()
+ .properties(Map.of("maven.compiler.release", "8"))
+ .build(Build.newBuilder().plugins(List.of(compilerPlugin)).build())
+ .build();
+ assertEquals(8, strategy.detectSourceLevelFromModel(model));
+ }
+ }
}