Java 基本的 maven 插件项目不工作,Mojo 插件描述符不生成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18999401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:13:56  来源:igfitidea点击:

Basic maven plugin project not working, Mojo plugin descriptors not generating

javamavenmaven-plugin

提问by coderatchet

I am following the tutorialfor creating a maven plugin and cannot run mvn install without getting errors. The info complains that i don't have the required mojo descriptors when the annotations should be generating them for me. I am running maven 3.0.5 and using intellij as my ide. here is my Main class:

我正在遵循创建 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());
        }
    }
}

And here is my pom.xml

这是我的 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>

Note:I had to separately add the annotations dependency as the main plugin api did not contain these classes. when i run mvn install on my project, the output is as follows:

注意:我必须单独添加注释依赖项,因为主插件 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

采纳答案by coderatchet

after reading the Jira Issue that Gyro posted, i added the following lines to my pom and everything compiled nicely.

阅读Gyro 发布Jira 问题后,我将以下几行添加到我的 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>

回答by Gyro Gearless

Maybe this is related to a unresolved issue in Maven: https://issues.apache.org/jira/browse/MNG-5346

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

For my plugin projects, i could workaround by adding an explicit execution of the maven-plugin-plugin:

对于我的插件项目,我可以通过添加 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>

But see the comments in the JIRA issue for more elaborate solutions!

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

回答by Jmini

As mentioned in the previous answer, this was a bug and it is now fixed.

正如之前的回答中提到的,这是一个错误,现在已修复。

You just need to tell maven it should use a newer version of the maven-plugin-plugin.

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

Here is how my pom file looks like:

这是我的 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>

回答by randomUser56789

Simply incrementing maven plugin version to 3.3 or 3.4

只需将 Maven 插件版本增加到 3.3 或 3.4

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-plugin-plugin</artifactId>
        <version>3.3</version>
      </plugin>
    </plugins>
  </build>

won't fix any issue (as some has stated).

不会解决任何问题(正如某些人所说)。

You have to add a minimum of default-descriptorexecution with the correct phase. So the minimum config of build information is as follows:

您必须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>

irrespective of maven-plugin-pluginversion. (it can be 3.1, 3.2, 3.3. 3.4 (didn't test the others)).

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

will yield:

将产生:

...
[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] ------------------------------------------------------------------------

Another option if you don't want to have build tags in your pom, you can use javadocs on your Mojo. For instance:

如果你不想在你的 pom 中有构建标签,你可以在你的 Mojo 上使用 javadocs。例如:

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

will yield:

将产生:

...
[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] ------------------------------------------------------------------------

Please refer to this guide for more info http://maven.apache.org/plugin-tools/maven-plugin-plugin/examples/using-annotations.html

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

回答by Shane Gannon

Initially I thought Gyro's answer fixed the error. But later when executing the goal it failed.

最初我认为 Gyro 的回答修复了错误。但是后来在执行目标时失败了。

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

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

produced

产生

[ERROR] Could not find goal 'sayhi' in plugin sample.plugin:hello-maven-plugin:1.0-SNAPSHOT among available goals -> [Help 1]

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

Turns out that

原来如此

skipErrorNoDescriptorsFound

跳过错误NoDescriptorsFound

only suppressed the error. i.e. It did not resolve the underlying problem. I removed this fix.

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

After that the solution was simple (and the cause purely my fault). When I created GreetingMojo.java I placed it in the following directory

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

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

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

It needed to be under

它需要在

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

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

回答by solbs

Great answers above -- I'd like to augment them by adding a resource to find the latest version: https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-plugin-plugin

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

April 2, 2019 -- I encountered the same error above and resolved it by using 3.6.0

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