Maven之如果属性为空/空则跳过插件

thcjp 阅读:32 2025-02-15 21:57:57 评论:0

我想获得以下行为:当我为属性“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 跳过插件的执行?

请您参考如下方法:

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

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

以上意味着您可以为调试定义任何值,这意味着 -Ddebug就够了。

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

更新:

我建议使用配置文件而不是属性。因此,您可以简单地在命令行上定义 mvn -Pxyz install 或保留它。


标签:Maven
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号