实际上我正在使用maven插件并创建自己的maven插件。
首先我遵循了本教程:http://maven.apache.org/guides/plugin/guide-java-plugin-development.html但我意识到这不再起作用,因为"org.apache.maven.plugins.annotations.Mojo"
不存在!
之后我发现它现在应该如何运作。因为我创建了这个类:
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);
}
}
我的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/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>
所以现在使用mvn de.sample.plugin运行我的插件:hello-maven-plugin:0.0.1-SNAPSHOT:hello
工作良好。但使用此:mvn de.sample.plugin:hello-maven-plugin:0.0.1-SNAPSHOT:hello hello.message="Test"
会导致此错误:
未知生命周期阶段"hello.message=Test"
。您必须以........
但它应该有用吗?有人可以帮忙吗?
答案 0 :(得分:1)
知道了!
@parameter default-value="Hallo World!" expression="${hello.message}"
和
mvn de.sample.plugin:hello-maven-plugin:0.0.1-SNAPSHOT:hello -Dhello.message="Test"
答案 1 :(得分:1)
“org.apache.maven.plugins.annotations.Mojo”存在!如果在插件的pom.xml中添加此依赖项,则可以在项目中使用它:
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.2</version>
<scope>provided</scope>
</dependency>