apply plugin: "maven-publish"

configurations.maybeCreate("default")
def publishArtifact = artifacts.add("default", file('src/lib/ahoysdk.aar'))

// set maven repository URL
ext.isSnapshot = ahoy_library_versionName.endsWith("-SNAPSHOT")
def mavenUrl = isSnapshot ? ahoy_publish_snapshotRepositoryUrl : ahoy_publish_releaseRepositoryUrl
if (ahoy_publish_toBuildDir.toBoolean()) {
    mavenUrl = "file://$buildDir/repos/" + isSnapshot ? 'snapshots' : 'releases'
}


publishing {
    publications {
        aar(MavenPublication) {
            groupId = ahoy_library_groupId
            artifactId = ahoy_library_artifactId
            version = ahoy_library_versionName
            artifact publishArtifact

            pom.withXml {
                // 2. add dependencies
                def dependenciesNode = asNode().appendNode('dependencies')
                def node = dependenciesNode.appendNode('dependency')
                node.appendNode('groupId', 'org.java-websocket')
                node.appendNode('artifactId', 'Java-WebSocket')
                node.appendNode('version', '1.5.7')
                node.appendNode('scope', 'compile')

                // 3. configure pom.xml extra information
                asNode().children().last() + {
                    resolveStrategy = Closure.DELEGATE_FIRST
                    name ahoy_library_name
                    url ahoy_library_url
                    description ahoy_library_description
                }
            }
        }
    }
    // Config maven repository
    repositories {
        maven {
            url mavenUrl
            credentials {
                username = ahoy_publish_username
                password = ahoy_publish_password
            }
        }
    }
}

repositories {
    google()
    mavenCentral()
}