为什么行家找不到我的自定义魔咒?(Why can Maven not find my custom

2019-08-07 08:20发布

我有一个自己的Mojo类。

 @Mojo(name="mojo", threadSafe=true)
 public class MyMojo extends AbstractMojo
 {
     @Component
     private MavenProject project;

     public void execute() throws MojoExecutionException, MojoFailureException
     {
        getLog().info("Execute");
     }
  }

从那以后,我安装在本地仓库。

 [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 0 mojo descriptors.
 [INFO] Applying mojo extractor for language: bsh
 [INFO] Mojo extractor for language: bsh found 0 mojo descriptors.
 ....
 [INFO] BUILD SUCCESS

但是,当尝试调用“魔力”的目标我有恩的错误

   [ERROR] Could not find goal 'mojo' in plugin my.plugins:my-plugin:1.0-SNAPSHOT among available goals -> [Help 1]

what is the problem?

这里是Maven的插件,插件配置。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-plugin-plugin</artifactId>
    <version>3.2</version>
    <configuration>                                                 
        <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
     </configuration>
 </plugin>

旧机制的javadoc注释工作得很好,但我想用java注解。

<dependency>
    <groupId>org.apache.maven.plugin-tools</groupId>
    <artifactId>maven-plugin-annotations</artifactId>
    <version>3.2</version>
</dependency>


 [INFO] --- maven-plugin-plugin:3.2:descriptor (default-descriptor) @ bla-mvn-plugin 

为什么默认描述符被启用,而不是魔力描述符?

Answer 1:

本节添加到您的插件的POM:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
                </configuration>
                <executions>
                    <execution>
                        <id>mojo-descriptor</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>descriptor</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

PS。 见Maven的编译器插件:3.0的来源与注解建设MOJOs的全面工作示例



Answer 2:

编辑(寻址使用魔注释):

我试图建设有注释的插件,遇到同样的问题。 我通过结合插件到缺省的生命周期阶段解决它如下面所示@Mojo注释:

@Mojo(name = "hello", defaultPhase = LifecyclePhase.INSTALL)
public class MyMojo extends AbstractMojo
{
    public void execute() throws MojoExecutionException, MojoFailureException
    {
        getLog().info("Hello");
    }
}

我的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>
    <groupId>testware.mojotest</groupId>
    <artifactId>mojotest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mojotest</name>
    <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>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

项目调用魔的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>
    <groupId>testware.mojotest</groupId>
    <artifactId>mojotest-runner</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mojotest-runner</name>

    <build>
        <plugins>
            <plugin>
                <groupId>testware.mojotest</groupId>
                <artifactId>mojotest</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>hello</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>


文章来源: Why can Maven not find my custom Mojo?