<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>
    <packaging>war</packaging>

    <artifactId>brooklyn-clocker-jsgui</artifactId>

    <name>Clocker REST JavaScript Web GUI</name>

    <description>
        JavaScript+HTML GUI for interacting with clocker, building on Brooklyn JS GUI and using the Brooklyn REST API
    </description>

    <parent>
        <groupId>io.brooklyn.clocker</groupId>
        <artifactId>brooklyn-clocker-parent</artifactId>
        <version>1.2.0</version> <!-- CLOCKER_VERSION -->
        <relativePath>../pom.xml</relativePath>
    </parent>

    <properties>
        <maven-replacer-plugin.version>1.5.2</maven-replacer-plugin.version>
        <nodejs-maven-plugin.version>1.0.3</nodejs-maven-plugin.version>
        <jasmine-maven-plugin.version>1.2.0.0</jasmine-maven-plugin.version>
        <requirejs-maven-plugin.version>2.0.0</requirejs-maven-plugin.version>
        <maven-war-plugin.version>2.4</maven-war-plugin.version>

        <project.build.webapp>
            ${project.build.directory}/${project.build.finalName}
        </project.build.webapp>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.brooklyn</groupId>
            <artifactId>brooklyn-jsgui</artifactId>
            <version>${brooklyn.version}</version>
            <type>war</type>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
     <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>${maven-war-plugin.version}</version>
        <configuration>
          <useCache>true</useCache> <!-- to prevent replaced files being overwritten -->
          <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
            </manifest>
            <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
          </archive>
          <overlays>
            <overlay>
              <groupId>org.apache.brooklyn</groupId>
              <artifactId>brooklyn-jsgui</artifactId>
<!-- if css files are overridden you need:
              <excludes>
                <exclude>assets/css/*.gz</exclude>
              </excludes>
-->
            </overlay>
          </overlays>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <executions>
          <execution>
            <id>bundle-manifest</id>
            <phase>process-classes</phase>
            <goals>
              <goal>manifest</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <supportedProjectTypes>
            <supportedProjectType>war</supportedProjectType>
          </supportedProjectTypes>
          <instructions>
            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
            <Bundle-Version>${project.version}</Bundle-Version>
            <Web-ContextPath>/</Web-ContextPath>
          </instructions>
        </configuration>
      </plugin>
     </plugins>
    </build>

    <profiles>
        <profile>
            <id>Optimize resources</id>
            <activation>
                <property>
                    <name>!skipOptimization</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <!-- Installs node.js in target/. Means we get the benefits of node's speed
                         (compared to Rhino) without having to install it manually. -->
                    <plugin>
                        <groupId>com.github.skwakman.nodejs-maven-plugin</groupId>
                        <artifactId>nodejs-maven-plugin</artifactId>
                        <version>${nodejs-maven-plugin.version}</version>
                        <executions>
                            <execution>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>extract</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <!-- target directory for node binaries -->
                            <targetDirectory>${project.basedir}/target/nodejs/</targetDirectory>
                        </configuration>
                    </plugin>

                    <!-- Including the exploded goal means sources are in place ready for the replacer plugin. -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>${maven-war-plugin.version}</version>
                        <executions>
                            <execution>
                                <id>prepare-war</id>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>exploded</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Runs the require.js optimizer with node to produce a single artifact. -->
                    <plugin>
                        <groupId>com.github.mcheely</groupId>
                        <artifactId>requirejs-maven-plugin</artifactId>
                        <version>${requirejs-maven-plugin.version}</version>
                        <executions>
                            <execution>
                                <id>optimize-js</id>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>optimize</goal>
                                </goals>
                                <configuration>
                                    <configFile>${project.basedir}/src/build/optimize-js.json</configFile>
                                </configuration>
                            </execution>
                            <execution>
                                <id>optimize-css</id>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>optimize</goal>
                                </goals>
                                <configuration>
                                    <configFile>${project.basedir}/src/build/optimize-css.json</configFile>
                                </configuration>
                            </execution>
                        </executions>
                        <configuration>
                            <nodeExecutable>${project.basedir}/target/nodejs/node</nodeExecutable>
                            <optimizerFile>${project.basedir}/src/build/requirejs-maven-plugin/r.js</optimizerFile>
                            <filterConfig>true</filterConfig>
                        </configuration>
                    </plugin>

                    <!-- Modify index.html to point to the optimized resources generated above. -->
                    <plugin>
                        <groupId>com.google.code.maven-replacer-plugin</groupId>
                        <artifactId>replacer</artifactId>
                        <version>${maven-replacer-plugin.version}</version>
                        <executions>
                            <execution>
                                <id>modify-for-optimized</id>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>replace</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <file>${project.build.webapp}/index.html</file>
                            <replacements>
                                <replacement>
                                    <token>assets/js/config.js</token>
                                    <value>assets/js/gui.all.min.js</value>
                                </replacement>
                                <replacement>
                                    <token>assets/css/styles.css</token>
                                    <value>assets/css/styles.min.css</value>
                                </replacement>
                                <replacement>
                                    <token>GIT_SHA_1</token>
                                    <value>${buildNumber}</value>
                                </replacement>
                            </replacements>
                        </configuration>
                    </plugin>

                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>Compress resources</id>
                                <phase>prepare-package</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <target>
                                        <gzip src="${project.build.webapp}/assets/css/styles.min.css"
                                              destfile="${project.build.webapp}/assets/css/styles.min.css.gz"/>
                                        <gzip src="${project.build.webapp}/assets/css/brooklyn.css"
                                              destfile="${project.build.webapp}/assets/css/brooklyn.css.gz"/>
                                        <gzip src="${project.build.webapp}/assets/js/gui.all.min.js"
                                              destfile="${project.build.webapp}/assets/js/gui.all.min.js.gz"/>
                                        <gzip src="${project.build.webapp}/assets/js/libs/require.js"
                                              destfile="${project.build.webapp}/assets/js/libs/require.js.gz"/>
                                    </target>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                </plugins>
            </build>
        </profile>
    </profiles>

</project>
