首页 > 解决方案 > 根据配置文件属性条件执行maven插件

问题描述

给定 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>

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

感谢帮助!

标签: mavenpom.xml

解决方案


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


推荐阅读