Protect sensible data in your build.gradle file

Asterios Raptis
2 min readMar 7, 2022
curious look

When you have passwords or other sensible data, you have to protect them from the public audience so nobody can abuse them. So we have to reference to a key in the project gradle.properties that holds another reference to a key to the local file ‘~/.gradle/gradle.properties’. For demonstrate this scenario we will extract the username and password from sonatype.

secure sensible data

Create a project ‘gradle.properties’ in the main directory of your gradle project. and add two property keys:

projectRepositoriesUserNameKey=ossrhUsername
projectRepositoriesPasswordKey=ossrhPassword

And in your local file ‘~/.gradle/gradle.properties’ you have the following two property keys:

ossrhUsername=user1234
ossrhPassword=secret-pw

For now we have take all precondition and can access the sensible data from the build.gradle file as follows:

credentials {
username project.property("$projectRepositoriesUserNameKey")
password project.property("$projectRepositoriesPasswordKey")
}

So the property key from project ‘gradle.properties’ ‘projectRepositoriesUserNameKey’ refers to the property key ‘ossrhUsername’ from the local file ‘~/.gradle/gradle.properties’.

Note that you can also write directy the property key from the local file ‘~/.gradle/gradle.properties’ in the credetials section and it will also resolve the values.

credentials {
username project.property("ossrhUsername")
password project.property("ossrhPassword")
}

Thanks for reading

--

--

Asterios Raptis

Asterios Raptis is a Fullstack Developer and Software Consultant with over three decades of experience in the software development