Maven Mojo Unknown lifecycle phase

3.2k Views Asked by At

Actually im working with maven plugins and creating own maven plugins.

first i followed this tutorial: http://maven.apache.org/guides/plugin/guide-java-plugin-development.html but i realised that this isnt working anymore cause somehow "org.apache.maven.plugins.annotations.Mojo" doesnt exists!

After that i found out how it should work now. for that i created this class:

 import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

/**
 * @goal hello
 * @requiresProject false
 */
public class MyMojo extends AbstractMojo {
/**
 * message to print
 * 
 * @parameter property="hello.message" default-value="Hallo World!"
 */
private String message = "";

/**
 * 
 */
public void execute() throws MojoExecutionException, MojoFailureException {
    getLog().info(message);
}
}

My pom.xml is this:

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.sample.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>hello-maven-plugin Maven Mojo</name>
<url>http://maven.apache.org</url>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <compilerVersion>1.7</compilerVersion>
                <target>1.7</target>
                <source>1.7</source>
            </configuration>
        </plugin>
    </plugins>
</build>


<dependencies>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
    </dependency>
</dependencies>

</project>

so now running my plugin with mvn de.sample.plugin:hello-maven-plugin:0.0.1-SNAPSHOT:hello works fine. But using this: mvn de.sample.plugin:hello-maven-plugin:0.0.1-SNAPSHOT:hello hello.message="Test" leads to this error:

Unknown lifecycle phase "hello.message=Test". You must specify a valid lifecylce Phase or a goal in the format........

But it should work? Can someone help?

2

There are 2 best solutions below

0
On

Got it!

@parameter default-value="Hallo World!" expression="${hello.message}"

and

mvn de.sample.plugin:hello-maven-plugin:0.0.1-SNAPSHOT:hello -Dhello.message="Test"
0
On

"org.apache.maven.plugins.annotations.Mojo" exists! You can use it in project, if adding this dependency in your plugin's pom.xml:

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