Maven插件开发:有没有办法在pom.xml中声明插件时自动运行插件?
我正在开发一种新的魔力。它只有一个目标,因此不强迫用户添加 executions
部分(如果他们不想更改默认阶段
)似乎是合乎逻辑的。
这应该是可能的,因为当我添加一个非常简单的 surefire
插件描述时,maven 知道它的单一目标 test
应该运行,即:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
</plugin>
这足以运行插件。
我怎样才能为我的插件实现类似的小配置?
这是我现在所拥有的(如果没有 executions
部分,它就无法工作):
/**
*
* @goal test
* @phase test
*/
public class MyMojoPlugin extends AbstractMojo {
... (implementation details)
}
<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>org.somegroup</groupId>
<artifactId>mymojo-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
.... (other dependencies)
</dependencies>
<build>
<plugins>
... (some plugins)
</plugins>
</build>
</project>
I'm developing a new mojo. It has only one goal so it seems logical not to force a user to add an executions
section (if they don't want to change a default phase
).
This should be possible because when I add a very simple description of a surefire
plugin, maven understands that its single goal test
should be run, i.e.:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
</plugin>
and this is enough to run the plugin.
How can I achieve similarly small configuration for my plugin?
Here is what I have now (and it doesn't work without executions
section):
/**
*
* @goal test
* @phase test
*/
public class MyMojoPlugin extends AbstractMojo {
... (implementation details)
}
<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>org.somegroup</groupId>
<artifactId>mymojo-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
.... (other dependencies)
</dependencies>
<build>
<plugins>
... (some plugins)
</plugins>
</build>
</project>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“默认”生命周期绑定是在 Maven 中定义的。 Build Lifecycle 文档对此进行了描述,该文档还表明插件需要“executions”部分。
我想你可以编辑 components.xml 并为您的特定站点重新编译 Maven。
在我看来,至少对于内部开发来说,更好的选择是使用包含特定于站点的配置的共享 Parent POM。
编辑:我正在寻找对父 POM 的引用,并看到 这个。我不确定这是否是 Maven3 独有的功能,但它可能值得研究。
The "default" lifecycle bindings are defined within Maven. This is described in the Build Lifecycle document, which document also indicates that the "executions" section is required for plugins.
I suppose that you could edit components.xml and recompile Maven for your particular site.
In my opinion a better alternative, for in-house development at least, is to use a shared Parent POM that contains site-specific configuration.
Edit: I was looking for a reference to Parent POMs and saw this. I'm not sure if that's a Maven3-only feature, but it's probably worth investigating.