Reputation: 698
I am trying to write a Hello World Maven plugin.
I assigned defaultPhase=LifecyclePhase.CLEAN
to it but when I execute mvn clean
it is not working.
When I execute mvn gorov:clean
it is working.
Any suggestions?
DS
The code is:
Location in project: maven-plugin\src\main\java\com\gorovdude\plugins\maven\mojos\WriteConsoleMojo.java
package com.gorovdude.plugins.maven.mojos;
import java.io.File;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
@Mojo(name = "clean", defaultPhase = LifecyclePhase.CLEAN, threadSafe = true, aggregator = true)
public class WriteConsoleMojo extends AbstractMojo {
public void execute() throws MojoExecutionException, MojoFailureException {
try {
String path = "C:\\Apache-maven-3.2.5\\testing.txt";
System.out.println("testing");
File f = new File(path);
f.createNewFile();
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
}
The pom.xml
of the plugin:
Location in maven-plugin\pom.xml
<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>com.gorovdude</groupId>
<artifactId>gorov-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<description>gorov Maven Plugin</description>
<properties>
<mavenVersion>3.0</mavenVersion>
<mavenPluginVersion>3.1</mavenPluginVersion>
</properties>
<prerequisites>
<maven>${mavenVersion}</maven>
</prerequisites>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>${mavenPluginVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>2.0.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>${mavenPluginVersion}</version>
<executions>
<execution>
<id>generate-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
<configuration>
<goalPrefix>gorov</goalPrefix>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
</plugin>
</plugins>
</build>
</project>
The pom.xml
that I run mvn
commands on:
Location in Project: maven-plugin\src\test\pom.xml
<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>com.gorovdude</groupId>
<artifactId>test-gorov-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Testing the plugin</description>
<build>
<plugins>
<plugin>
<groupId>com.gorovdude</groupId>
<artifactId>gorov-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
</plugin>
</plugins>
</build>
</project>
Upvotes: 5
Views: 4383
Reputation: 14762
See the help text if you enter just mvn
:
No goals have been specified for this build. You must specify a valid
lifecycle phase or a goal in the format <plugin-prefix>:<goal>
[...].
Available lifecycle phases are:
[...], clean,
[...].
If you enter mvn clean
you're not invoking the clean
goal of your plugin but the clean
phase of the clean
lifecycle (which has the clean
goal of the [maven-]clean[-plugin]
plugin bound to it by default).
If you enter mvn gorov:clean
you're using the plugin prefix gorov
declared in <goalPrefix>gorov</goalPrefix>
in your plugin's POM (though this declaration wouldn't be necessary since the same prefix would be derived from the artifact ID of your plugin gorov-maven-plugin
by default).
See also Maven: Lifecycle vs. Phase vs. Plugin vs. Goal.
Upvotes: 1
Reputation: 3002
Your source code of the plugin can be called as following
<build>
<plugins>
<plugin>
<groupId>com.gorovdude</groupId>
<artifactId>gorov-maven-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<executions>
<execution>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You have to specify the goal in the executions. if you type "maven clean", you just call the maven clean plugin, not yours. A plugin may contain multiple goals, unless you define in the executions of you project, which the goals you want to run, they won't run.
Look here: https://maven.apache.org/guides/plugin/guide-java-plugin-development.html
Upvotes: 4