gpt4 book ai didi

maven - 当 mojo 从不同模块(无 AbstractMojo)扩展 mojo 时,自定义 Maven 插件失败

转载 作者:行者123 更新时间:2023-12-03 22:29:29 26 4
gpt4 key购买 nike

我有一个包含 2 个 Maven 插件模块的 mutli 项目:base 和 child (<packaging>maven-plugin</packaging>)。 child 依赖基地。

基础插件只有一个类:

public abstract class BaseMojo extends AbstractMojo {}

使用 POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>plugin-set</groupId>
<artifactId>plugin-set</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>plugin-base</artifactId>
<packaging>maven-plugin</packaging>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.3.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.4</version>
<configuration>
<goalPrefix>my</goalPrefix>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
<executions>
<execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

子插件只有一个类:

public abstract class ChildMojo extends BaseMojo {}

使用 POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>plugin-set</groupId>
<artifactId>plugin-set</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>plugin-child</artifactId>
<packaging>maven-plugin</packaging>

<dependencies>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>plugin-set</groupId>
<artifactId>plugin-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.4</version>
<configuration>
<goalPrefix>my-child</goalPrefix>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
<executions>
<execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Base 编译正确,child 通过编译但失败 maven-plugin-plugin:3.4:descriptor :

Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:3.4:descriptor (default-descriptor) on project plugin-child: The API of the mojo scanner is not compatible with this plugin version. Please check the plugin dependencies configured in the POM and ensure the versions match. org/apache/maven/plugin/AbstractMojo: org.apache.maven.plugin.AbstractMojo -> [Help 1]

如何构建子模块?

base 和 child 都有几乎相同的 pom.xml,child 多了 1 个依赖:对 base

来源:https://github.com/michaldo/mojo-extend-mojo

最佳答案

编译问题的直接解决方案是删除 <scope>provided</scope>来自 maven-core依赖于 plugin-child .

但是,尽管之后会进行编译,但要使其正常工作,您的项目还需要进行大量更改。


您应该清楚了解什么是 Mojo 以及它是如何实现的。

首先,a Mojo should comply to a simple contract ,它正在实现 Mojo 界面。

Although the requirements on Mojos are minimal by design, there are still a very few requirements that Mojo developers must keep in mind. Basically, these Mojo requirements are embodied by the org.apache.maven.plugin.Mojo interface, which the Mojo must implement (or else extend its abstract base class counterpart org.apache.maven.plugin.AbstractMojo).

你可以有一个抽象类的层次结构,但最顶层的抽象类应该继承自 AbstractMojo (或实现 Mojo )。拥有一个共同的抽象父类并不少见。它由很多标准的 Maven 插件完成,比如 maven-install-plugin .

需要注意的是,抽象类只是将来自多个 Mojo 的通用代码分解为一个插件。它们不代表目标。因此,plugin-base模块,其中包含您的抽象父类(super class):

  • 不应打包为 maven-plugin .
  • 不应声明执行 maven-plugin-plugin插件。

具体的魔力就在里面plugin-child所以这个模块应该打包为 maven-plugin .由于这将是具体的实现,它不能是抽象的,可以用 @Mojo 注释简化声明:

@Mojo(name = "child")
public class ChildMojo extends BaseMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
}
}

这声明了目标 "child"为了这个魔力。


示例代码

plugin-base

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>plugin-set</groupId>
<artifactId>plugin-set</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>plugin-base</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.3.3</version>
</dependency>
</dependencies>
</project>

唯一的类是

import org.apache.maven.plugin.AbstractMojo;

public abstract class BaseMojo extends AbstractMojo { }

plugin-child

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>plugin-set</groupId>
<artifactId>plugin-set</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>plugin-child</artifactId>
<packaging>maven-plugin</packaging>

<dependencies>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>plugin-set</groupId>
<artifactId>plugin-base</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.4</version>
<configuration>
<goalPrefix>my-child</goalPrefix>
</configuration>
<executions>
<execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

使用以下 Java 代码:

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;

@Mojo(name = "child")
public class ChildMojo extends BaseMojo {

@Override
public void execute() throws MojoExecutionException, MojoFailureException {

}

}

最后,您可以根据这个插件来测试它是否有效:

<plugin>
<groupId>plugin-set</groupId>
<artifactId>plugin-child</artifactId>
<version>0.0.1-SNAPSHOT</version>
<executions>
<execution>
<id>foo</id>
<goals>
<goal>child</goal>
</goals>
<phase>install</phase>
</execution>
</executions>
</plugin>

如果根据plugin-child构建测试项目那么您将正确地看到 Maven 在 install 上调用它相位在这里。

关于maven - 当 mojo 从不同模块(无 AbstractMojo)扩展 mojo 时,自定义 Maven 插件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37838620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com