Sitelet https://github.com/apache/maven-plugin-tools/pull/1236
Skip to content

Mirror the mojo's injected constructor in the generated v4 factory - #1236

Open
CaptainAni187 wants to merge 1 commit into
apache:masterfrom
CaptainAni187:fix-v4-factory-constructor-injection
Open

CaptainAni187 wants to merge 1 commit into
apache:masterfrom
CaptainAni187:fix-v4-factory-constructor-injection

Conversation

@CaptainAni187

Copy link
Copy Markdown

Fixes #1229.

The factory generated for a Maven 4 API mojo always called the mojo's no-arg constructor, so a mojo using constructor injection built fine and then failed with NoSuchMethodError when the goal ran.

The factory constructor now mirrors the constructor DI would use on the mojo: the @Inject one, otherwise the no-arg one, otherwise the only one. It keeps the generic signature and the constructor and parameter annotations, so qualifiers like @Named still apply, and passes the arguments to super(...). A mojo with a no-arg constructor and no @Inject constructor gets the same factory as before. If no constructor can be picked (several without @Inject, or more than one with it), the descriptor goal fails with a message instead of producing a plugin that breaks at run time.

The new tests build the mojo classes with ASM, so the Maven 4 API isn't needed on the test classpath. I also ran the reproducer from the issue on Maven 4.0.0-rc-7: it fails with 4.0.0-beta-3 and runs with this change.


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

The factory generated for a Maven 4 API mojo always called the mojo's
no-arg constructor, so a mojo using constructor injection built fine
and then failed at run time with NoSuchMethodError.

The factory constructor now mirrors the constructor DI would pick on
the mojo (the @Inject one, else the no-arg one, else the only one),
with the same generic signature and annotations, and passes its
arguments to super. A mojo with no usable constructor fails the
descriptor goal instead.

Fixes apache#1229
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.

Generated factory for a Maven 4 API mojo requires a no-arg constructor

1 participant