aboutsummaryrefslogtreecommitdiff
path: root/buildSrc/src/main/java/BintrayPublishing.kt
blob: 1429b0cb63b1708e0649cbedfa37d79089e71e68 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved.
 * SPDX-License-Identifier: GPL-3.0-only
 */

import com.android.build.gradle.TestedExtension
import java.util.Locale
import org.gradle.api.Project
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.jvm.tasks.Jar
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.register

/**
 * Register the `sourcesJar` task so that our published artifacts can include them.
 */
internal fun TestedExtension.registerSourcesJarTask(project: Project) {
    project.tasks.register<Jar>("sourcesJar") {
        archiveClassifier.set("sources")
        from(sourceSets.getByName("main").java.srcDirs)
    }
}

/**
 * Configures the `apsMaven` and `bintray` repositories along with an `aps` publication
 */
internal fun PublishingExtension.configureMavenPublication(project: Project) {
    repositories {
        maven {
            val artifactId = project.getKey("POM_ARTIFACT_ID")
            name = "bintray"
            url = project.uri("https://api.bintray.com/maven/android-password-store/$artifactId/$artifactId/;publish=1;override=0")
            credentials {
                username = project.getCredential("user")
                password = project.getCredential("password")
            }
        }
    }
    publications {
        create<MavenPublication>("aps") {
            from(project.components.getByName("release"))
            groupId = project.getKey("GROUP")
            artifactId = project.getKey("POM_ARTIFACT_ID")
            version = project.getKey("VERSION_NAME")
            artifact(project.tasks.getByName("sourcesJar"))
            configurePom(project)
        }
    }
}

private fun Project.getCredential(type: String): String {
    return when (type) {
        // Attempt to find credentials passed by -Pmaven.$type=
        "user", "password" -> (findProperty("maven.$type")
        // Fall back to MAVEN_$type from env
            ?: System.getenv("MAVEN_${type.toUpperCase(Locale.ROOT)}"))?.toString()
        // Finally fallthrough to an empty string to let task configuration complete
        // even if actual publishing is going to fail
            ?: ""
        else -> throw IllegalArgumentException("Invalid credential type: $type")
    }
}

private fun Project.getKey(propertyName: String): String {
    return findProperty(propertyName)?.toString()
        ?: error("Failed to find value for property: $propertyName")
}

private fun MavenPublication.configurePom(project: Project) {
    pom {
        name.set(project.getKey("POM_ARTIFACT_ID"))
        description.set(project.getKey("POM_ARTIFACT_DESCRIPTION"))
        url.set(project.getKey("POM_URL"))
        licenses {
            license {
                name.set(project.getKey("POM_LICENSE_NAME"))
                url.set(project.getKey("POM_LICENSE_URL"))
            }
        }
        developers {
            developer {
                id.set(project.getKey("POM_DEVELOPER_ID"))
                name.set(project.getKey("POM_DEVELOPER_NAME"))
                email.set(project.getKey("POM_DEVELOPER_EMAIL"))
            }
        }
        scm {
            connection.set(project.getKey("POM_SCM_CONNECTION"))
            developerConnection.set(project.getKey("POM_SCM_DEV_CONNECTION"))
            url.set(project.getKey("POM_SCM_URL"))
        }
    }
}