Why can Maven not find my custom Mojo?

2020-06-01 01:44发布

问题:

I have an own Mojo class.

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

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

After that I install it in local repository.

 [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

But when try to call 'mojo' goal I got en error

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

what is the problem?

Here is maven-plugin-plugin configuration.

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

Old mechanism with javadoc annotations works well, but i want to use java annotation.

<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 

Why default-descriptor is enabled instead of mojo-descriptor?

回答1:

Add this section to your plugin's 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. See maven-compiler-plugin:3.0 sources for full working example of building MOJOs with annotations



回答2:

EDIT (addressing the use of the Mojo annotations):

I tried building a plugin with the annotations and ran into the same issue. I solved it by binding the plugin to a default lifecycle phase as shown below in the @Mojo annotation:

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>

POM of Project Invoking Mojo

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