/*
    Copyright (C) 2000 - 2026 Silverpeas

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    As a special exception to the terms and conditions of version 3.0 of
    the GPL, you may redistribute this Program in connection with Free/Libre
    Open Source Software ("FLOSS") applications as described in Silverpeas's
    FLOSS exception.  You should have received a copy of the text describing
    the FLOSS exception, and it is also available here:
    "https://www.silverpeas.org/legal/floss_exception.html"

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
import org.apache.tools.ant.filters.ReplaceTokens

buildscript {
  repositories {
    maven {
      url 'https://nexus3.silverpeas.org/repository/silverpeas'
    }
    mavenLocal()
  }
}

plugins {
  id 'java-gradle-plugin'
  id 'org.ajoberstar.grgit' version '5.3.3' apply true
  id 'idea'
  id 'maven-publish'
  id 'net.linguica.maven-settings' version '0.5'
  id 'groovy'
}

description = 'The Gradle plugin to set up Silverpeas. It is used both to install or to upgrade Silverpeas.'
group = 'org.silverpeas'
version = '6.5-build260805'

gradlePlugin {
  plugins {
    silversetup {
      id = 'silversetup'
      implementationClass = 'org.silverpeas.setup.SilverpeasSetupPlugin'
    }
  }
}

/* cache management
configurations {
  all {
    resolutionStrategy {
      cacheDynamicVersionsFor 0, 'seconds'
      cacheChangingModulesFor 0, 'seconds'
    }
  }
}*/

project.configure(project) {
  if (project.version.endsWith('-SNAPSHOT')) {
    project.extensions.snapshot = true
  } else {
    project.extensions.snapshot = false
  }
}

repositories {
  maven {
    name 'silverpeas'
    url 'https://nexus3.silverpeas.org/repository/silverpeas'
  }
  maven {
    name 'gradle-plugins'
    url 'https://plugins.gradle.org/m2/'
  }
  mavenLocal()
}

java {
  // use a Java 17 toolchain so that the compilation and the tests execution both run on a
  // JDK 17, regardless of the JVM launching Gradle. This makes the build reproducible even when
  // Gradle itself is started with another Java version (for example Java 11).
  toolchain {
    languageVersion = JavaLanguageVersion.of(17)
  }
  withSourcesJar()
}

publishing {
  publications {
    maven(MavenPublication) {
      from components.java
    }
  }

  repositories {
    maven {
      if (project.snapshot) {
        name 'silverpeas-snapshots'
        url 'https://nexus3.silverpeas.org/repository/snapshots/'
      } else {
        name 'silverpeas'
        url 'https://nexus3.silverpeas.org/repository/builds/'
      }
    }
  }
}


dependencies {
  api gradleApi()
  api 'org.apache.commons:commons-lang3:3.20.0'
  api 'org.apache.commons:commons-text:1.15.0'
  api 'commons-io:commons-io:2.22.0'
  api 'org.apache.commons:commons-dbcp2:2.14.0'
  api 'commons-codec:commons-codec:1.22.0'
  api 'com.google.guava:guava:33.0.0-jre'
  api 'javax.jcr:jcr:2.0'
  api 'org.apache.jackrabbit:oak-jcr:2.2.0'
  api 'org.apache.jackrabbit:oak-segment-tar:2.2.0'
  api 'org.apache.jackrabbit:oak-store-document:2.2.0'


  runtimeOnly 'org.eclipse.jetty:jetty-jndi:12.1.11'
  runtimeOnly 'org.eclipse.jetty:jetty-util:12.1.11'
  runtimeOnly 'com.h2database:h2:2.2.224'
  runtimeOnly 'org.postgresql:postgresql:42.7.12'
  runtimeOnly 'net.sourceforge.jtds:jtds:1.3.1'
  runtimeOnly 'io.dropwizard.metrics:metrics-core:4.2.26'
  runtimeOnly 'org.mongodb:mongodb-driver-sync:5.6.2'
  testImplementation gradleTestKit()
  testImplementation platform("org.spockframework:spock-bom:2.4-groovy-3.0")
  testImplementation 'org.spockframework:spock-core'
  testImplementation 'org.spockframework:spock-junit4'
  testImplementation 'junit:junit:4.13.2'
}

ext {
  revision = grgit.head().abbreviatedId
}

jar {
  manifest {
    attributes(
        "Build-Version": project.version,
        "Git-Commit": grgit.head().abbreviatedId)
  }
}

task sourceJar(type: Jar) {
    from sourceSets.main.allSource
}

processTestResources {
  filesMatching('**/*.properties') {
    filter(ReplaceTokens,
        tokens: [buildDir: layout.buildDirectory.get().asFile.path.replace('\\', '/')], beginToken: '${', endToken: '}')
  }
  filesMatching('**/*.xml') {
    filter(ReplaceTokens,
        tokens: [buildDir: layout.buildDirectory.get().asFile.path.replace('\\', '/')], beginToken: '${', endToken: '}')
  }
}
