当前位置: 首页 > 知识库问答 >
问题:

自定义maven插件在mojo从不同模块扩展mojo时失败(无抽象mojo)

易书
2023-03-14

我有一个带有两个maven插件模块的mutli项目:base和child( maven-plugin )。孩子依赖基础。

public abstract class BaseMojo extends AbstractMojo {}
<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-html" target="_blank">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>

基本编译正确,子级通过编译但失败maven-plugin-plugin:3.4:descriptor:

未能在项目plugin-child上执行goal org.apache.maven.plugins:maven-plugin-plugin:3.4:descriptor(default-descriptor):mojo扫描器的API与此插件版本不兼容。请检查POM中配置的插件依赖项,并确保版本匹配。org/apache/maven/plugin/abstractMojo:org.apache.maven.plugin.abstractMojo->[帮助1]

共有1个答案

索梓
2023-03-14

编译问题的直接解决方案是从plugin-child中的maven-core依赖项中删除 提供的。

然而,尽管它将在之后进行编译,但要使其工作,需要对项目进行大量更改。

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

需要注意的是,抽象类只是为了从一个插件中的多个Mojo中提取公共代码。它们不代表一个目标。因此,plugin-base模块包含抽象超类:

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

具体的Mojo位于plugin-child内部,因此应该将此模块打包为maven-plugin。因为这将是具体的实现,所以它不能是抽象的,可以用@mojo对其进行注释,以简化其声明:

@Mojo(name = "child")
public class ChildMojo extends BaseMojo {
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
    }
}
<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 { }
<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>
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构建测试项目,那么在install阶段,您将正确地看到Maven正在调用它。

 类似资料:
  • 我试图运行两次MOJO的exec-maven-plugin,但它抱怨主类没有设置。有了这个我的。Main我想生成几个文件,它们必须在编译阶段之前完成。我做错了什么?为两次执行都设置了主类,并具有正确的参数。 我的pom.xml包含以下内容: 跑步后 我得到这个错误 向你问好,SK

  • 我的问题是:根据这些参数,我可以在哪里归档标准maven插件的API fefinition?

  • Maven的默认目标是构建。如果我想将我的插件添加到Resources:TestResources阶段,@mojo注释必须是什么,以及我的插件的pom表示应该是什么样子?

  • Mojolicious is a fresh take on Perl web development, based on years of experience developing the Catalyst framework,and utilizing the latest web standards and technologies. You can get started with yo

  • 我正在创建一个自定义maven插件。在其中一个mojo中,我使用以下代码段从XML文件中读取一个Xpp3Dom对象: 我正在读取的XML文件(

  • 我在执行此错误发生在集成sonarqube之后。 [ERROR]无法执行目标org.springframework.boot:spall-boot-maven-plugin:2.6.4:在项目bootloader上运行(默认):无法解析mojoorg.springframework.boot的配置:spall-boot-maven-plugin:2.6.4:run for参数包括:无法在类org中