我正在尝试编写Hello World Maven插件。
我已将defaultPhase=LifecyclePhase.CLEAN
分配给它,但当我执行mvn clean
时,它无效。
当我执行mvn gorov:clean
时,它正在运行。
有什么建议吗?
DS
代码为:
项目中的位置: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);
}
}
}
插件的pom.xml
:
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>
我运行pom.xml
命令的mvn
:
项目中的位置: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>
答案 0 :(得分:0)
您可以按以下方式调用插件的源代码
<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>
您必须在执行中指定目标。如果你键入&#34; maven clean&#34;,你只需要调用maven clean插件,而不是你的插件。插件可能包含多个目标,除非您在项目的执行中定义了您想要运行的目标,否则它们将无法运行。
请看这里:https://maven.apache.org/guides/plugin/guide-java-plugin-development.html
答案 1 :(得分:0)
如果只输入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,
[...].
如果您输入mvn clean
,则不会调用插件的clean
目标,而是the clean
phase of the clean
lifecycle(其clean
目标[maven-]clean[-plugin]
}插件默认绑定到它。
如果您输入mvn gorov:clean
,那么您使用插件POM中gorov
中声明的plugin prefix <goalPrefix>gorov</goalPrefix>
(虽然此声明不会是&#39} ; t是必要的,因为默认情况下会从插件gorov-maven-plugin
的工件ID派生相同的前缀。