<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2014-2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->
<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>
        <artifactId>developerstudio-distribution-parent</artifactId>
        <groupId>org.wso2.developerstudio</groupId>
        <version>4.0.0-SNAPSHOT</version>
    </parent>

    <artifactId>developerstudio-assembly-ide</artifactId>
    <packaging>pom</packaging>
    <name>WSO2 Developer Studio :: Assembly-IDE</name>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>copy-sdk</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.eclipse.che.sdk</groupId>
                                    <artifactId>assembly-sdk</artifactId>
                                    <version>${com.codenvy.sdk.version}</version>
                                    <!-- SET WSO2 Version-->
                                    <type>zip</type>
                                    <overWrite>false</overWrite>
                                    <outputDirectory>${basedir}/resources/codenvy-sdk/</outputDirectory>
                                    <destFileName>${com.codenvy.sdk.version}.zip</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-extensions</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.wso2.developerstudio</groupId>
                                    <artifactId>core-extension</artifactId>
                                    <version>4.0.0-SNAPSHOT</version>
                                    <type>jar</type>
                                    <overWrite>false</overWrite>
                                    <outputDirectory>${basedir}/target/${com.codenvy.sdk.version}/ext/</outputDirectory>
                                    <destFileName>core-extension-4.0.0-SNAPSHOT.jar</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>unpack-default-sdk</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo message="unpacking codenvy sdk V${com.codenvy.sdk.version}"/>
                                <unzip dest="${basedir}/target/${com.codenvy.sdk.version}"
                                       src="${basedir}/resources/codenvy-sdk/${com.codenvy.sdk.version}.zip"/>
                                <chmod dir="${basedir}/target/${com.codenvy.sdk.version}/" includes="**/*.sh"
                                       perm="+x"/>

                                <delete file="${project.basedir}/target/${com.codenvy.sdk.version}/sdk-resources/pom.xml"/>
                                <copy file="${basedir}/resources/template/pom-template.xml"
                                      tofile="${project.basedir}/target/${com.codenvy.sdk.version}/sdk-resources/pom.xml"/>
                            </tasks>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-compiled-wars</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <delete file="${basedir}/target/${com.codenvy.sdk.version}/webapps/ws.war"/>
                                <copy file="${basedir}/target/${com.codenvy.sdk.version}/sdk-resources/temp/target/assembly-ide-${com.codenvy.sdk.version}.war"
                                      tofile="${basedir}/target/${com.codenvy.sdk.version}/webapps/ws.war"/>
                                <delete dir="${basedir}/target/${com.codenvy.sdk.version}/webapps/ws"/>
                                <unwar dest="${basedir}/target/${com.codenvy.sdk.version}/webapps/api"
                                       src="${basedir}/target/${com.codenvy.sdk.version}/webapps/api.war"/>
                                <copy todir="${basedir}/target/${com.codenvy.sdk.version}/webapps/api/WEB-INF/lib">
                                    <fileset dir="${basedir}/target/${com.codenvy.sdk.version}/ext">
                                        <include name="*.jar"/>
                                    </fileset>
                                </copy>
                                <delete file="${basedir}/target/${com.codenvy.sdk.version}/webapps/api.war"/>
                                <war basedir="${basedir}/target/${com.codenvy.sdk.version}/webapps/api"
                                     destfile="${basedir}/target/${com.codenvy.sdk.version}/webapps/api.war"/>
                                <delete dir="${basedir}/target/${com.codenvy.sdk.version}/webapps/api"/>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>${maven.exec.plugin.version}</version>
                <executions>
                    <execution>
                        <id>install-extensions</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <additionalClasspathElements>
                                <additionalClasspathElement>
                                    ${basedir}/target/${com.codenvy.sdk.version}/sdk-tools/che-plugin-sdk-tools.jar
                                </additionalClasspathElement>
                            </additionalClasspathElements>
                            <mainClass>org.eclipse.che.ide.sdk.tools.InstallExtension</mainClass>
                            <arguments>
                                <argument>--extDir=${basedir}/target/${com.codenvy.sdk.version}/ext</argument>
                                <argument>--extResourcesDir=${basedir}/target/${com.codenvy.sdk.version}/sdk-resources
                                </argument>
                            </arguments>
                        </configuration>
                    </execution>
                    <!--do the sample copying and maven.json file replacing here before re-compiling che-->
                    <execution>
                        <id>Template-clone</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>${basedir}/resources/template/che-template-clone${script.extension}</executable>
                        </configuration>
                    </execution>
                    <execution>
                        <id>compile-ide</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>mvn</executable>
                            <workingDirectory>${project.basedir}/target/${com.codenvy.sdk.version}/sdk-resources/temp/
                            </workingDirectory>
                            <arguments>
                                <argument>clean</argument>
                                <argument>install</argument>
                                <argument>-Dskip-validate-sources=true</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.google.code.sortpom</groupId>
                <artifactId>maven-sortpom-plugin</artifactId>
                <version>2.3.0</version>
                <executions>
                    <execution>
                        <id>sort-modified-pom</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>sort</goal>
                        </goals>
                        <configuration>
                            <pomFile>${basedir}/target/${com.codenvy.sdk.version}/sdk-resources/temp/pom.xml</pomFile>
                            <expandEmptyElements>false</expandEmptyElements>
                            <nrOfIndentSpace>4</nrOfIndentSpace>
                            <createBackupFile>false</createBackupFile>
                            <sortProperties>true</sortProperties>
                            <sortDependencies>scope,groupId,artifactId</sortDependencies>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>Windows</id>
            <activation>
                <os>
                    <family>Windows</family>
                </os>
            </activation>
            <properties>
                <script.extension>.bat</script.extension>
            </properties>
        </profile>
        <profile>
            <id>unix</id>
            <activation>
                <os>
                    <family>unix</family>
                </os>
            </activation>
            <properties>
                <script.extension>.sh</script.extension>
            </properties>
        </profile>
        <profile>
            <id>mac</id>
            <activation>
                <os>
                    <family>mac</family>
                </os>
            </activation>
            <properties>
                <script.extension>.sh</script.extension>
            </properties>
        </profile>
    </profiles>
</project>
