Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KTLN-571] Getting the Value of a Custom Property Defined in 'gradle.properties' in Kotlin #1077

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions kotlin-build-plugins-1/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
plugins {
id("java")
kotlin("jvm") version "1.9.0"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
}

dependencies {
testImplementation(platform("org.junit:junit-bom:5.9.1"))
testImplementation("org.junit.jupiter:junit-jupiter")
implementation(kotlin("stdlib-jdk8"))
}

tasks.test {
useJUnitPlatform()
}
tasks.withType<Test> {
environment("CUSTOM_PROPERTY", project.findProperty("custom.property") as String)
environment("API_URL", project.findProperty("api.url") as String)
}

tasks.register<Copy>("generateCustomProperties") {
from("${project.rootDir}/gradle.properties")
into("src/generated/resources")
rename { "custom.properties" }
}

tasks.named<Copy>("processResources") {
dependsOn("generateCustomProperties")
from("src/generated/resources") {
include("custom.properties")
}
}
kotlin {
jvmToolchain(19)
}
5 changes: 5 additions & 0 deletions kotlin-build-plugins-1/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0"
}
rootProject.name = "kotlin-build-plugins-1"

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.baeldung.getGradlePropertyValue

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.util.Properties

class GetGradlePropertyValueUnitTest {

@Test
fun `obtain environment variables using System getenv method`() {
val customProperty = System.getenv("CUSTOM_PROPERTY")
val apiUrl = System.getenv("API_URL")

assertEquals("CustomValue", customProperty)
assertEquals("https://api.example.com", apiUrl)
}

@Test
fun `obtain custom property value by loading properties from generated file`() {
val propertiesFileUrl = this::class.java.classLoader.getResource("custom.properties")
?: throw IllegalStateException("Properties file not found")

val properties = Properties().apply {
propertiesFileUrl.openStream().use { load(it) }
}

assertEquals("CustomValue", properties.getProperty("custom.property"))
assertEquals("https://api.example.com", properties.getProperty("api.url"))
}
}
17 changes: 17 additions & 0 deletions kotlin-build-plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,20 @@ dependencies {
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.23.0")
testImplementation(kotlin("test"))
}
//tasks.withType<Test> {
// environment("CUSTOM_PROPERTY", project.findProperty("custom.property") as String)
// environment("API_URL", project.findProperty("api.url") as String)
//}
//
//tasks.register<Copy>("generateCustomProperties") {
// from("${project.rootDir}/gradle.properties")
// into("src/generated/resources")
// rename { "custom.properties" }
//}
//
//tasks.named<Copy>("processResources") {
// dependsOn("generateCustomProperties")
// from("src/generated/resources") {
// include("custom.properties")
// }
//}
2 changes: 2 additions & 0 deletions kotlin-build-plugins/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
api.url=https://api.example.com
custom.property=CustomValue
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.baeldung.getGradlePropertyValue

import junit.framework.TestCase.assertEquals
import org.junit.Test
import java.io.File
import java.util.Properties
import java.io.FileInputStream

class GetGradlePropertyValueUnitTest {

@Test
fun `obtain environment variables using System getenv method`() {
val customProperty = System.getenv("CUSTOM_PROPERTY")
val apiUrl = System.getenv("API_URL")

assertEquals("CustomValue", customProperty)
assertEquals("https://api.example.com", apiUrl)
}

@Test
fun `obtain custom property value by loading properties from generated file`() {
val propertiesFileUrl = this::class.java.classLoader.getResource("custom.properties")
?: throw IllegalStateException("Properties file not found")

val properties = Properties().apply {
propertiesFileUrl.openStream().use { load(it) }
}

assertEquals("CustomValue", properties.getProperty("custom.property"))
assertEquals("https://api.example.com", properties.getProperty("api.url"))
}
}