Search code examples
mavenmaven-pluginjava-17plexuseclipse-sisu

Bindings with Eclipse Sisu for Maven Plugin do not work for Java 17


Reproducible at: GitHub

I have a simple setup of a custom Maven plugin execution where I use PlexusContainer to perform a lookup for the defined containers.

import javax.inject.Inject;

...

@Mojo(
    name = "process-sources",
    defaultPhase = LifecyclePhase.PROCESS_SOURCES,
    instantiationStrategy = InstantiationStrategy.PER_LOOKUP,
    threadSafe = true
)
public class ProcessSourcesMojo extends AbstractMojo {

    @Inject
    private PlexusContainer container;

    @Parameter
    private String componentName;

    ...

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        if (container.hasComponent(MyComponent.class, componentName)) {
            try {
                final AbstractComponent myComponent = container.lookup(AbstractComponent.class, componentName);
                // USE THE COMPONENT
            } catch (ComponentLookupException e) {
                throw new IllegalArgumentException(e);
            }
        }
    }
}

I don't want to @Inject a particular one but rather resolve by type and name. The components are properly defined, for example:

@Named("myComponent")
public class MyComponent extends AbstractComponent {
}

Problem:

The whole problem lies in the used Java version as the newer Java versions cannot register the components properly.

  • Java 8: container.hasComponent(MyComponent.class, "myComponent") -> true
  • Java 17: container.hasComponent(MyComponent.class, "myComponent") -> false

Background:

I discovered that maven-plugin-plugin is responsible for generating descriptors picked up during the components scan. I have the following configuration:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-plugin-plugin</artifactId>
    <version>3.9.0</version>
    <configuration>
        <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
    </configuration>
    <executions>
        <execution>
            <id>descriptor-help</id>
            <goals>
                <goal>helpmojo</goal>
            </goals>
        </execution>
    </executions>
</plugin>

I also use Eclipse Sisu plugin as suggested:

<plugin>
    <groupId>org.eclipse.sisu</groupId>
    <artifactId>sisu-maven-plugin</artifactId>
</plugin>

Dependencies

The minimum supported Maven version is 3.5.4.

  • org.apache.maven:maven-artifact:3.5.4:compile
  • org.apache.maven:maven-plugin-api:3.5.4:compile
  • org.apache.maven:maven-core:3.5.4:compile
  • org.apache.maven:maven-model:3.5.4:provided
  • org.apache.maven:maven-model-builder:3.5.4:provided
  • org.apache.maven:maven-settings:3.5.4:provided

  • org.apache.maven.plugin-tools:maven-plugin-annotations:3.6.1:provided

  • org.codehaus.plexus:plexus-utils:4.0.0:compile
  • org.codehaus.plexus:plexus-xml:3.0.0:compile
  • org.codehaus.plexus:plexus-classworlds:2.5.2:provided

  • org.eclipse.sisu:org.eclipse.sisu.plexus:0.9.0:M2:provided

  • javax.inject:javax.inject:1:provided
  • javax.annotation:javax.annotation-api:1.3.2:provided

I tried to update the versions up and down, but no luck with running on Java 17. What is the correct combination? Is Java 17 even supported?


Solution

  • When you want to compile Maven plugin for JDK 17+ you must use a Maven 3.9.6+ for executing.

    Related to issue: https://issues.apache.org/jira/browse/MNG-7913