Maven - 如果属性为空/ null,则跳过插件

问题描述 投票:2回答:3

我想获得以下行为:当我为属性“my.prop”指定一个值时,我希望执行依赖项和清理插件。如果没有为该属性指定值,我希望跳过它们。

我创建了“my.prop”,如下所示:

<properties>
    <my.prop></my.prop>
</properties>

然后我读到配置文件激活只适用于系统属性,所以我删除了上面的并使用了surefire插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.17</version>
    <configuration>
        <systemPropertyVariables>
            <my.prop></my.prop>
        </systemPropertyVariables>
    </configuration>
</plugin>

我尝试使用配置文件,像这样:

<profiles>
    <profile>
        <id>default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <skipDependecyAndCleanPlugins>false</skipDependecyAndCleanPlugins>
        </properties>
    </profile>
    <profile>
        <id>skip-dependency-and-clean-plugins</id>
        <activation>
            <property>
                <name>my.prop</name>
                <value></value>
                <!-- I also tried:  <value>null</value> without success.-->
            </property>
        </activation>
        <properties>
            <skipDependecyAndCleanPlugins>true</skipDependecyAndCleanPlugins>
        </properties>
    </profile>
</profiles>

之后,对于每个插件,我都会这样做:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.9</version>
    <configuration>
        <skip>${skipDependecyAndCleanPlugins}</skip>
    </configuration>
    ....
</plugin>

但插件仍然执行...

当“my.prop”为空/ null时,如何确定Maven跳过插件的执行?

maven maven-2 maven-plugin pom.xml maven-profiles
3个回答
3
投票

最简单的解决方案是使用以下形式的激活:

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

以上意味着你可以为debug定义任何值,这意味着-Ddebug就足够了。

空值不能定义为pom文件,因为<value></value>等同于<value/>,这意味着与未定义相同。

更新:

我建议使用个人资料而不是财产。所以你可以简单地在命令行mvn -Pxyz install上定义或保留它。


0
投票

您可以在插件的配置中使用my.prop属性:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.9</version>
    <configuration>
        <skip>${my.prop}</skip>
    </configuration>
    ....
</plugin>   

现在执行时:

mvn ... -Dmy.prop=true 

然后将跳过插件


0
投票

你非常接近。您可以在配置文件激活中使用!my.prop语法实现您所描述的内容。

<build>
  <plugins>
    <plugin>
      <artifactId>maven-clean-plugin</artifactId>
      <configuration>
        <skip>${skipDependecyAndCleanPlugins}</skip>
      </configuration>
    </plugin>
  </plugins>
</build>

<profiles>
  <profile>
    <id>skip-dependency-and-clean-plugins</id>
    <activation>
      <property>
        <name>!my.prop</name>
      </property>
    </activation>
    <properties>
      <skipDependecyAndCleanPlugins>true</skipDependecyAndCleanPlugins>
    </properties>
  </profile>
</profiles>

根据Maven documentation,当系统属性skip-dependency-and-clean-plugins没有定义时,my.prop配置文件将被激活。

© www.soinside.com 2019 - 2024. All rights reserved.