<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.mule.runtime.plugins</groupId>
        <artifactId>mule-plugins-maven-plugin-parent</artifactId>
        <version>1.11.7</version>
    </parent>

    <artifactId>mule-module-parent-pom-holder</artifactId>
    <packaging>pom</packaging>

    <properties>
        <parent.pom.groupId>org.mule.extensions</parent.pom.groupId>
        <parent.pom.artifactId>mule-modules-parent</parent.pom.artifactId>
    </properties>

    <build>
        <resources>
            <resource>
                <directory>src/main/pom</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <!-- Default the deploy target to the snapshot repo as RUNTIME properties.
                         These must NOT live in <properties>: a <properties> value shadows the
                         parse-alt-deployment-repository profile's regex-property override
                         (build-helper cannot override a model <properties> value), which is what
                         sent RC builds to mule-snapshots. As runtime properties they share the
                         profile's precedence, and the profile's executions run later and win. -->
                    <execution>
                        <id>default-deploy-repo-id</id>
                        <phase>initialize</phase>
                        <goals><goal>regex-property</goal></goals>
                        <configuration>
                            <name>deployRepoId</name>
                            <value>${project.distributionManagement.snapshotRepository.id}</value>
                            <regex>.*</regex>
                            <replacement>$0</replacement>
                            <failIfNoMatch>false</failIfNoMatch>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-deploy-repo-url</id>
                        <phase>initialize</phase>
                        <goals><goal>regex-property</goal></goals>
                        <configuration>
                            <name>deployRepoUrl</name>
                            <value>${project.distributionManagement.snapshotRepository.url}</value>
                            <regex>.*</regex>
                            <replacement>$0</replacement>
                            <failIfNoMatch>false</failIfNoMatch>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>filter-pom</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>resources</goal>
                        </goals>
                        <configuration>
                            <escapeString>\</escapeString>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <executions>
                    <execution>
                        <id>install-parent-pom</id>
                        <!-- Make sure the parent poms are available if just running tests -->
                        <phase>package</phase>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                        <configuration>
                            <file>${project.build.outputDirectory}/pom.xml</file>
                            <groupId>${parent.pom.groupId}</groupId>
                            <artifactId>${parent.pom.artifactId}</artifactId>
                            <version>${project.version}</version>
                            <packaging>pom</packaging>
                            <generatePom>false</generatePom>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <executions>
                    <execution>
                        <id>deploy-parent-pom</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>deploy-file</goal>
                        </goals>
                        <configuration>
                            <file>${project.build.outputDirectory}/pom.xml</file>
                            <groupId>${parent.pom.groupId}</groupId>
                            <artifactId>${parent.pom.artifactId}</artifactId>
                            <version>${project.version}</version>
                            <packaging>pom</packaging>
                            <repositoryId>${deployRepoId}</repositoryId>
                            <url>${deployRepoUrl}</url>
                            <generatePom>false</generatePom>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <!-- When the build is invoked with -DaltDeploymentRepository=id::layout::url
             (e.g. by mule-runtime-pipeline for release builds), parse the value into
             deployRepoId / deployRepoUrl so the deploy-file execution above picks
             up the release repo instead of the snapshot defaults.
             SNAPSHOT builds do not pass altDeploymentRepository, the profile stays
             inactive, and the snapshot defaults set above (build-helper) apply. -->
        <profile>
            <id>parse-alt-deployment-repository</id>
            <activation>
                <property>
                    <name>altDeploymentRepository</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>build-helper-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>extract-deploy-repo-id</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>regex-property</goal>
                                </goals>
                                <configuration>
                                    <name>deployRepoId</name>
                                    <value>${altDeploymentRepository}</value>
                                    <regex>^([^:]+)::.*$</regex>
                                    <replacement>$1</replacement>
                                    <failIfNoMatch>true</failIfNoMatch>
                                </configuration>
                            </execution>
                            <execution>
                                <id>extract-deploy-repo-url</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>regex-property</goal>
                                </goals>
                                <configuration>
                                    <name>deployRepoUrl</name>
                                    <value>${altDeploymentRepository}</value>
                                    <regex>^[^:]+::[^:]*::(.*)$</regex>
                                    <replacement>$1</replacement>
                                    <failIfNoMatch>true</failIfNoMatch>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>
