Skip to content

Add Mojo configuration introspection service - #14111

Open
abdulhannansyed wants to merge 1 commit into
apache:masterfrom
abdulhannansyed:feature-13518-mojo-configuration-introspection
Open

abdulhannansyed wants to merge 1 commit into
apache:masterfrom
abdulhannansyed:feature-13518-mojo-configuration-introspection

Conversation

@abdulhannansyed

@abdulhannansyed abdulhannansyed commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Implement a Mojo configuration introspection service for the Maven v4 API, allowing embedders to inspect resolved Mojo parameter values without instantiating or executing the root Mojo.

Implementation

  • Add MojoConfigurationService to the Maven API.
  • Implement parameter extraction in Maven Core.
  • Support configured parameters and descriptor defaults.
  • Resolve expressions and convert values to their declared types.
  • Extract and reuse plugin configuration interpolation logic.
  • Avoid instantiating the root Mojo or invoking its setters.
  • Add tests covering configuration values, defaults, expressions, aliases, type conversion, nested configuration, and plugin realm resolution.

Testing

  • Full Maven reactor: BUILD SUCCESS
  • 38 modules successful
  • 3,127 tests, 0 failures, 0 errors, 17 skipped
  • Checkstyle: 0 violations
  • RAT: 0 unapproved / 0 unknown
  • git diff --check: clean

To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
check one of the following:

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this feature! Adding a MojoConfigurationService for introspecting Mojo parameters without instantiation is a useful capability. I have a few issues to address before this can merge.

🔴 Wrong @since tag

The @since tag on MojoConfigurationService reads 4.1.0, but master is currently 4.2.0-SNAPSHOT. This must be @since 4.2.0.

🟠 Missing class name in exception messages

Both guard clauses in DefaultMojoConfigurationService throw an IllegalArgumentException with a fixed message, making it impossible to diagnose failures at runtime. The message should include the actual runtime class name of the rejected argument.

// Instead of:
throw new IllegalArgumentException("Unsupported MojoExecution implementation");
// Use:
throw new IllegalArgumentException("Unsupported MojoExecution implementation: " + mojoExecution.getClass().getName());

Same for the Project guard clause.

🟡 Cross-package coupling via PluginConfigurationInterpolator

DefaultMojoConfigurationService lives in configuration.internal but imports PluginConfigurationInterpolator from plugin.internal. Coupling two separate internal packages is a layering concern — PluginConfigurationInterpolator should either be moved to the shared configuration.internal package or extracted to a non-internal location.

🟡 TCCL swap undocumented

The TCCL swap at line 100 carries an implicit threading restriction: callers must invoke getParameters() from the same thread that holds the plugin class loader. Consider adding a @ThreadSafe / @NotThreadSafe annotation or at minimum a Javadoc note explaining why the TCCL swap is needed and what threading contract callers must honour.

🔵 Test boilerplate: createMavenSession(null) / new ClassWorld() repeated 11×

Each of the 11 test methods calls createMavenSession(null) and new ClassWorld(...) independently. Extract them to a @BeforeEach setup method.

🔵 No negative tests for guard clauses

The implementation rejects non-DefaultMojoExecution, non-DefaultProject, and non-V4-Mojo inputs with IllegalArgumentException. None of these cases are tested. Please add assertThrows tests for each guard clause.


This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following up on my earlier review — all 6 issues have been addressed.

  • 🔴 @since tag — ✅ fixed to @since 4.2.0
  • 🟠 Exception messages missing class name — ✅ both now include .getClass().getName()
  • 🟡 Cross-package coupling (PluginConfigurationInterpolator in plugin.internal) — ✅ moved to configuration.internal
  • 🟡 TCCL swap undocumented — ✅ Javadoc now states the class loader is temporarily set and restored
  • 🔵 Test boilerplate repeated 11× — ✅ extracted to @BeforeEach setUp()
  • 🔵 No negative tests for guard clauses — ✅ three assertThrows tests added (rejectsUnsupportedMojoExecutionImplementation, rejectsUnsupportedProjectImplementation, rejectsNonV4Mojo)

LGTM.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet gnodet left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Before we go further on the implementation, I'd like to understand the intended use case better, as it drives some key design choices.

Who is the caller?​ The description says "embedders", which makes sense — a Mojo doesn't need this, it already gets its parameters injected. So this is for tools that want to inspect resolved Mojo configuration without executing the Mojo: IDEs, migration tools, linters, JBang/Quarkus dev-mode, etc.

 V3 plugin support is mandatory.​ The current implementation rejects any Mojo with !isV4Api(). In practice, the plugins embedders most want to inspect today — maven-compiler-plugin, maven-surefire-plugin, maven-jar-plugin — are all V3 plugins. The equivalent abstractions exist in core (PluginParameterExpressionEvaluator, ComponentConfigurator("basic")), so the V4-only restriction is a simplification choice, not a technical constraint. This needs to be supported before the API can be considered useful.

 Return type: Map<String, Object> vs Map<String, XmlNode>.​
 This depends entirely on what callers actually do with the values. The current Map<String, Object> has a classloader leak: for primitive types (String, int, boolean, File, Path) it's fine, but for complex nested types (e.g. <archive> in maven-jar-plugin) the converter instantiates objects from the plugin realm. The caller is in a different classloader and can't cast or even inspect those types — ClassCastException guaranteed.

 Could you describe your concrete use case? Specifically: what do you do with the returned objects? If the answer is "display them" or "check whether a parameter is set", then Map<String, XmlNode> (the interpolated configuration tree, before type conversion) is the right contract — no classloader exposure, no Mojo instantiation, and it works identically for V3 and V4 plugins. If you genuinely need typed values, then we need to constrain the return type to JDK types only and document that explicitly.

@gnodet-bot

Copy link
Copy Markdown
Contributor

@abdulhannansyed548-cmd Thanks for the PR! Before we go further on the implementation, I'd like to understand the intended use case better, as it drives some key design choices.

Who is the caller? The description says "embedders", which makes sense — a Mojo doesn't need this, it already gets its parameters injected. So this is for tools that want to inspect resolved Mojo configuration without executing the Mojo: IDEs, migration tools, linters, JBang/Quarkus dev-mode, etc.

V3 plugin support is mandatory. The current implementation rejects any Mojo with !isV4Api(). In practice, the plugins embedders most want to inspect today — maven-compiler-plugin, maven-surefire-plugin, maven-jar-plugin — are all V3 plugins. The equivalent abstractions exist in core (PluginParameterExpressionEvaluator, ComponentConfigurator("basic")), so the V4-only restriction is a simplification choice, not a technical constraint. This needs to be supported before the API can be considered useful.

Return type: Map<String, Object> vs Map<String, XmlNode>.
This depends entirely on what callers actually do with the values. The current Map<String, Object> has a classloader leak: for primitive types (String, int, boolean, File, Path) it's fine, but for complex nested types (e.g. <archive> in maven-jar-plugin) the converter instantiates objects from the plugin realm. The caller is in a different classloader and can't cast or even inspect those types — ClassCastException guaranteed.

Could you describe your concrete use case? Specifically: what do you do with the returned objects? If the answer is "display them" or "check whether a parameter is set", then Map<String, XmlNode> (the interpolated configuration tree, before type conversion) is the right contract — no classloader exposure, no Mojo instantiation, and it works identically for V3 and V4 plugins. If you genuinely need typed values, then we need to constrain the return type to JDK types only and document that explicitly.

This comment was posted by an AI agent, Hermès, on behalf of @gnodet.

@abdulhannansyed

Copy link
Copy Markdown
Contributor Author

@gnodet Thanks for raising these points. Let me clarify the intended use case.

The primary use case I had in mind is IDE integration, particularly the kind of functionality currently implemented by m2e. For example, its Java project configurator reads resolved parameters from maven-compiler-plugin, such as source, target, release, encoding, includes, excludes, and parameters, and uses them to configure the IDE project.

The caller needs resolved parameter values, rather than a configured Mojo instance. In this scenario, the values are primarily simple types and collections such as String, String[], Boolean, and List. The intention is not to expose arbitrary plugin-specific objects or allow callers to interact with the Mojo itself.

I agree that returning complex objects loaded from a plugin realm through Map<String, Object> creates a classloader-boundary problem. The current implementation does not adequately address that concern.

I also agree that V3 support is important. The compiler, Surefire, and JAR plugins are practical examples, so restricting the service to V4 plugins would significantly limit its usefulness.

I think we should clarify the result contract before extending the implementation. One possibility is to return typed values only for supported JDK types and avoid exposing plugin-specific complex objects. Another is to return an interpolated Map<String, XmlNode> and provide typed conversion separately.

Would you prefer the service to expose only the classloader-neutral XML representation, or should we retain typed access for supported JDK types as part of this API?

@gnodet

gnodet commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

@gnodet Thanks for raising these points. Let me clarify the intended use case.

The primary use case I had in mind is IDE integration, particularly the kind of functionality currently implemented by m2e. For example, its Java project configurator reads resolved parameters from maven-compiler-plugin, such as source, target, release, encoding, includes, excludes, and parameters, and uses them to configure the IDE project.

The caller needs resolved parameter values, rather than a configured Mojo instance. In this scenario, the values are primarily simple types and collections such as String, String[], Boolean, and List. The intention is not to expose arbitrary plugin-specific objects or allow callers to interact with the Mojo itself.

I agree that returning complex objects loaded from a plugin realm through Map<String, Object> creates a classloader-boundary problem. The current implementation does not adequately address that concern.

I also agree that V3 support is important. The compiler, Surefire, and JAR plugins are practical examples, so restricting the service to V4 plugins would significantly limit its usefulness.

I think we should clarify the result contract before extending the implementation. One possibility is to return typed values only for supported JDK types and avoid exposing plugin-specific complex objects. Another is to return an interpolated Map<String, XmlNode> and provide typed conversion separately.

Would you prefer the service to expose only the classloader-neutral XML representation, or should we retain typed access for supported JDK types as part of this API?

Good idea, retaining typed access for supported JDK types looks like a good middle ground to me.

@abdulhannansyed

Copy link
Copy Markdown
Contributor Author

Hi @gnodet ,

I've pushed an update addressing the feedback discussed in the review.

The changes include:

  • V3 support: Added the legacy V3 expression evaluator path alongside the existing V4 support.
  • Classloader safety: Added parameter-type inspection to avoid returning plugin-specific objects that could retain references to the plugin classloader. Plugin-specific parameter configurations are represented as XmlNode values instead.
  • No Mojo instantiation: The service continues to inspect and resolve configuration without instantiating or executing the root Mojo.
  • Tests: Added coverage for parameter-type classification and configuration resolution.

I also ran the full Maven Core reactor test suite:

mvn -pl impl/maven-core -am test

All 30 modules passed; Maven Core reported 708 tests, 0 failures, 0 errors, and 1 skipped.

Could you please review the updated implementation, particularly the XmlNode representation for plugin-specific parameter values and the V3 support? I'd appreciate your feedback on whether this API approach fits the intended IDE/embedding use case.

Thanks!

@gnodet
gnodet force-pushed the feature-13518-mojo-configuration-introspection branch from 139f0f5 to 3f69d5d Compare November 27, 2026 20:05

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following up on my earlier review — the previous CHANGES_REQUESTED findings are all resolved. ✅ The new commit adds V3 support and extracts PluginConfigurationInterpolator to configuration.internal. Two correctness issues found in the new code.

🔴 StringIndexOutOfBoundsException in findCachedMethod when called from setDefault

EnhancedCompositeBeanHelper.setDefault() calls findCachedMethod(beanType, "", null) to locate a default "set" method (line 83). Inside findCachedMethod, the first thing it does is:

String title = Character.toTitleCase(propertyName.charAt(0)) + propertyName.substring(1);

When propertyName is "", charAt(0) throws StringIndexOutOfBoundsException. The fallback lookup in the caller (methodCache.get("set")) is never reached — the exception propagates up before the fallback branch on lines 85–87 executes.

🟠 No null check on pluginRealm after setupPluginRealm

After calling pluginManager.setupPluginRealm(...), the code re-reads pluginDescriptor.getClassRealm(). If setupPluginRealm succeeds but fails to register a realm (unusual but possible with certain plugin descriptor inconsistencies), pluginRealm remains null. Then:

  • setContextClassLoader(null) silently clears the TCCL
  • Class.forName(impl, false, null) uses the boot classloader → ClassNotFoundException
  • The error message "Could not resolve Mojo parameters" with an attached ClassNotFoundException is misleading since realm setup ostensibly succeeded

The finally block restores the TCCL correctly, but adding an explicit null check here makes the failure mode unambiguous.

🔵 Unbounded static caches

METHOD_CACHE and FIELD_CACHE are static ConcurrentHashMap fields keyed by Class<?>. They are never evicted. Each distinct mojo class loaded across plugin realms adds an entry that holds a strong reference to the Class<?>, keeping its ClassLoader (the plugin realm) alive indefinitely — preventing GC of the entire realm and all classes it loaded. In a long-running daemon build with many plugins this is a steady memory leak.

Consider WeakHashMap<Class<?>, ...> (with appropriate synchronization), a size-bounded LRU, or at minimum document the unbounded growth in the class Javadoc. clearCaches() exists for testing, but is not called in production.


This review was generated by an AI agent, Hermès on behalf of @gnodet.

private MethodInfo findCachedMethod(Class<?> beanType, String propertyName, Class<?> valueType) {
Map<String, MethodInfo> classMethodCache = METHOD_CACHE.computeIfAbsent(beanType, this::buildMethodCache);

String title = Character.toTitleCase(propertyName.charAt(0)) + propertyName.substring(1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 StringIndexOutOfBoundsException when propertyName is "": setDefault() on line 83 calls findCachedMethod(beanType, "", null). When this reaches here, propertyName.charAt(0) throws StringIndexOutOfBoundsException before the fallback classMethodCache.get("set") in the caller (lines 85–87) is reached.

Suggested change
String title = Character.toTitleCase(propertyName.charAt(0)) + propertyName.substring(1);
if (propertyName.isEmpty()) {
return classMethodCache.get("set");
}
String title = Character.toTitleCase(propertyName.charAt(0)) + propertyName.substring(1);

if (pluginRealm == null) {
try {
pluginManager.setupPluginRealm(pluginDescriptor, interpolationSession, null, null, null);
pluginRealm = pluginDescriptor.getClassRealm();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Missing null guard: if setupPluginRealm succeeds without registering a realm, pluginRealm is null here. setContextClassLoader(null) then silently clears the TCCL, and Class.forName(..., false, null) throws ClassNotFoundException with a misleading error message. Add an explicit check:

Suggested change
pluginRealm = pluginDescriptor.getClassRealm();
pluginRealm = pluginDescriptor.getClassRealm();
if (pluginRealm == null) {
throw new IllegalStateException(
"Plugin realm setup completed but no ClassRealm was registered for "
+ pluginDescriptor.getId());
}

private static final ConcurrentMap<Class<?>, Map<String, MethodInfo>> METHOD_CACHE = new ConcurrentHashMap<>();

// Cache for field lookups: Class -> FieldName -> Field
private static final ConcurrentMap<Class<?>, Map<String, Field>> FIELD_CACHE = new ConcurrentHashMap<>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Unbounded static cache: METHOD_CACHE and FIELD_CACHE are static and never evicted. Each distinct mojo Class<?> from any plugin realm adds an entry that holds a strong reference to the Class<?> — keeping its ClassLoader (the plugin realm) alive indefinitely, preventing GC of the entire realm and all classes it loaded. clearCaches() exists for testing but is not called in production. Consider WeakHashMap<Class<?>, ...> (synchronized) or document the unbounded growth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants