【问题标题】:Basic maven plugin project not working, Mojo plugin descriptors not generating基本 maven 插件项目不工作,Mojo 插件描述符不生成
【发布时间】:2013-09-30 17:15:53
【问题描述】:

我正在关注 tutorial 创建一个 maven 插件,并且无法运行 mvn install 而不会出现错误。当注释应该为我生成它们时,该信息抱怨我没有所需的 mojo 描述符。我正在运行 maven 3.0.5 并使用 intellij 作为我的 ide。这是我的主要课程:

@Mojo(name = "modify-connector")
public class ComplianceMojo extends AbstractMojo {

    @Parameter
    private String artifactId;

    @Parameter
    private String version;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        File jar = new File(getPluginContext().get("project.build.directory") + "/"
                + getPluginContext().get("project.build.finalname") + "/" + artifactId + "-" + version);
        if(jar.exists()){
            getLog().info("The file exists! " + jar.getAbsolutePath());
        } else {
            getLog().info("The file does not exist: " + jar.getAbsolutePath());
        }
    }
}

这是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <groupId>mysql-jdbc-compliance-maven-plugin</groupId>
    <artifactId>mysql-jdbc-compliance-maven-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>maven-plugin</packaging>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

注意:我必须单独添加注解依赖,因为主插件 api 不包含这些类。当我在我的项目上运行 mvn install 时,输出如下:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.867s
[INFO] Finished at: Wed Sep 25 17:45:55 EST 2013
[INFO] Final Memory: 8M/244M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:2.9:descriptor (default-descriptor) on project mysql-jdbc-compliance-maven-plugin: Error extracting plugin descriptor: 'No mojo definitions were found for plugin: mysql-jdbc-compliance-maven-plugin:mysql-jdbc-compliance-maven-plugin.' -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

【问题讨论】:

    标签: java maven maven-plugin


    【解决方案1】:

    正如previous answer 中所述,这是一个错误,现已修复。

    您只需要告诉 maven 它应该使用更新版本的maven-plugin-plugin

    这是我的 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">
      <!-- ...other maven config... -->
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-plugin-plugin</artifactId>
            <version>3.3</version>
          </plugin>
        </plugins>
      </build>
    </project>
    

    【讨论】:

    • 2019-06-10 版本 3.6.1
    • @jordiburgos search.maven.org 指定 3.6.0 为 2019-07-16 的最新版本(2018-10-29 更新)
    【解决方案2】:

    简单地将 maven 插件版本增加到 3.3 或 3.4 不会解决任何问题(正如某些人所说)。

    您必须在正确的阶段添加最少的default-descriptor 执行。 所以构建信息的最小配置如下:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>3.1</version>
                <executions>
                    <execution>
                        <id>default-descriptor</id>
                        <phase>process-classes</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    maven-plugin-plugin 版本无关。 (可以是 3.1, 3.2, 3.3. 3.4(其他的没测试))。

    将产生:

    ...
    [INFO] --- maven-plugin-plugin:3.2:descriptor (default-descriptor) @ example-maven-plugin ---
    [WARNING] Using platform encoding (UTF-8 actually) to read mojo metadata, i.e. build is platform dependent!
    [INFO] Applying mojo extractor for language: java-annotations
    [INFO] Mojo extractor for language: java-annotations found 1 mojo descriptors.
    [INFO] Applying mojo extractor for language: java
    [INFO] Mojo extractor for language: java found 0 mojo descriptors.
    [INFO] Applying mojo extractor for language: bsh
    [INFO] Mojo extractor for language: bsh found 0 mojo descriptors.
    ...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    

    如果您不想在 pom 中包含构建标签,则可以在 Mojo 上使用 javadocs。例如:

    /**
     * @goal run123
     */
    @Mojo(name = "run123")
    public class MyMojo extends AbstractMojo {
    }
    

    将产生:

    ...
    [INFO] --- maven-plugin-plugin:3.2:descriptor (default-descriptor) @ example-maven-plugin ---
    [WARNING] Using platform encoding (UTF-8 actually) to read mojo metadata, i.e. build is platform dependent!
    [INFO] Applying mojo extractor for language: java-annotations
    [INFO] Mojo extractor for language: java-annotations found 0 mojo descriptors.
    [INFO] Applying mojo extractor for language: java
    [INFO] Mojo extractor for language: java found 1 mojo descriptors.
    [INFO] Applying mojo extractor for language: bsh
    [INFO] Mojo extractor for language: bsh found 0 mojo descriptors. 
    ...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    

    请参阅本指南了解更多信息 http://maven.apache.org/plugin-tools/maven-plugin-plugin/examples/using-annotations.html

    【讨论】:

    • 在构建插件时应该会自动添加描述符。此外,您不需要声明 maven-plugin-plugin 依赖项,因为在构建带有声明的包装 maven-plugin 的项目时也会自动找到它。但是,您可能无法获得最新版本 - 使用 mvn 3.3.9 我获得了 maven-plugin-plugin 3.2,因此我必须声明依赖项以指定 v3.5
    • ++ 这是唯一对我有用的答案。看起来插件默认运行在不同的阶段,这导致它找不到任何 mojo 条目。
    【解决方案3】:

    上面的答案很好——我想通过添加资源来查找最新版本来扩充它们: https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-plugin-plugin

    2019 年 4 月 2 日 -- 我遇到了上述相同的错误,并使用 3.6.0 解决了它

    【讨论】:

    • 它适用于目标是 11
    【解决方案4】:

    这可能与 Maven 中未解决的问题有关:https://issues.apache.org/jira/browse/MNG-5346

    对于我的插件项目,我可以通过添加 maven-plugin-plugin 的显式执行来解决:

    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-plugin-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <!-- see http://jira.codehaus.org/browse/MNG-5346 -->
                        <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
                    </configuration>
    
                    <executions>
                        <execution>
                            <id>mojo-descriptor</id>
                            <goals>
                                <goal>descriptor</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    

    但请参阅 JIRA 问题中的 cmets 以获得更详细的解决方案!

    【讨论】:

    • 我已阅读您发布的问题,并已在网站上实施了相关工作,并且成功了!感谢您向我指出:)
    • 此问题已在 3.2.2 中修复:maven.apache.org/docs/3.2.2/release-notes.html
    • 谢谢,这解决了我在运行 Maven 3.0.5 的机器上的构建问题
    • 五年后,当 maven-plugin-plugin 在 maven 3.6.3 上崩溃时,这个解决方案仍然有效。谢谢
    • 我仍然可以在 3.5+ 中重现该问题,但幸运的是,同样的解决方案仍然可以解决它!谢谢!
    【解决方案5】:

    在阅读了Jira Issue that Gyro posted 之后,我在我的 pom 中添加了以下几行,一切都编译得很好。

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <goalPrefix>mysql-jdbc-compliance</goalPrefix>
                </configuration>
                <executions>
                    <execution>
                        <id>default-descriptor</id>
                        <goals>
                            <goal>descriptor</goal>
                        </goals>
                        <phase>process-classes</phase>
                    </execution>
                    <execution>
                        <id>help-descriptor</id>
                        <goals>
                            <goal>helpmojo</goal>
                        </goals>
                        <phase>process-classes</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    【讨论】:

    • 如果您使用 maven 原型生成项目,此条目实际上会自动添加到插件的 pom.xmlmvn archetype:generate -DgroupId=com.example -DartifactId=my-awesome-maven-plugin -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-plugin
    • 使用 mvn 3.3.9 我只需要 groupId、artifactId 和 v3.5 - 所有配置都是默认的。
    【解决方案6】:

    最初我认为 Gyro 的回答解决了这个错误。但后来在执行目标时失败了。

    mvn sample.plugin:hello-maven-plugin:1.0-SNAPSHOT:sayhi

    生产

    [错误] 在可用目标中的插件 sample.plugin:hello-maven-plugin:1.0-SNAPSHOT 中找不到目标 'sayhi' -> [帮助 1]

    原来是这样

    skipErrorNoDescriptorsFound

    仅抑制错误。即它没有解决根本问题。我删除了此修复程序。

    之后解决方案很简单(原因纯粹是我的错)。当我创建 GreetingMojo.java 时,我将它放在以下目录中

    .../Development/my-maven-plugin/src/sample/plugin/GreetingMojo.java

    它必须在下面

    .../Development/my-maven-plugin/src/main/Java/sample/plugin/GreetingMojo.java

    【讨论】:

    • 谢谢,这让我犯了错误。在构建部分添加 src 将包含此源文件夹。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 2013-05-16
    • 1970-01-01
    • 2018-10-20
    相关资源
    最近更新 更多