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”为空/空时,如何确定 Maven 跳过插件的执行?

4

3 回答 3

4

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

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

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

空值不能定义一个pom文件原因<value></value>等价于<value/>未定义的意思。

更新:

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

于 2014-10-15T09:09:21.133 回答
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 

然后插件将被跳过

于 2014-10-15T09:17:29.780 回答
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 文档,当系统属性根本没有定义时,skip-dependency-and-clean-plugins配置文件将被激活。my.prop

于 2019-04-08T10:09:08.623 回答