import org.elasticsearch.hadoop.gradle.buildtools.info.BuildParams

apply plugin: 'es.hadoop.build.integration'

description = "Elasticsearch Hadoop Hive"

evaluationDependsOn(':elasticsearch-hadoop-mr')

configurations {
    embedded {
        transitive = false
        canBeResolved = true
    }
    implementation {
        extendsFrom project.configurations.embedded
    }
}

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.group == 'org.apache.hadoop') {
            details.useVersion project.ext.hadoopVersion
        }
    }
}

dependencies {
    embedded(project(":elasticsearch-hadoop-mr"))

    implementation("org.apache.hive:hive-service:$hiveVersion") {
        exclude module: "log4j-slf4j-impl"
    }
    implementation("org.apache.hive:hive-exec:$hiveVersion")
    implementation("org.apache.hive:hive-metastore:$hiveVersion")
    implementation("commons-logging:commons-logging:1.1.1")
    implementation("javax.xml.bind:jaxb-api:2.3.1")

    testImplementation(project(":test:shared"))
    testImplementation("org.apache.hive:hive-service:$hiveVersion") {
        exclude module: "log4j-slf4j-impl"
    }
    testImplementation("org.apache.hive:hive-jdbc:$hiveVersion") {
        exclude module: "log4j-slf4j-impl"
    }
    testImplementation("com.fasterxml.jackson.core:jackson-databind:2.7.8")

    itestImplementation(project(":test:shared"))
    itestImplementation("org.apache.hive:hive-service:$hiveVersion") {
        exclude module: "log4j-slf4j-impl"
    }
    itestImplementation("org.apache.hive:hive-jdbc:$hiveVersion") {
        exclude module: "log4j-slf4j-impl"
    }
    itestImplementation("org.apache.tez:tez-dag:0.9.1")
    itestImplementation("com.fasterxml.jackson.core:jackson-databind:2.7.8")

    additionalSources(project(":elasticsearch-hadoop-mr"))
    javadocSources(project(":elasticsearch-hadoop-mr"))
}

tasks.named("test").configure {
    onlyIf { JavaVersion.current() >= JavaVersion.VERSION_16 }
    jvmArgs "--add-opens=java.base/java.io=ALL-UNNAMED" // Needed for IOUtils's BYTE_ARRAY_BUFFER reflection
    jvmArgs "--add-opens=java.base/java.net=ALL-UNNAMED" // Needed for org.apache.hadoop.hive.common.StringInternUtils
}

jar {
    dependsOn(project.configurations.embedded)
    from(project.configurations.embedded.collect { it.isDirectory() ? it : zipTree(it)}) {
        include "org/elasticsearch/hadoop/**"
        include "esh-build.properties"
        include "META-INF/services/*"
    }
}

def hiveSiteProperties = ['testSeed': BuildParams.testSeed]
processItestResources {
    filesMatching("**/hive-site.xml") {
        expand(hiveSiteProperties)
    }
}

itestJar {
    dependsOn(project.configurations.embedded)
    from(project.configurations.embedded.collect { it.isDirectory() ? it : zipTree(it)}) {
        include "org/elasticsearch/hadoop/**"
        include "esh-build.properties"
        include "META-INF/services/*"
    }
}


tasks.register('copyPoms', Copy) {
    BasePluginExtension baseExtension = project.getExtensions().getByType(BasePluginExtension.class);
    from(tasks.named('generatePomFileForMainPublication'))
    into(new File(project.buildDir, 'distributions'))
    rename 'pom-default.xml', "${baseExtension.archivesName.get()}-${project.getVersion()}.pom"
}

tasks.named('distribution').configure {
    dependsOn 'copyPoms'
}
