gradle-maven-publish-plugin icon indicating copy to clipboard operation
gradle-maven-publish-plugin copied to clipboard

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

Open zhlinh opened this issue 1 year ago • 3 comments

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)
            }
        }
    }
}

zhlinh avatar Jun 25 '24 05:06 zhlinh

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

alirezat775 avatar Jun 26 '24 16:06 alirezat775

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.

Nek-12 avatar Jul 05 '24 14:07 Nek-12

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

Geka000 avatar Aug 13 '24 21:08 Geka000

Not planning to add support for this. The username and password are accessed through the Gradle credentials API so Gradle would need to add support for this.

gabrielittner avatar Apr 22 '25 21:04 gabrielittner

The only thing you would need to change in MavenPublishBaseExtension.kt is here:

val buildService = project.registerMavenCentralBuildService(
  repositoryUsername = project.providers.gradleProperty("mavenCentralUsername"),
  repositoryPassword = project.providers.gradleProperty("mavenCentralPassword"),
  rootBuildDirectory = @Suppress("UnstableApiUsage") project.layout.settingsDirectory.dir("build"),
  buildEventsListenerRegistry = buildEventsListenerRegistry,
)

Change project.providers.gradleProperty to project.findProperty

Then, users can use

extra["mavenCentralUsername"] = ...
extra["mavenCentralPassword"] = ...

to set this programatically with a value from a secret store.

This is what the Gradle publish plugin does, for example.

ingokegel avatar Aug 19 '25 19:08 ingokegel