首页 > 解决方案 > Maven 项目属性是否具有一次性语义

问题描述

我今天观察到一个让我感到困惑的 Maven 行为。要么项目属性具有某种一次性写入语义,要么......我找不到任何可以解释这种行为的参考文档。

我可以在验证阶段动态设置 Maven 属性,以便在以后的阶段将它们评估为插件属性吗?

目标

我想根据其他属性的存在跳过配置文件中插件的执行。

计划

  1. validate在阶段运行 Groovy 插件。
  2. 评估该其他属性的存在。
  3. 设置跳过项目属性。
  4. 使用 skip 属性来配置在后续阶段运行的插件。

POM 摘录

<profile>
    <id>my-id</id>
    <activation>
        <file>
            <exists>${basedir}/somefile</exists>
        </file>
    </activation>
    <properties>
        <skipIt>false</skipIt> <!-- intention: use this as default value -->
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <scripts>
                                <script>
                                <![CDATA[
                                def someOtherProp = project.properties['some-other-property']
                                def skipIt = someOtherProp ? 'false' : 'true'
                                project.properties['skipIt'] = skipIt
                                println skipIt
                                println project.properties['skipIt']
                                ]]>
                                </script>
                            </scripts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <configuration>
                    <skip>${skipIt}</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>my-unpack</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>

行为

如果some-other-property不存在(即未设置),则打印true两次。这意味着 Groovy 评估是正确的并且skipIt设置正确。然而,依赖插件仍然运行my-unpack执行。为什么?

但是,如果我在第 9 行删除<skipIt>false</skipIt>,一切都会按我的预期工作。为什么?

标签: maven

解决方案


推荐阅读