基本的maven插件项目无法正常工作,Mojo插件描述符无法生成

cod*_*het 63 java maven-plugin maven

我正在按照创建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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

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

Gyr*_*ess 55

也许这与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>
Run Code Online (Sandbox Code Playgroud)

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

  • 此问题已在3.2.2中修复:http://maven.apache.org/docs/3.2.2/release-notes.html (7认同)
  • 五年后,当 maven-plugin-plugin 在 maven 3.6.3 上崩溃时,这个解决方案仍然有效。谢谢 (2认同)

cod*_*het 36

在阅读了Gyro发布Jira Issue之后,我将以下几行添加到了我的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>
Run Code Online (Sandbox Code Playgroud)

  • 如果使用maven archetype生成项目,则此条目实际上会自动添加到插件的`pom.xml`中:`mvn archetype:generate -DgroupId = com.example -DartifactId = my-awesome-maven-plugin -DarchetypeGroupId = org.apache .maven.archetypes -DarchetypeArtifactId = maven-archetype-plugin` (6认同)
  • 使用mvn 3.3.9我只需要groupId,artifactId和v3.5 - 所有配置都是默认的. (2认同)

Jmi*_*ini 29

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

你只需要告诉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>
Run Code Online (Sandbox Code Playgroud)


ran*_*789 13

只需将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>
Run Code Online (Sandbox Code Playgroud)

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

您必须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>
Run Code Online (Sandbox Code Playgroud)

无论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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

另一个选择如果你不想在你的pom中有构建标签,你可以在你的Mojo上使用javadocs.例如:

/**
 * @goal run123
 */
@Mojo(name = "run123")
public class MyMojo extends AbstractMojo {
}
Run Code Online (Sandbox Code Playgroud)

会产生:

...
[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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅本指南 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 (3认同)

sol*_*lbs 5

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

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