Maven:仅当存在命令行标志时才执行插件 [英] Maven: Only execute plugin when a command line flag is present

查看:97
本文介绍了Maven:仅当存在命令行标志时才执行插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用mvn命令时,我希望Maven仅在命令行上有标志时才运行某个插件.

I want Maven to only run a certain plugin when there is a flag on the command line when I call the mvn command.

例如:

假设我有一个名为maven-foo-plugin的插件.我只希望当我调用maven命令时出现标志--foo时,maven才能运行此插件.

Let's say I have a plugin called maven-foo-plugin. I only want maven to run this plugin when the flag --foo is present when I call the maven command.

所以,不用说...

mvn install

...我会说...

...I would say...

mvn install --foo

第一个不应使用/调用插件maven-foo-plugin,但第二个应使用/调用插件.默认情况下,我不希望此插件运行,但是当且仅当--foo存在.我要添加到此maven-foo-plugin的另一个参数来使其执行此操作吗?

The first one should NOT use/call the plugin maven-foo-plugin, but the second one should. By default, I don't want this plugin to run, but if and only if, the --foo is present. Is there another parameter that I add to this maven-foo-plugin to make it do this?

我正在使用的插件是maven-antrun-plugin,其任务是将文件解压缩.当然,有时此文件存在,有时不存在.有时,它不存在,我不需要它.因此,我需要一种方法(最好是通过命令行,除非有人有更好的主意)打开/关闭此插件.

The plugin I'm using is the maven-antrun-plugin that has the task that unzips a file. Of course, sometimes this file is present and sometimes not. Sometimes, it's not present and I don't need it. So, I need a way (Preferably through command line unless someone has a better idea) to turn on/off this plugin.

推荐答案

如@Gab所述,使用Maven配置文件是可行的方法.创建一个配置文件并在该配置文件中配置您的插件.然后在配置文件的activation部分中,引用带或不带值的环境变量:

As @Gab has mentioned, using Maven profiles is the way to go. Create a profile and configure your plugin in the profile. Then in the profile's activation section, reference the environment variable, with or without a value:

<profiles>
  <profile>
    <activation>
      <property>
        <name>debug</name>
      </property>
    </activation>
    ...
  </profile>
</profiles>

如果在调用Maven时定义了debug变量,则上面的示例将激活配置文件:

The above example would activate the profile if you define the debug variable when calling Maven:

mvn install -Ddebug

请注意,必须将环境变量加上-D前缀,才能将它们传递给运行Maven的JVM.

Please note that you have to prefix environment variables with -D in order to pass them to the JVM running Maven.

更多详细信息可以在这里找到: http://maven.apache .org/guides/introduction/introduction-to-profiles.html

More details can be found here: http://maven.apache.org/guides/introduction/introduction-to-profiles.html

这篇关于Maven:仅当存在命令行标志时才执行插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆