如何为 Maven 插件定义默认 mojo
我编写了一个插件,可以在 target/ generated-sources/ 中生成一个文件。 这个插件只有一个魔力。这个魔力是用以下内容声明的:
/**
* @goal convertsql
* @phase generate-sources
* @requiresProject
*/
public class ConverterMojo extends AbstractMojo {
在项目中,我想使用该插件,但如果我不指定执行标签,它就不起作用:
<executions>
<execution>
<id>convert</id>
<goals><goal>convertsql</goal></goals>
<phase>generate-sources</phase>
</execution>
</executions>
我只想像这样配置插件:
<plugin>
<groupId>com.my.plugins</groupId>
<artifactId>sqlconverter</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<sourceFile>src/main/resources/sql/schema_oracle.sql</sourceFile>
</configuration>
</plugin>
是否可以指定我的插件的默认魔力?默认的目标和阶段是在mojo中定义的...我的意思是,当使用jar插件时,我不必告诉我要执行的目标,在哪个阶段...它是自动的。
谢谢!
I've written a plugin that generate one file in target/generated-sources/.
This plugin only has one mojo. This mojo is declared with the following :
/**
* @goal convertsql
* @phase generate-sources
* @requiresProject
*/
public class ConverterMojo extends AbstractMojo {
In the project, i want to use the plugin but it doesn't work if i don't specify the executions tag :
<executions>
<execution>
<id>convert</id>
<goals><goal>convertsql</goal></goals>
<phase>generate-sources</phase>
</execution>
</executions>
I would like to only configure the plugin like this :
<plugin>
<groupId>com.my.plugins</groupId>
<artifactId>sqlconverter</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<sourceFile>src/main/resources/sql/schema_oracle.sql</sourceFile>
</configuration>
</plugin>
Is it possible to specify the default mojo for my plugin ? The default goal and phase are defined in the mojo... I mean, when using the jar plugin, i don't have to tell the goal i want to execute, at which phase... it is automatic.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让 Maven 插件在默认阶段执行时自动运行其默认目标是不可能的。这很令人困惑,因为有许多针对特定包装的标准插件“绑定”。这些是在 Maven 核心中定义的: https://maven .apache.org/ref/3.6.1/maven-core/default-bindings.html
例如,对于 WAR 打包,它是:
通过在插件中定义默认阶段,您不必指定它,只是目标。在您的情况下:
另请参阅 https://maven.apache.org/developers/ mojo-api-specation.html(查找
@phase
)。相关引用(我的重点):Having your Maven plugin automatically run its default goal when its default phase executes is not possible. This is confusing because there are a lot of standard plugin ‘bindings’ for specific packagings. Those are defined in Maven core: https://maven.apache.org/ref/3.6.1/maven-core/default-bindings.html
For example, for WAR packaging it is:
By defining a default phase in your plugin you won’t have to specify that, just the goal. In your case:
Also see https://maven.apache.org/developers/mojo-api-specification.html (look for
@phase
). The relevant quote (my emphasis):您需要将
META-INF/plexus/components.xml
文件添加到您的插件中,并在插件块中设置true
。请参阅11.6.3。覆盖默认生命周期(来自 Maven Book)以供参考
You need to add a
META-INF/plexus/components.xml
file to your plugin and set<extensions>true</extensions>
in your plugin block.See 11.6.3. Overriding the Default Lifecycle from the Maven Book for reference