【问题标题】:Conditional execution of maven plugin depending on profile property根据配置文件属性条件执行maven插件
【发布时间】:2020-06-03 12:12:15
【问题描述】:

给定 pom.xml

<plugin>
    <groupId>com.myplugin</groupId>
    <artifactId>my-plugin</artifactId>
    <version>1.0.0</version>
    <executions>
        <execution>
            <id>generate-file</id>
            <phase>package</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <templateFile>
                    ${project.build.directory}/template.json
                </templateFile>
                <outputFile>
                    ${project.build.directory}/${env}-${server}-file.json
                </outputFile>
            </configuration>
        </execution>
        <execution>
            <id>send-file</id>
            <phase>package</phase>
            <goals>
                <goal>send</goal>
            </goals>
            <configuration>
                <file>
                    ${project.build.directory}/${env}-${server}-file.json
                </file>
            </configuration>
        </execution>
    </executions>
</plugin>

<profiles>
    <profile>
        <id>dev-a</id>
        <properties>
            <env>test</env>
            <server>serverA</server>
        </properties>
    </profile>
    <profile>
        <id>prod-a</id>
        <properties>
            <env>prod</env>
            <server>serverA</server>
        </properties>
    </profile>
    <profile>
        <id>dev-b</id>
        <properties>
            <env>test</env>
            <server>serverB</server>
        </properties>
    </profile>
    <profile>
        <id>prod-b</id>
        <properties>
            <env>prod</env>
            <server>serverB</server>
        </properties>
    </profile>
</profiles>

只有当env 属性等于prod 时,我才能执行send-file 任务?我知道在该示例中我可以将plugin 标签移动到dev-aprod-a 配置文件,但我需要为每个配置文件复制该部分,这似乎不是一个好的解决方案。在我的情况下,我定义了更多配置文件并且插件执行定义更长,它会使我的 pom 文件混乱很多并且使任何更改都非常容易出错。 是否有任何条件标签可以传递给执行,让我决定是否调用它?或者是否有可能通过配置文件标签中的 id 排除执行?

感谢您的帮助!

【问题讨论】:

  • 这能回答你的问题吗? Using maven profiles to control build execution
  • @KarthikeyanVaithilingam 不,不是真的。正如我在问题中所推理的那样,他们正在描述一种我想避免的方法。我需要为大多数配置文件执行相同的插件,但不是全部,我不想只复制文件的一部分。
  • 看来这个插件不适合使用...这应该由插件处理,而不是通过配置文件等。

标签: maven pom.xml


【解决方案1】:

如果my-plugin 真的是你的插件(即你写的),最简单的事情就是在插件中实现你想要的逻辑。

【讨论】:

  • 如果不是呢?有没有办法在不向插件代码添加此类逻辑的情况下禁用具有某些属性的插件的执行?
  • 没有好的方法,除了配置文件。您可以有条件地使用另一个插件来设置插件的跳过属性。让我再补充一句:通常不建议为不同的环境(如 dev、prod 等)使用不同的构建。通常你在所有阶段都使用相同的工件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 2018-07-15
  • 2013-09-24
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
相关资源
最近更新 更多