<?xml version="1.0" encoding="UTF-8"?>
<!--
  Copyright (C) 2006-2018 Talend Inc. - www.talend.com
   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/xsd/maven-4.0.0.xsd">
  <parent>
    <artifactId>component-runtime</artifactId>
    <groupId>org.talend.sdk.component</groupId>
    <version>0.0.1</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <artifactId>component-server</artifactId>
  <name>Component Runtime :: Server</name>
  <description>The Web server of the framework. Mainly provides UI interaction hooks.</description>

  <properties>
    <talend.icon.output>${project.build.outputDirectory}</talend.icon.output>
    <talend.build.name>${talend.build.name.base}.server</talend.build.name>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.talend.sdk.component</groupId>
      <artifactId>component-runtime-manager</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency> <!-- extends runtime manager with design time data -->
      <groupId>org.talend.sdk.component</groupId>
      <artifactId>component-runtime-design-extension</artifactId>
      <version>${project.version}</version>
    </dependency>

    <dependency>
      <groupId>org.talend.ui</groupId>
      <artifactId>talend-icon</artifactId>
      <version>${talend-ui.version}</version>
      <scope>provided</scope>
      <optional>true</optional>
    </dependency>

    <dependency>
      <groupId>io.zipkin.brave</groupId>
      <artifactId>brave-instrumentation-jaxrs2</artifactId>
      <version>4.13.1</version>
    </dependency>
    <dependency>
      <groupId>io.zipkin.reporter2</groupId>
      <artifactId>zipkin-sender-kafka11</artifactId>
      <version>2.2.2</version>
    </dependency>

    <dependency>
      <groupId>commons-cli</groupId>
      <artifactId>commons-cli</artifactId>
      <version>${commons-cli.version}</version>
    </dependency>

    <dependency> <!-- override until we upgrade meecrowave -->
      <groupId>org.apache.johnzon</groupId>
      <artifactId>johnzon-jsonb</artifactId>
      <version>${johnzon.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.meecrowave</groupId>
      <artifactId>meecrowave-core</artifactId>
      <version>${meecrowave.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.deltaspike.core</groupId>
      <artifactId>deltaspike-core-impl</artifactId>
      <version>${deltaspike.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-slf4j-impl</artifactId>
      <version>${log4j2.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-web</artifactId>
      <version>${log4j2.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>${log4j2.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>${log4j2.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-jul</artifactId>
      <version>${log4j2.version}</version>
    </dependency>

    <dependency>
      <groupId>org.apache.tomcat</groupId>
      <artifactId>tomcat-websocket</artifactId>
      <version>${tomcat.version}</version>
    </dependency>

    <dependency>
      <groupId>org.webjars</groupId>
      <artifactId>swagger-ui</artifactId>
      <version>3.2.2</version>
    </dependency>

    <dependency>
      <groupId>org.apache.meecrowave</groupId>
      <artifactId>meecrowave-junit</artifactId>
      <version>${meecrowave.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.tomee</groupId>
      <artifactId>ziplock</artifactId>
      <version>${ziplock.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.talend.sdk.component</groupId>
      <artifactId>component-runtime-junit-base</artifactId>
      <version>${project.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>pl.project13.maven</groupId>
        <artifactId>git-commit-id-plugin</artifactId>
        <version>${git-commit-id-plugin.version}</version>
        <executions>
          <execution>
            <id>get-the-git-infos</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>revision</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <generateGitPropertiesFile>true</generateGitPropertiesFile>
          <generateGitPropertiesFilename>${project.build.outputDirectory}/TALEND-INF/git.properties</generateGitPropertiesFilename>
        </configuration>
      </plugin>
      <plugin>
        <groupId>com.sebastian-daschner</groupId>
        <artifactId>jaxrs-analyzer-maven-plugin</artifactId>
        <version>0.16</version>
        <executions>
          <execution>
            <id>analyzer-adoc</id>
            <goals>
              <goal>analyze-jaxrs</goal>
            </goals>
            <configuration>
              <inlinePrettify>true</inlinePrettify>
              <backend>asciidoc</backend>
            </configuration>
          </execution>
          <execution>
            <id>analyzer-swagger</id>
            <goals>
              <goal>analyze-jaxrs</goal>
            </goals>
            <configuration>
              <backend>swagger</backend>
              <resourcesDir>classes/META-INF/resources/documentation</resourcesDir>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>${gmavenplus.version}</version>
        <executions>
          <execution>
            <id>setup-deps-and-copy-icons</id>
            <phase>initialize</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <allowSystemExits>true</allowSystemExits>
              <scripts>
                <script>
                  project.properties.setProperty('osgiVersion', project.version.replace('-', '.'))
                  project.properties.setProperty('osgiExportedVersion', project.version.replace('-SNAPSHOT', ''))
                  project.properties.setProperty('osgiLicence', project.licenses[0].url)
                  project.properties.setProperty('osgiTimestamp', Long.toString(System.currentTimeMillis()))
                </script>
                <script>${project.parent.basedir}/build/CreatePNGIcons.groovy</script>
              </scripts>
            </configuration>
          </execution>
          <execution>
            <id>fix-swagger.json</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <allowSystemExits>true</allowSystemExits>
              <scripts>
                <script>
                  def swaggerJson = new File(project.build.outputDirectory, 'META-INF/resources/documentation/swagger.json')
                  def content = swaggerJson.text
                  swaggerJson.text = content.replaceAll("\"basePath\": \"[^\"]+\",", '"basePath": "/api/v1",')
                </script>
              </scripts>
            </configuration>
          </execution>
          <execution>
            <id>enrich-adoc-with-descriptions</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <allowSystemExits>true</allowSystemExits>
              <scripts>
                <script><![CDATA[
                  import javax.ws.rs.GET
                  import javax.ws.rs.ApplicationPath
                  import javax.ws.rs.Path
                  import javax.ws.rs.POST
                  import org.talend.sdk.component.api.meta.Documentation

                  def doc = new File(project.build.directory, 'jaxrs-analyzer/rest-resources.adoc')
                  if (!doc.exists()) {
                    log.warn("${doc.absolutePath} doesn't exist")
                    return
                  }

                  def content = doc.text
                  def base = org.talend.sdk.component.server.front.TalendComponentApplication.class.getAnnotation(ApplicationPath.class).value()
                  // for all endpoints replace the header appending the description
                  [ // we could scan the resources but we'll never get a bunch so this is easier for now
                    org.talend.sdk.component.server.front.ActionResource.class,
                    org.talend.sdk.component.server.front.ComponentResource.class,
                    org.talend.sdk.component.server.front.ConfigurationTypeResource.class,
                    org.talend.sdk.component.server.front.DocumentationResource.class,
                    org.talend.sdk.component.server.front.ExecutionResource.class
                  ].each { clazz ->
                    clazz.methods.findAll { mtd ->
                      mtd.isAnnotationPresent(Documentation.class) && mtd.isAnnotationPresent(Path.class)
                    }.each { mtd ->
                      def verb;
                      if (mtd.isAnnotationPresent(POST.class)) {
                        verb = 'POST'
                      } else if (mtd.isAnnotationPresent(GET.class)) {
                        verb = 'GET'
                      } else {
                        throw new IllegalArgumentException("Unkown verb for ${mtd.toGenericString()}")
                      }
                      def path = mtd.declaringClass.getAnnotation(Path.class).value() + '/' + mtd.getAnnotation(Path.class).value();
                      def docContent = mtd.getAnnotation(Documentation.class).value()
                      def token = "== `${verb} ${base}/${path}`"
                      content = content.replace(token, "${token}\n\n${docContent}\n")
                    }
                  }
                  doc.text = content

                  // copy for the doc
                  def docCopy = new File(project.basedir, '../documentation/src/main/antora/modules/ROOT/pages/_partials/generated_rest-resources.adoc')
                  if (!docCopy.exists() || !docCopy.text.trim() != content.trim()) {
                    docCopy.text = content
                  }
                ]]></script>
              </scripts>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>${groovy.version}</version>
          </dependency>
          <dependency>
            <groupId>org.apache.tomee</groupId>
            <artifactId>ziplock</artifactId>
            <version>${ziplock.version}</version>
          </dependency>
          <dependency>
            <groupId>org.apache.xmlgraphics</groupId>
            <artifactId>batik-codec</artifactId>
            <version>${batik.version}</version>
            <exclusions>
              <exclusion>
                <groupId>xml-apis</groupId>
                <artifactId>xml-apis</artifactId>
              </exclusion>
              <exclusion>
                <groupId>xalan</groupId>
                <artifactId>xalan</artifactId>
              </exclusion>
              <exclusion>
                <groupId>xerces</groupId>
                <artifactId>xercesImpl</artifactId>
              </exclusion>
            </exclusions>
          </dependency>
        </dependencies>
      </plugin>
      <!--
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <id>bundle</id>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <shadedClassifierName>fat</shadedClassifierName>
              <shadedArtifactAttached>true</shadedArtifactAttached>
              <dependencyReducedPomLocation>${project.build.directory}/reduced-pom-bundle.xml
              </dependencyReducedPomLocation>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>org.apache.meecrowave.runner.Cli</mainClass>
                </transformer>
                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                  <resource>META-INF/cxf/bus-extensions.txt</resource>
                </transformer>
                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                  <resource>log4j2.component.properties</resource>
                </transformer>
                <transformer implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer" />
                <transformer implementation="org.apache.openwebbeans.maven.shade.OpenWebBeansPropertiesTransformer" />
                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
              </transformers>
              <filters>
                <filter>
                  <artifact>*:*</artifact>
                  <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                    <exclude>META-INF/LICENSE.txt</exclude>
                    <exclude>META-INF/LICENSE</exclude>
                    <exclude>META-INF/NOTICE.txt</exclude>
                    <exclude>META-INF/NOTICE</exclude>
                  </excludes>
                </filter>
              </filters>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.apache.openwebbeans</groupId>
            <artifactId>openwebbeans-maven</artifactId>
            <version>1.7.0</version>
          </dependency>
          <dependency>
            <groupId>com.github.edwgiz</groupId>
            <artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
            <version>2.1</version>
          </dependency>
        </dependencies>
      </plugin>
      -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>${dependency-plugin.version}</version>
        <executions>
          <execution>
            <id>create-TALEND-INF/dependencies.txt</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>list</goal>
            </goals>
            <configuration>
              <outputFile>${project.build.outputDirectory}/TALEND-INF/dependencies.txt</outputFile>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
            <configuration>
              <classifier>model</classifier>
              <includes>
                <include>org/talend/sdk/component/server/front/model/**/*.class</include>
              </includes>
              <archive>
                <manifestEntries>
                  <Build-Jdk>${java.version}</Build-Jdk>
                  <Built-By>${user.name}</Built-By>
                  <Bundle-ManifestVersion>2</Bundle-ManifestVersion>
                  <Bundle-Name>${project.name}</Bundle-Name>
                  <Bundle-SymbolicName>${project.groupId}.server.model</Bundle-SymbolicName>
                  <Created-By>Talend Component Kit Build</Created-By>
                  <Require-Capability>osgi.ee;filter:="(&amp;(osgi.ee=JavaSE)(version=1.8))"</Require-Capability>
                  <Export-Package>org.talend.sdk.component.server.front.model;version="${osgiExportedVersion}",org.talend.sdk.component.server.front.model.error;version="${osgiExportedVersion}",org.talend.sdk.component.server.front.model.execution;version="${osgiExportedVersion}"</Export-Package>
                  <Bnd-LastModified>${osgiTimestamp}</Bnd-LastModified>
                  <Bundle-License>${osgiLicence}</Bundle-License>
                  <Bundle-Version>${osgiVersion}</Bundle-Version>
                </manifestEntries>
              </archive>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin> <!-- for dev for now -->
        <groupId>org.apache.meecrowave</groupId>
        <artifactId>meecrowave-maven-plugin</artifactId>
        <version>${meecrowave.version}</version>
        <executions>
          <execution>
            <id>bundle</id>
            <phase>package</phase>
            <goals>
              <goal>bundle</goal>
            </goals>
            <configuration>
              <attach>true</attach>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <cdiConversation>false</cdiConversation>
          <webapp>${project.basedir}/src/dev/webapp</webapp>
          <properties>
            <talend.component.server.component.coordinates>${talend.component.server.component.coordinates.samples}</talend.component.server.component.coordinates>
          </properties>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <id>travis</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
              <systemPropertyVariables>
                <java.util.logging.manager>org.apache.logging.log4j.jul.LogManager</java.util.logging.manager>
                <component.server.test.logging.skip>true</component.server.test.logging.skip>
              </systemPropertyVariables>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
