plugins {
    id 'java-library'
    id 'eclipse'
    id 'idea'
    id 'jacoco'
    id 'org.sonarqube' version '2.8'
    id 'checkstyle'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'com.jfrog.artifactory' version '4.17.2'
}

description = 'Spring Integration Cassandra Support'

group = 'org.springframework.integration'

repositories {
    if (version.endsWith('-SNAPSHOT')) {
        maven { url 'https://repo.spring.io/libs-snapshot' }
    }
    maven { url 'https://repo.spring.io/libs-milestone' }
//	maven { url 'https://repo.spring.io/libs-staging-local' }
}

ext {
    assertjVersion = '3.18.0'
    cassandraUnitVersion = '4.3.1.0'
    junitVersion = '5.7.0'
    reactorVersion = '2020.0.0'
    slf4jVersion = '1.7.30'
    springDataVersion = '2020.0.0'
    springIntegrationVersion = '5.4.0'

    idPrefix = 'cassandra'

    linkHomepage = 'https://github.com/spring-projects/spring-integration-extensions'
    linkCi = 'https://build.spring.io/browse/INTEXT'
    linkIssue = 'https://github.com/spring-projects/spring-integration-extensions/issues'
    linkScmUrl = 'https://github.com/spring-projects/spring-integration-extensions'
    linkScmConnection = 'https://github.com/spring-projects/spring-integration-extensions.git'
    linkScmDevConnection = 'git@github.com:spring-projects/spring-integration-extensions.git'

}

dependencyManagement {
    resolutionStrategy {
        cacheChangingModulesFor 0, 'seconds'
    }
    applyMavenExclusions = false
    generatedPomCustomization {
        enabled = false
    }

    imports {
        mavenBom "org.springframework.integration:spring-integration-bom:$springIntegrationVersion"
        mavenBom "org.springframework.data:spring-data-bom:$springDataVersion"
        mavenBom "io.projectreactor:reactor-bom:$reactorVersion"
        mavenBom "org.junit:junit-bom:$junitVersion"
    }

}

compileJava {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
}

eclipse.project.natures += 'org.springframework.ide.eclipse.core.springnature'

java {
    withJavadocJar()
    withSourcesJar()
}

sourceSets {
    test {
        resources {
            srcDirs = ['src/test/resources', 'src/test/java']
        }
    }
}

jacoco {
    toolVersion = '0.8.6'
}

checkstyle {
    configDirectory.set(rootProject.file("src/checkstyle"))
    toolVersion = '8.36.2'
}


dependencies {
    api 'org.springframework.integration:spring-integration-core'
    api ('org.springframework.data:spring-data-cassandra') {
        exclude group: 'org.codehaus.groovy'
    }

    testImplementation ('org.springframework.integration:spring-integration-test') {
        exclude group: 'junit'
    }

    testImplementation "org.assertj:assertj-core:$assertjVersion"
    testImplementation ("org.cassandraunit:cassandra-unit-spring:$cassandraUnitVersion") {
        exclude group: 'junit'
    }
    testImplementation 'io.projectreactor:reactor-test'
    testImplementation 'org.junit.jupiter:junit-jupiter-api'

    testRuntimeOnly "org.slf4j:jcl-over-slf4j:$slf4jVersion"
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

// enable all compiler warnings; individual projects may customize further
[compileJava, compileTestJava]*.options*.compilerArgs = ['-Xlint:all,-options']

test {
    // suppress all console output during testing unless running `gradle -i`
    logging.captureStandardOutput(LogLevel.INFO)
    jacoco {
        destinationFile = file("$buildDir/jacoco.exec")
    }
    useJUnitPlatform()
}

jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination file("$buildDir/reports/jacoco/html")
    }
}

check.dependsOn javadoc
build.dependsOn jacocoTestReport

sonarqube {
    properties {
        property 'sonar.jacoco.reportPath', "${buildDir.name}/jacoco.exec"
        property 'sonar.links.homepage', linkHomepage
        property 'sonar.links.ci', linkCi
        property 'sonar.links.issue', linkIssue
        property 'sonar.links.scm', linkScmUrl
        property 'sonar.links.scm_dev', linkScmDevConnection
        property 'sonar.java.coveragePlugin', 'jacoco'
    }
}

task api(type: Javadoc) {
    group = 'Documentation'
    description = 'Generates the Javadoc API documentation.'
    title = "${rootProject.description} ${version} API"
    options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PROTECTED
    options.author = true
    options.header = rootProject.description
    options.overview = 'src/api/overview.html'

    source = sourceSets.main.allJava
    classpath = project.sourceSets.main.compileClasspath
    destinationDir = new File(buildDir, "api")
}

task schemaZip(type: Zip) {
    group = 'Distribution'
    archiveClassifier = 'schema'
    description = "Builds -${archiveClassifier} archive containing all " +
            "XSDs for deployment at static.springframework.org/schema."

    def Properties schemas = new Properties();
    def shortName = idPrefix.replaceFirst("${idPrefix}-", '')

    project.sourceSets.main.resources.find {
        it.path.endsWith("META-INF${File.separator}spring.schemas")
    }?.withInputStream { schemas.load(it) }

    for (def key : schemas.keySet()) {
        File xsdFile = project.sourceSets.main.resources.find {
            it.path.replaceAll('\\\\', '/').endsWith(schemas.get(key))
        }
        assert xsdFile != null
        into("integration/${shortName}") {
            from xsdFile.path
        }
    }

}

task docsZip(type: Zip) {
    group = 'Distribution'
    archiveClassifier = 'docs'
    description = "Builds -${archiveClassifier} archive containing api " +
            "for deployment at static.spring.io/spring-integration/docs."

    from('src/dist') {
        include 'changelog.txt'
    }

    from(javadoc) {
        into 'api'
    }
}

task distZip(type: Zip, dependsOn: [docsZip, schemaZip]) {
    group = 'Distribution'
    archiveClassifier = 'dist'
    description = "Builds -${archiveClassifier} archive, containing all jars and docs, " +
            "suitable for community download page."

    ext.baseDir = "${project.name}-${project.version}";

    from('src/dist') {
        include 'readme.txt'
        include 'license.txt'
        into "${baseDir}"
    }

    from(zipTree(docsZip.archiveFile)) {
        into "${baseDir}/docs"
    }

    from(zipTree(schemaZip.archiveFile)) {
        into "${baseDir}/schema"
    }

    into("${baseDir}/libs") {
        from project.jar
        from project.sourcesJar
        from project.javadocJar
    }
}

// Create an optional "with dependencies" distribution.
// Not published by default; only for use when building from source.
task depsZip(type: Zip, dependsOn: distZip) { zipTask ->
    group = 'Distribution'
    archiveClassifier = 'dist-with-deps'
    description = "Builds -${archiveClassifier} archive, containing everything " +
            "in the -${distZip.archiveClassifier} archive plus all dependencies."

    from zipTree(distZip.archiveFile)

    gradle.taskGraph.whenReady { taskGraph ->
        if (taskGraph.hasTask(":${zipTask.name}")) {
            def projectName = rootProject.name
            def artifacts = new HashSet()

            rootProject.configurations.runtime.resolvedConfiguration.resolvedArtifacts.each { artifact ->
                def dependency = artifact.moduleVersion.id
                if (!projectName.equals(dependency.name)) {
                    artifacts << artifact.file
                }
            }

            zipTask.from(artifacts) {
                into "${distZip.baseDir}/deps"
            }
        }
    }
}

task dist(dependsOn: assemble) {
    group = 'Distribution'
    description = 'Builds -dist, -docs and -schema distribution archives.'
}

apply from: "${rootProject.projectDir}/publish-maven.gradle"
