我有一个自定义 Maven 插件执行的简单设置,我使用
PlexusContainer
来执行对已定义容器的查找。
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);
}
}
}
}
我不想
@Inject
特定的一个,而是通过类型和名称来解析。组件已正确定义,例如:
@Named("myComponent")
public class MyComponent extends AbstractComponent {
}
问题:
整个问题在于所使用的Java版本,因为较新的Java版本无法正确注册组件。
container.hasComponent(MyComponent.class, "myComponent")
-> true
container.hasComponent(MyComponent.class, "myComponent")
-> false
背景:
我发现
maven-plugin-plugin
负责生成在组件扫描期间拾取的描述符。我有以下配置:
<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>
我还按照建议使用 Eclipse Sisu 插件:
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
</plugin>
依赖关系
支持的最低 Maven 版本是
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
我尝试上下更新版本,但在 Java 17 上运行失败。正确的组合是什么? Java 17 是否支持?
当您想为 JDK 17+ 编译 Maven 插件时,您必须使用 Maven 3.9.6+ 来执行。