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

我正在按照 教程创建一个 maven 插件,不能运行 mvn install 而不出错。信息抱怨说我没有所需的魔力描述符,而注释应该为我生成它们。我正在运行 Maven3.0.5,并使用 intellij 作为我的主页。这是我的主要课程:

@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
69429 次浏览

也许这与 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 问题中的评论,以获得更详细的解决方案!

在阅读了 陀螺发布的吉拉问题之后,我将下面的代码添加到我的 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 它应该使用更新版本的 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>

简单地将 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 中使用 build 标记,可以在 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

起初我以为陀螺仪的答案修正了错误,但是后来在执行目标时失败了。

Plugin: hello-maven-plugin: 1.0-SNAPSHOT: ayhi

制作

[错误]无法在 plugin sample.plugin: hello-maven-plugin: 1.0-SNAPSHOT 中找到目标‘ sahi’-> [ Help 1]

原来

发现

只是抑制了错误。也就是说,它没有解决根本的问题。我删除了这个修复。

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

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

它需要在下面

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

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

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