简体   繁体   English

Maven:仅当存在命令行标志时才执行插件

[英]Maven: Only execute plugin when a command line flag is present

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仅在调用mvn命令时在命令行上有标志时才运行某个插件。

for example: 例如:

Let's say I have a plugin called maven-foo-plugin . 假设我有一个名为maven-foo-plugin I only want maven to run this plugin when the flag --foo is present when I call the maven command. 我只希望当调用maven命令时出现--foo标志时,maven才能运行此插件。

So, instead of saying... 所以,不用说...

mvn install

...I would say... ...我会说...

mvn install --foo

The first one should NOT use/call the plugin maven-foo-plugin , but the second one should. 第一个不应该使用/调用插件maven-foo-plugin ,但是第二个应该。 By default, I don't want this plugin to run, but if and only if, the --foo is present. 默认情况下,我不希望此插件运行,但是当且仅当--foo存在。 Is there another parameter that I add to this maven-foo-plugin to make it do this? 我是否要在此maven-foo-plugin添加另一个参数以使其执行此操作?

The plugin I'm using is the maven-antrun-plugin that has the task that unzips a file. 我使用的插件是maven-antrun-plugin ,其任务是将文件解压缩。 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. 因此,我需要一种方法(最好是通过命令行,除非有人有更好的主意)打开/关闭此插件。

The correct way to trigger conditional action in maven is to use profile. 在Maven中触发条件操作的正确方法是使用配置文件。 You can so configure a specific profile including the plugin activation, you will then trigger the execution using 您可以配置一个特定的配置文件,包括激活插件,然后使用

mvn targetPhase -P myprofile

(you can also specify a specific property value to activate the profile) (您也可以指定特定的属性值来激活配置文件)

see Maven: Using a Plugin Based On Profile 请参阅Maven:使用基于配置文件的插件

As @Gab has mentioned, using Maven profiles is the way to go. 正如@Gab提到的,使用Maven配置文件是正确的方法。 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: 然后,在配置文件的activation部分中,引用环境变量(带或不带值):

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

The above example would activate the profile if you define the debug variable when calling Maven: 如果在调用Maven时定义了debug变量,则上面的示例将激活配置文件:

mvn install -Ddebug

Please note that you have to prefix environment variables with -D in order to pass them to the JVM running Maven. 请注意,必须将环境变量添加-D前缀,才能将其传递给运行Maven的JVM。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM