Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maven agent modes support work #315

Closed
wants to merge 1 commit into from
Closed

Conversation

lazar-mitrovic
Copy link
Collaborator

This draft PR is a result of many of my attempts at getting Maven to bind values from pom.xml to the Mojo @Parameter annotated fields while NativeExtension is being run.

Currently, the only way to access configuration values during extension time is to manually traverse the DOM tree. I strongly believe that this approach is fundamentally wrong and that there should be a better way - a 2 way binding between Mojo fields that are annotated as @Parameter and a corresponding String value that is loaded from the effective pom.xml. IMO this was a prerequisite for #260 since the shear amount of nested parameters would soon become unmanageable.

I tried to look up a way to utilize existing Maven internal code, however I was unable to decouple the parameter binding logic from the rest of the code. This effectively means that the only way forward is to reinvent the wheel.

My original idea then was to reflecively introspect the Mojos in order to lookup Parameter annotations and then use them to bind values from the plugin's configuration block to the Mojo fields. Unfortunately this doesn't work since Retention parameter of the @Parameter annotation is set to RetentionPolicy.CLASS...

Unfortunately this leaves only the hard way: simulate what Maven does by doing the following steps:

  • Parse PluginDescriptor and get a ParameterMap.
  • Find all fields in the given Mojo, making sure that private fields are included
  • Try binding entries in the parameter map to the Mojo class fields
  • Try binding configuration entries to the PluginDescriptor
  • Try converting String values from the configuration to the target type

The last point is problematic since annotated types can be generics, or even classes with nested parameters. In the ExtensionTimeConfigurationUtility you can find my attempt at tackling this without external dependencies.

This is where I have ran out of time. This was a much bigger challenge than I originally anticipated.
IMO if somebody decides to properly solve this issue, the code for it should definitely be published as a standalone dependency (as opposed to some utility package in this plugin) as many plugins could benefit from it.

With this experience, as a path forward for solving #260 I suggest extracting maven/config/agent classes, integrating them into that new PR as dummy values, and proceeding to parse XML by hand. That is exactly what I wanted to avoid, but it is the only way to deliver this feature in real time (unless some Maven wizard creates some way to do what I attempted to implement).
After fetching the values, following things should be done:

  • Use AgentConfiguration.getAgentCommandLine in the NativeExtension in order to configure the agent invocation.
  • Configure Mojos for merging generated metadata.
  • Add a Mojo that copies the resulting metadata (say mvn native:metadataCopy).

None of these should be particularly difficult.

cc: @sdeleuze @alvarosanchez

Signed-off-by: Lazar Mitrović <lazar.mitrovic@oracle.com>
@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Sep 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant