<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.mulesoft.mule.distributions</groupId>
        <artifactId>mule-ee-embedded</artifactId>
        <version>4.7.4</version>
    </parent>
    <artifactId>mule-ee-distribution-embedded</artifactId>
    <!-- Packaging 'pom' ensures that maven does not try to bind a plugin to the 
         'package' phase. We bind the assembly plugin here, that's enough. -->
    <packaging>pom</packaging>
    <name>Embedded (Single jar file)</name>
    <description>Assembles the Mule core, modules, and transports into a single library (jar). This is useful for
        embedding Mule into an existing project.
    </description>

    <properties>
        <javaModuleName>com.mulesoft.mule.distribution.embedded</javaModuleName>
        <gmavenPluginVersion>2.1.0</gmavenPluginVersion>
        <embeddedDir>${project.build.directory}/embedded</embeddedDir>
        <formatterConfigPath>../../formatter.xml</formatterConfigPath>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <!--
                        Step 1: Unpack the CE embedded distribution, we're adding to it now
                    -->
                    <execution>
                        <id>unpack-ce-distribution</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${embeddedDir}</outputDirectory>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.mule.distributions</groupId>
                                    <artifactId>mule-embedded-distribution</artifactId>
                                    <version>${project.version}</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                            <overWriteSnapshots>true</overWriteSnapshots>
                            <overWriteReleases>true</overWriteReleases>
                        </configuration>
                    </execution>
                    <!--
                        Step 3: Unpack all ee modules/transports on top of the CE embedded distro
                    -->
                    <execution>
                        <id>unpack-ee-modules</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${embeddedDir}</outputDirectory>
                            <includeGroupIds>com.mulesoft</includeGroupIds>
                            <excludeTypes>pom</excludeTypes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>${gmavenPluginVersion}</version>
                <executions>
                    <!--
                        Step 2: Invoke a groovy script to update the mule.schemas
                        files from CE's embedded distribution
                    -->
                    <execution>
                        <id>generate-spring-handlers</id>
                        <phase>package</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <scripts>
                                <script>file:///${project.basedir}/BuildAggregator.groovy</script>
                            </scripts>
                        </configuration>
                    </execution>
                    <!--
                        Step 4: Invoke a groovy script to remove the mule.schemas and spring.schemas
                        files from EE's embedded distribution, otherwise they cannot be overwritten
                        on the assembly goal (maven 3 issue)
                    -->
                    <execution>
                        <id>cleanup-spring-handlers</id>
                        <phase>package</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <scripts>
                                <script>file:///${project.basedir}/DeleteFiles.groovy</script>
                            </scripts>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.groovy</groupId>
                        <artifactId>groovy</artifactId>
                        <version>${groovyVersionBuild}</version>
                        <scope>runtime</scope>
                    </dependency>
                    <dependency>
                        <groupId>org.apache.groovy</groupId>
                        <artifactId>groovy-ant</artifactId>
                        <version>${groovyVersionBuild}</version>
                        <scope>runtime</scope>
                    </dependency>
                </dependencies>
            </plugin>

            <!--
                Step 5: pack up the whole bundle in target/embedded plus the augmented
                mule.schemas files
            -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <!--
                                'single' ensures there's only a single run bound to m2's lifecycle.
                                The default 'assembly' goal would run every plugin in this pom twice.
                            -->
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>mule-enterprise-embedded-${project.version}</finalName>
                            <descriptors>
                                <descriptor>assembly.xml</descriptor>
                            </descriptors>
                            <appendAssemblyId>false</appendAssemblyId>
                            <archive>
                                <manifestEntries>
                                    <More-Info>For more information go to ${productUrl}</More-Info>
                                    <Support>Get commercial support: ${project.organization.url}/support</Support>
                                    <Description>Mule Runtime and Integration Platform</Description>
                                    <License>MSA v1.0 http://www.mulesoft.com/</License>
                                    <Vendor-Url>${project.organization.url}</Vendor-Url>
                                    <Product-Url>${productUrl}</Product-Url>
                                    <Build-Date>${timestamp}</Build-Date>
                                    <Supported-Jdks>[1.8.0_20,1.9)</Supported-Jdks>
                                    <Recommended-Jdks>[1.8.0_20,1.9)</Recommended-Jdks>
                                </manifestEntries>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <configuration>
                    <!-- Do not publish (empty) source jars -->
                    <attach>false</attach>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>auto-clean</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <!-- Enterprise artifacts -->
        <dependency>
            <groupId>com.mulesoft.mule.runtime</groupId>
            <artifactId>mule-core-ee</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.mulesoft.mule.runtime.modules</groupId>
            <artifactId>mule-modules-all</artifactId>
            <version>${project.version}</version>
            <type>pom</type>
        </dependency>

        <!-- TODO MULE-9655 Determine how services and extensions work in embedded mode -->
        <dependency>
            <groupId>com.mulesoft.mule.distributions</groupId>
            <artifactId>mule-services-all</artifactId>
            <version>${project.version}</version>
            <type>pom</type>
        </dependency>
    </dependencies>
</project>
