5

I want to work on a Maven plugin, and as part of its configuration (which will be mapped to the mojo fields), I'd like to supply XML attributes on a few tags. Unfortunately, the official guide makes no mention of the use of attributes in configuration blocks (which may or may not mean that it's simply not possible.)

What I'd like to have is this:

<plugin>
  <configuration>
    <my_option attr="x" /> <!-- is this possible? -->
  </configuration>
</plugin>

Is this possible at all? If so, how will this map to a field in my mojo?

mxk
  • 43,056
  • 28
  • 105
  • 132
  • It would have been hugely beneficial if the maven developers had incorporated XML attributes into the pom design. The result of not doing so is unfortunately long and verbose. For my plugin, I need to configure some maps for which the keys are not valid XML tags, so I'm forced into an even more verbose ```keyvalue``` style. What started with `````` was perpetuated as a mess. – Ed Randall Jan 30 '20 at 08:21

2 Answers2

3

I know I'm late to the question, but I think the answer is different. After all, the Maven antrun plugin works exactly the way the OP wanted; look at the way target can supply any XML you could use in an ant build file.

It's done in the mojo by declaring that parameter with type PlexusConfiguration, see around lines 164-171.

PlexusConfiguration doesn't extend any well-known XML API such as DOM, and it seems not to support namespaces, PIs, etc., but it has the basic getChildren/getAttributeNames/getAttribute methods you would want for most purposes.

Whether there's also a way to do it in the newer JSR-330 (non-Plexus) way, I don't know, but as far as the Plexus APIs go, the doc does say "those APIs will be supported forever, or at least until Maven fully drops Maven 2 support."

Chapman Flack
  • 604
  • 5
  • 13
  • Looking for more on this, there may be some clues here: https://stackoverflow.com/a/38642688/482828 and here: https://stackoverflow.com/a/38633590/482828 – Ed Randall Jan 30 '20 at 08:18
2

Nope. Maven does not support attributes. You can however use a map of strings or so. Look at e.g how the compiler plugin does compilerArguments or how the Android Maven Plugin (on which you are working) does jvmArguments in the dex mojo configuration.

Manfred Moser
  • 29,539
  • 13
  • 92
  • 123
  • Thanks Manfred; I ended up creating a POJO that holds the configuration. I'd rather have it resemble the syntax used to define the same settings in the Manifest, but as far as I can tell, this will not be possible with any of these approaches. – mxk Jan 07 '12 at 11:04
  • I believe this answer is not complete, in fact it is possible to use custom attributes if a mojo's parameter has type `PlexusConfiguration` (see details in answer by @chapman-flack below https://stackoverflow.com/a/32800814/547270) – scrutari Jul 05 '18 at 09:22