plugins {
    id "java-library"
    id "maven-publish"

    id "com.github.johnrengelman.shadow"
    id "com.google.protobuf"
    id "ru.vyarus.animalsniffer"
}

description = "gRPC: Authorization"

dependencies {
    implementation project(':grpc-protobuf'),
            project(':grpc-core')

    annotationProcessor libraries.autovalue
    compileOnly libraries.javax_annotation

    testImplementation project(':grpc-testing'),
            project(':grpc-testing-proto')
    testImplementation (libraries.guava_testlib) {
        exclude group: 'junit', module: 'junit'
    }

    def xdsDependency = implementation project(':grpc-xds')
    shadow configurations.implementation.getDependencies().minus([xdsDependency])
    shadow project(path: ':grpc-xds', configuration: 'shadow')

    signature "org.codehaus.mojo.signature:java17:1.0@signature"
}

jar {
    classifier = 'original'
}

// TODO(ashithasantosh): Remove javadoc exclusion on adding authorization
// interceptor implementations.
javadoc {
    exclude "io/grpc/authz/*"
}

shadowJar {
    classifier = null
    dependencies {
        exclude(dependency {true})
    }
    relocate 'io.grpc.xds', 'io.grpc.xds.shaded.io.grpc.xds'
    relocate 'udpa.annotations', 'io.grpc.xds.shaded.udpa.annotations'
    relocate 'com.github.udpa', 'io.grpc.xds.shaded.com.github.udpa'
    relocate 'envoy.annotations', 'io.grpc.xds.shaded.envoy.annotations'
    relocate 'io.envoyproxy', 'io.grpc.xds.shaded.io.envoyproxy'
    relocate 'com.google.api.expr', 'io.grpc.xds.shaded.com.google.api.expr'
}

publishing {
    publications {
        maven(MavenPublication) {
            // We want this to throw an exception if it isn't working
            def originalJar = artifacts.find { dep -> dep.classifier == 'original'}
            artifacts.remove(originalJar)

            pom.withXml {
                def dependenciesNode = new Node(null, 'dependencies')
                project.configurations.shadow.allDependencies.each { dep ->
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', dep.group)
                    dependencyNode.appendNode('artifactId', dep.name)
                    dependencyNode.appendNode('version', dep.version)
                    dependencyNode.appendNode('scope', 'compile')
                }
                asNode().dependencies[0].replaceNode(dependenciesNode)
            }
        }
    }
}

[publishMavenPublicationToMavenRepository]*.onlyIf {false}
