1

Given 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>

How can I execute send-file task only when env property is equal to prod? I know I can move the plugin tag to dev-a and prod-a profiles in that example but I would need to duplicate that part for each profile which doesn't seem to be good solution. In my case I have many more profiles defined and the plugin execution definition is much longer, it would clutter my pom file a lot and make any changes very error-prone. Is there any condition tag that I can pass to execution that would let me decide if it is invoked or not? Or maybe is there a possibility to exclude executions by id in profile tag?

Thanks for help!

Please
  • 328
  • 3
  • 17
  • 1
    Does this answer your question? [Using maven profiles to control build execution](https://stackoverflow.com/questions/4684837/using-maven-profiles-to-control-build-execution) – seenukarthi Feb 19 '20 at 11:59
  • @KarthikeyanVaithilingam No, not really. They are describing an approach that I wanted to avoid as I reasoned in my question. I need to execute the same plugin for most profiles but not all, I don't want to just duplicate parts of file. – Please Feb 19 '20 at 12:23
  • It looks like the plugin is not the right one to use...this should be handled by the plugin and not via profiles etc. – khmarbaise Feb 19 '20 at 19:30

1 Answers1

0

If my-plugin is really your plugin (i.e. you wrote it), the easiest thing would be to just implement the logic you want inside the plugin.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • And what if it's not? Isn't there any way to disable execution of plugin with some property without adding such logic to plugin code? – Please Feb 20 '20 at 08:22
  • No nice ways, except for profiles. You can maybe set the skip property of the plugin by use of another plugin, conditionally. Let me also add a comment: It is usually not recommended to have different builds for different environments like dev, prod etc. Usually you use the very same artifact in all stages. – J Fabian Meier Feb 20 '20 at 08:39