vanniktech / gradle-maven-publish-plugin

A Gradle plugin that publishes your Android and Kotlin libraries, including sources and javadoc, to Maven Central or any other Nexus instance.
https://vanniktech.github.io/gradle-maven-publish-plugin
Apache License 2.0
1.3k stars 119 forks source link

How to set username and password config by project local.properties or by parameters #798

Open zhlinh opened 4 months ago

zhlinh commented 4 months ago

Now provided two ways to set username and password config. by ~/.gradle/gradle.properties or by system env.

But if want different project has different username and password settings, I found it so hard to do it.

  1. I want the plugin to read config in project local.properties, but it can't
  2. Or I read it by myself and set it to this plugin, but I can not find how to set it by parameters
  3. It's hard to change system env in the gradle scripts which I tried, but not working
    private fun Project.setSystemEnv() {
    // set environment variable
    val envMap = mapOf(
        "ORG_GRADLE_PROJECT_mavenCentralUsername" to getLocalProperties("mavenCentralUsername", ""),
        "ORG_GRADLE_PROJECT_mavenCentralPassword" to getLocalProperties("mavenCentralPassword", ""),
        "ORG_GRADLE_PROJECT_signingInMemoryKey" to getSigningInMemoryKey(),
        "ORG_GRADLE_PROJECT_signingInMemoryKeyId" to getLocalProperties("signing.keyId", ""),
        "ORG_GRADLE_PROJECT_signingInMemoryKeyPassword" to getLocalProperties("signing.password", "")
    )
    // or set in ~/.gradle/gradle.properties, but I do not want to share with other projects
    // mavenCentralUsername
    // mavenCentralPassword
    // signing.keyId
    // signing.password
    // signing.secretKeyRingFile
    // detail in https://vanniktech.github.io/gradle-maven-publish-plugin/central/#configuring-the-pom
    tasks.withType(Exec::class.java).configureEach {
        // set env
        environment(envMap)
        envMap.forEach { (key, value) ->
            if (value.isNotEmpty()) {
                System.setProperty(key, value)
            }
        }
    }
    }
alirezat775 commented 4 months ago

I need the same capability, we are using secrets.properties to provide our credentials for the project, and since the gradle.properties is committed to the git, we can not save the credentials in that file, and ~/.gradle/gradle.properties is ok but in that case we need to maintain two places for credentials, and it's preferable to have all of them in the secrets.properties.

basically, I need to provide a new properties file to the gradle plugin and it reads the variables from that file

val secretProperties = Properties().apply {
    load(FileInputStream(File(rootProject.rootDir, "secrets.properties")))
}
properties = secretProperties
Nek-12 commented 4 months ago

Same, we have a lot of properties and creating an env variable for each of them is cumbersome. Instead, we have been just base64'ing and creating a local.properties file with all secrets on each ci deployment. Strange that manual overrides are not possible with this plugin.

Geka000 commented 2 months ago

I've found a workaround only for signing with inMemory key.

plugins {
    signing
}

val signProperties = Properties().apply { load(rootProject.file("lib1.sign.properties").reader()) }
//lib1.sign.properties
//secretKeyId=***
//secretKeyPass=***
//secretInMemoryKey=***

signing {
    useInMemoryPgpKeys(
        signProperties.getProperty("secretKeyId"),
        signProperties.getProperty("secretInMemoryKey"),
        signProperties.getProperty("secretKeyPass")
    )
}

But there is no solution for mavenCentralUsername and mavenCentralPassword