Skip to content

Commit

Permalink
build: Fix Gradle erroring out when private.properties is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
nhubbard committed Feb 13, 2024
1 parent 742fc9a commit 736ccd7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ jobs:
distribution: 'temurin'
java-version: ${{ matrix.java_version }}
- name: Build and Test with Gradle
run: ./gradlew build
env:
OSS_USER_TOKEN: ${{ secrets.OSS_USER_TOKEN }}
OSS_USER_PASSWORD: ${{ secrets.OSS_USER_PASSWORD }}
run: ./gradlew build
18 changes: 11 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@
import java.util.*

// Helper function to protect private properties from being published
fun getPrivateProperty(key: String): String = file("private.properties").let {
if (it.exists()) {
fun getPrivateProperty(key: String, env: String): String {
val file = file("private.properties")
return if (file.exists()) {
val properties = Properties()
properties.load(it.inputStream())
properties.load(file.inputStream())
properties.getProperty(key)
} else error("Property is empty")
} else {
// Fallback if private.properties is not available
System.getenv(env).takeIf { !it.isNullOrEmpty() } ?:
error("Key $key in private.properties not found, and $env is null or empty!")
}
}

val ossUserToken by extra { getPrivateProperty("ossUserToken") }
val ossUserPassword by extra { getPrivateProperty("ossUserPassword") }
val signPublications by extra { getPrivateProperty("signPublications") }
val ossUserToken by extra { getPrivateProperty("ossUserToken", "OSS_USER_TOKEN") }
val ossUserPassword by extra { getPrivateProperty("ossUserPassword", "OSS_USER_PASSWORD") }

plugins {
java
Expand Down

0 comments on commit 736ccd7

Please sign in to comment.