<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.alfresco</groupId>
      <artifactId>aikau-parent</artifactId>
      <version>1.1.8</version>
      <relativePath>../pom.xml</relativePath>
   </parent>

   <artifactId>aikau</artifactId>
   
   <name>Aikau</name>
   <description>Aikau is a meta-framework for Alfresco specific UI development</description>

   <properties>
      <underscore>_</underscore>
   </properties>

    <!-- Run profile that invokes the tomcat plugin -->
    <profiles>
        <profile>
            <id>run</id>
            <build>
                <plugins>
                    <!-- Cargo's embedded Tomcat deploys the exploded testApp folder directly and
                         does NOT merge in the project's Maven runtime classpath (unlike the Jetty
                         plugin's classesDirectory behaviour). So any runtime-scoped dependencies
                         needed by the test webapp (e.g. urlrewritefilter) must be physically
                         copied into its WEB-INF/lib folder before Cargo starts. This execution is
                         scoped to this profile ONLY so plain "jetty:run" invocations never see the
                         extra duplicate jars on their classpath (which caused duplicate-class scan
                         warnings/failures such as with closure-compiler). -->
                    <plugin>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-webapp-runtime-libs</id>
                                <phase>test-compile</phase>
                                <goals>
                                    <goal>copy-dependencies</goal>
                                </goals>
                                <configuration>
                                    <includeScope>runtime</includeScope>
                                    <outputDirectory>${project.build.testOutputDirectory}/testApp/WEB-INF/lib</outputDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.cargo</groupId>
                        <artifactId>cargo-maven3-plugin</artifactId>
                        <version>1.10.20</version>
                        <executions>
                            <execution>
                                <id>run-embedded-tomcat</id>
                                <!-- Bind to the integration-test phase -->
                                <phase>integration-test</phase>
                                <goals>
                                    <!-- Run will run the project as a dynamic webapp -->
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <container>
                                <!-- Tomcat 10 is the first Jakarta EE (jakarta.servlet) Tomcat release -->
                                <containerId>tomcat10x</containerId>
                                <type>embedded</type>
                            </container>
                            <configuration>
                                <properties>
									<!-- Tomcat's port  -->
                                    <cargo.servlet.port>8089</cargo.servlet.port>
                                </properties>
                            </configuration>
                            <deployables>
                                <deployable>
                                    <location>${basedir}/target/test-classes/testApp</location>
                                    <type>war</type>
                                    <properties>
                                        <context>/aikau</context>
                                    </properties>
                                </deployable>
                            </deployables>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
            <dependencies>
               <dependency>
                   <groupId>jakarta.servlet</groupId>
                   <artifactId>jakarta.servlet-api</artifactId>
                   <version>6.0.0</version>
                  <scope>provided</scope>
               </dependency>
            </dependencies>
        </profile>
        <profile>
            <id>unitTestsBStack</id>
            <build>
                <plugins>
                    <!-- See comment in the "run" profile above: this copy is required for Cargo's
                         embedded Tomcat webapp to find its runtime dependencies, and is scoped to
                         this profile only so Jetty runs never see duplicate jars. -->
                    <plugin>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-webapp-runtime-libs</id>
                                <phase>test-compile</phase>
                                <goals>
                                    <goal>copy-dependencies</goal>
                                </goals>
                                <configuration>
                                    <includeScope>runtime</includeScope>
                                    <outputDirectory>${project.build.testOutputDirectory}/testApp/WEB-INF/lib</outputDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.cargo</groupId>
                        <artifactId>cargo-maven3-plugin</artifactId>
                        <version>1.10.20</version>
                        <executions>
                            <execution>
                                <id>run-embedded-tomcat</id>
                                <!-- Bind to the pre-integration-test phase -->
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <!-- Start Tomcat in the background so the tests can run against it -->
                                    <goal>start</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>shutdown-embedded-tomcat</id>
                                <!-- Bind to the post-integration-test phase -->
                                <phase>post-integration-test</phase>
                                <goals>
                                    <!-- Safely stop tomcat when integration-test phase is done -->
                                    <goal>stop</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <container>
                                <containerId>tomcat10x</containerId>
                                <type>embedded</type>
                            </container>
                            <configuration>
                                <properties>
									<!-- tomact's port-->
                                    <cargo.servlet.port>8089</cargo.servlet.port>
                                </properties>
                            </configuration>
                            <deployables>
                                <deployable>
                                    <location>${basedir}/target/test-classes/testApp</location>
                                    <type>war</type>
                                    <properties>
                                        <context>/aikau</context>
                                    </properties>
                                </deployable>
                            </deployables>
                        </configuration>
                    </plugin>
                    <plugin>
                      <artifactId>maven-antrun-plugin</artifactId>
                      <executions>
                        <execution>
                          <id>run-unit-tests</id>
                          <phase>integration-test</phase>
                          <goals>
                            <goal>run</goal>
                          </goals>
                          <configuration>
                            <target>
                              <echo>Running npm install</echo>
                              <exec executable="npm" dir="${project.basedir}">
                                <arg line="install" />
                              </exec>
                              <echo>Running grunt test_bamboo</echo>
                              <exec executable="grunt" dir="${project.basedir}">
                                <arg line="test_bamboo" />
                              </exec>
                            </target>
                          </configuration>
                        </execution>
                      </executions>
                    </plugin>
                </plugins>
            </build>
            <dependencies>
               <dependency>
                   <groupId>jakarta.servlet</groupId>
                   <artifactId>jakarta.servlet-api</artifactId>
                   <version>6.0.0</version>
                  <scope>provided</scope>
               </dependency>
            </dependencies>
        </profile>
    </profiles>


   <build>

      <!-- Everything gets place into the META-INF folder of the JAR because Surf (the expected platform for Aikau)
           will be able to access resources from that location via the /res/ path -->
      <resources>
         <!-- By default we're going to place the Aikau source files into a versioned path... -->
         <resource>
            <targetPath>META-INF/js/aikau/${project.version}/alfresco</targetPath>
            <filtering>false</filtering>
            <directory>${basedir}/src/main/resources/alfresco</directory>
         </resource>

         <resource>
            <targetPath>META-INF/js/aikau/${project.version}/modules</targetPath>
            <filtering>false</filtering>
            <directory>${basedir}/src/main/resources/aikau</directory>
         </resource>

         <!-- Put the release notes in the root of the JAR file -->
         <resource>
            <targetPath>.</targetPath>
            <filtering>true</filtering>
            <includes>
               <include>ReleaseNotes.md</include>
            </includes>
            <directory>${basedir}</directory>
         </resource>

         <resource>
            <!-- Path has to be aligned with version because Surf caches by XML path -->
            <targetPath>./alfresco/site-data/extensions/${project.version}</targetPath>
            <filtering>true</filtering>
            <directory>${basedir}/src/main/resources/extension-module</directory>
         </resource>

         <resource>
            <!-- Spring Bean overrides -->
            <targetPath>./alfresco/web-extension/</targetPath>
            <filtering>true</filtering>
            <directory>${basedir}/src/main/resources/spring-context-config</directory>
         </resource>

         <resource>
            <!-- WebScript library files go straight into a package -->
            <targetPath>./alfresco/site-webscripts/org/alfresco/aikau/${project.version}/libs</targetPath>
            <filtering>false</filtering>
            <directory>${basedir}/src/main/resources/webscript-libs</directory>
         </resource>

         <resource>
            <!-- Extension WebScripts can't use a version with dots, so an Ant plugin renames the target folder later -->
             <targetPath>./alfresco/site-webscripts/customizations/${project.version}</targetPath>
            <filtering>true</filtering>
            <directory>${basedir}/src/main/resources/extension-webscripts</directory>
         </resource>

         <resource>
            <!-- Extension WebScripts can't use a version with dots, so an Ant plugin renames the target folder later -->
            <targetPath>./alfresco/site-webscripts/aikau-patches/${project.version}</targetPath>
            <filtering>true</filtering>
            <directory>${basedir}/src/main/resources/patches</directory>
         </resource>

         <resource>
            <!-- WebScripts go into their own package -->
            <targetPath>./alfresco/site-webscripts/org/alfresco/aikau/${project.version}/webscripts</targetPath>
            <filtering>true</filtering>
            <directory>${basedir}/src/main/resources/webscripts</directory>
         </resource>

         <!-- ...but everything else goes into a standard path -->
         <resource>
            <targetPath>META-INF/js/lib</targetPath>
            <filtering>false</filtering>
            <directory>${basedir}/src/main/resources/lib</directory>
         </resource>

          <!-- The tomcat plugin can't have a virtual classpath for this proejct, and it wont accept
               additional classpaths, so we have to copy these into the test classpath
          -->
         <resource>
            <directory>${project.build.outputDirectory}/alfresco</directory>
            <targetPath>${project.build.testOutputDirectory}/testApp/WEB-INF/classes/alfresco</targetPath>
         </resource>
      </resources>

      <testResources>
         <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
            <includes>
               <include>testApp/WEB-INF/surf.xml</include>
               <include>testApp/WEB-INF/config/web-application-config.xml</include>
            </includes>
         </testResource>
         <testResource>
            <directory>src/test/resources</directory>
            <filtering>false</filtering>
            <excludes>
               <exclude>testApp/WEB-INF/surf.xml</exclude>
               <exclude>testApp/WEB-INF/config/web-application-config.xml</exclude>
            </excludes>
         </testResource>
      </testResources>

      <plugins>

         <!-- See AKU-845 - This plugin has been brought in to create a property that is the project
              version with all "." characters replaced with "-" characters. The reason that this is 
              required is that we want to defined version specific WebScript URLs, but for some reason
              (most likely in Surf) the RequestDispatcher fails to map URLs containing more than
              one "." character - this means that the Aikau version cannot be used without modification -->
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
               <execution>
                  <id>regex-property</id>
                  <goals>
                     <goal>regex-property</goal>
                  </goals>
                  <configuration>
                     <name>webscript.version</name>
                     <value>${project.version}</value>
                     <regex>[\.\-]</regex>
                     <replacement>_</replacement>
                     <failIfNoMatch>false</failIfNoMatch>
                  </configuration>
             </execution>
           </executions>
         </plugin>

         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>templating-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
               <execution>
                  <id>filter-src</id>
                  <goals>
                     <goal>filter-sources</goal>
                  </goals>
                  <configuration>
                     <!-- 
                       Note the two following parameters are the default one. 
                       These are specified here just as a reminder. 
                       But as the Maven philosophy is strongly about conventions, 
                       it's better to just not specify them.
                     -->
                     <sourceDirectory>${basedir}/src/main/java-templates</sourceDirectory>
                     <outputDirectory>${project.build.directory}/generated-sources/java-templates</outputDirectory>
                  </configuration>
               </execution>
           </executions>
         </plugin>

         <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
               <execution>
                  <id>duplicate-english-messages</id>
                  <phase>generate-resources</phase>
                  <goals>
                     <goal>run</goal>
                  </goals>
                  <configuration>
                     <target>
                        <copy todir="${project.build.outputDirectory}/META-INF/js/aikau/${project.version}">
                           <fileset dir="${basedir}/src/main/resources" includes="alfresco/**/*.properties" />
                           <mapper from="^([^_]*).properties$" to="\1_en.properties" type="regexp" />
                        </copy>
                     </target>
                  </configuration>
               </execution>
               <execution>
                  <id>copy-webscript-beans</id>
                  <!-- Bound to test-compile (rather than process-test-classes) so this copy
                       reliably runs when invoking "mvn ... test-compile jetty:run" directly.
                       Directly-invoked goals (like jetty:run/cargo:run) don't trigger later
                       lifecycle phases such as process-test-classes on their own. -->
                  <phase>test-compile</phase>
                  <goals>
                     <goal>run</goal>
                  </goals>
                  <configuration>
                     <target>
                        <mkdir dir="${project.build.testOutputDirectory}/testApp/WEB-INF/classes" />
                        <copy todir="${project.build.testOutputDirectory}/testApp/WEB-INF/classes">
                           <fileset dir="${project.build.testOutputDirectory}" includes="**/*.class" />
                        </copy>
                      </target>
                  </configuration>
                </execution>
                <execution>
                  <id>rename-extensions-folder</id>
                  <phase>prepare-package</phase>
                  <goals>
                     <goal>run</goal>
                  </goals>
                  <configuration>
                     <target>
                        <echo>Renaming folders</echo>
                        <move file="${project.build.outputDirectory}/alfresco/site-webscripts/customizations/${project.version}" tofile="${project.build.outputDirectory}/alfresco/site-webscripts/customizations/${webscript.version}" />
                        <move file="${project.build.outputDirectory}/alfresco/site-webscripts/aikau-patches/${project.version}" tofile="${project.build.outputDirectory}/alfresco/site-webscripts/aikau-patches/${webscript.version}" />
                      </target>
                  </configuration>
                </execution>
                <!-- Cargo's embedded Tomcat only deploys a single location (target/test-classes/testApp),
                     unlike the Jetty plugin which is explicitly configured with TWO resourceBases
                     (testApp AND target/classes/META-INF). All the built Aikau/Dojo/jQuery JS resources
                     live under target/classes/META-INF, so without this copy Cargo's deployed webapp
                     never has them and Surf's DependencyAggregator logs "Could not find compressed file"
                     for every JS resource. Bound to "package" so it runs after the dojo/jquery
                     unpack-dependencies executions (prepare-package) but before Cargo starts
                     (integration-test). -->
                <execution>
                  <id>merge-classes-into-testapp</id>
                  <phase>package</phase>
                  <goals>
                     <goal>run</goal>
                  </goals>
                  <configuration>
                     <target>
                        <echo>Merging target/classes/META-INF into testApp webapp root for embedded Tomcat</echo>
                        <copy todir="${project.build.testOutputDirectory}/testApp" overwrite="true">
                           <fileset dir="${project.build.outputDirectory}/META-INF" />
                        </copy>
                      </target>
                  </configuration>
                </execution>
                <!-- <execution>
                  <id>rename-java-folder</id>
                  <phase>prepare-package</phase>
                  <goals>
                     <goal>run</goal>
                  </goals>
                  <configuration>
                     <target>
                        <echo>Renaming folder</echo>
                        <move file="${project.build.outputDirectory}/org/alfresco/aikau" tofile="${project.build.outputDirectory}/org/alfresco/aikau/${project.version}" />
                      </target>
                  </configuration>
                </execution> -->
            </executions>
        </plugin>

        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
               <execution>
                  <id>unpack-dependencies</id>
                  <phase>prepare-package</phase>
                  <goals>
                     <goal>unpack</goal>
                  </goals>
                  <configuration>
                     <artifactItems>
                        <artifactItem>
                           <groupId>org.dojotoolkit</groupId>
                           <artifactId>dojo</artifactId>
                           <version>${dependency.dojo.version}</version>
                           <type>zip</type>
                           <classifier>distribution</classifier>
                           <overWrite>true</overWrite>
                           <outputDirectory>${basedir}/target/classes/META-INF/js/lib</outputDirectory>
                        </artifactItem>
                     </artifactItems>
                  </configuration>
                </execution>
            </executions>
         </plugin>

         <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
               <execution>
                  <id>build-tests</id>
                  <goals>
                     <goal>test-jar</goal>
                  </goals>
                  <configuration>
                     <testClassesDirectory>${project.build.testOutputDirectory}/test-selectors</testClassesDirectory>
                     <includes>
                        <include>**/*.properties</include>
                     </includes>
                  </configuration>
               </execution>
            </executions>
         </plugin>

         <plugin>
            <groupId>org.eclipse.jetty.ee10</groupId>
            <artifactId>jetty-ee10-maven-plugin</artifactId>
            <version>12.0.37</version>
            <configuration>

               <!-- Aikau is packaged as a JAR (not a WAR). Jetty 12 only runs WAR modules by
                    default, so we must explicitly allow the jar packaging for jetty:run to work. -->
               <supportedPackagings>
                  <supportedPackaging>jar</supportedPackaging>
               </supportedPackagings>

               <!-- Override the system properties to set a port that *should* be unused.
                    Jetty 10+ changed systemProperties to a simple key/value map. -->
               <systemProperties>
                  <jetty.port>8089</jetty.port>
               </systemProperties>

               <!--  The following 3 settings allow us to execute a mvn jetty:stop command to shutdown the test application -->
               <stopPort>9966</stopPort>
               <stopKey>foo</stopKey>
               <stopWait>20</stopWait>

                <!-- Configure the application, this will essentially be Share but overlaid with the test resources -->
               <webApp>
                  <descriptor>target/test-classes/testApp/WEB-INF/web.xml</descriptor>
                  <contextPath>/aikau</contextPath>
                  <resourceBases>
                     <resourceBase>target/test-classes/testApp</resourceBase>
                     <resourceBase>target/classes/META-INF</resourceBase>
                     <!-- <resourceBase>${project.build.directory}/${project.build.finalName}</resourceBase> -->
                  </resourceBases>
                  <allowDuplicateFragmentNames>true</allowDuplicateFragmentNames> 
               </webApp>
               <webAppSourceDirectory>target/test-classes/testApp</webAppSourceDirectory>
               <classesDirectory>target/classes</classesDirectory>
               <testClassesDirectory>target/test-classes/testApp/WEB-INF/classes</testClassesDirectory>
            </configuration>
         </plugin>

         <!-- Generate jsdoc using a Maven plugin (Command: mvn jsdoc3:jsdoc3) -->
         <plugin>
            <groupId>com.phasebash.jsdoc</groupId>
            <artifactId>jsdoc3-maven-plugin</artifactId>
            <version>1.1.0</version>
            <configuration>
               <recursive>true</recursive>
               <directoryRoots>
                  <directoryRoot>${basedir}/src/main/resources/alfresco/</directoryRoot>
               </directoryRoots>
               <templateDirectory>${basedir}/resources/jsdoc/templates/alfresco</templateDirectory>
               <configFile>${basedir}/conf.json</configFile>
            </configuration>
         </plugin>
         
      </plugins>
   </build>

</project>
