为什么Maven找不到我的自定义Mojo?

时间:2013-01-18 16:51:42

标签: java maven maven-plugin

我有自己的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

但是当试图调用'mojo'目标时我得到了错误

   [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-plugin配置。

<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 

为什么启用default-descriptor而不是mojo-descriptor?

2 个答案:

答案 0 :(得分:32)

将此部分添加到插件的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-compiler-plugin:3.0源代码,了解使用注释构建MOJO的完整工作示例

答案 1 :(得分:5)

编辑(解决使用Mojo注释):

我尝试使用注释构建插件并遇到同样的问题。我通过将插件绑定到默认生命周期阶段来解决它,如下面@Mojo注释所示:

<强>魔

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

Mojo 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>

项目调用Mojo的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>
相关问题