{"id":13609769,"url":"https://github.com/gmazzo/gradle-buildconfig-plugin","last_synced_at":"2025-05-15T08:04:43.432Z","repository":{"id":37497355,"uuid":"172272573","full_name":"gmazzo/gradle-buildconfig-plugin","owner":"gmazzo","description":"A plugin for generating BuildConstants for any kind of Gradle projects: Java, Kotlin, Groovy, etc. Designed for KTS scripts.","archived":false,"fork":false,"pushed_at":"2025-05-06T10:44:26.000Z","size":862,"stargazers_count":743,"open_issues_count":0,"forks_count":31,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-05-06T11:49:43.251Z","etag":null,"topics":["buildconfig","gradle","gradle-kotlin-dsl","gradle-plugin","java","kotlin","kotlin-dsl","kotlin-js","kotlin-multiplatform","kts"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gmazzo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"gmazzo"}},"created_at":"2019-02-23T22:50:06.000Z","updated_at":"2025-05-06T10:42:53.000Z","dependencies_parsed_at":"2023-10-03T03:21:29.674Z","dependency_job_id":"931ea583-427f-40cd-90a7-be98dd341723","html_url":"https://github.com/gmazzo/gradle-buildconfig-plugin","commit_stats":{"total_commits":284,"total_committers":10,"mean_commits":28.4,"dds":0.3345070422535211,"last_synced_commit":"dc9c7cdb0756ba6470d00bc90ccd5e6a824781b2"},"previous_names":[],"tags_count":35,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmazzo%2Fgradle-buildconfig-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmazzo%2Fgradle-buildconfig-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmazzo%2Fgradle-buildconfig-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmazzo%2Fgradle-buildconfig-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gmazzo","download_url":"https://codeload.github.com/gmazzo/gradle-buildconfig-plugin/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254301422,"owners_count":22047901,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["buildconfig","gradle","gradle-kotlin-dsl","gradle-plugin","java","kotlin","kotlin-dsl","kotlin-js","kotlin-multiplatform","kts"],"created_at":"2024-08-01T19:01:37.852Z","updated_at":"2025-05-15T08:04:43.353Z","avatar_url":"https://github.com/gmazzo.png","language":"Kotlin","funding_links":["https://github.com/sponsors/gmazzo"],"categories":["Kotlin"],"sub_categories":[],"readme":"![GitHub](https://img.shields.io/github/license/gmazzo/gradle-buildconfig-plugin)\n[![Maven Central](https://img.shields.io/maven-central/v/com.github.gmazzo.buildconfig/com.github.gmazzo.buildconfig.gradle.plugin)](https://central.sonatype.com/artifact/com.github.gmazzo.buildconfig/com.github.gmazzo.buildconfig.gradle.plugin)\n[![Gradle Plugin Portal](https://img.shields.io/gradle-plugin-portal/v/com.github.gmazzo.buildconfig)](https://plugins.gradle.org/plugin/com.github.gmazzo.buildconfig)\n[![Build Status](https://github.com/gmazzo/gradle-buildconfig-plugin/actions/workflows/ci-cd.yaml/badge.svg)](https://github.com/gmazzo/gradle-buildconfig-plugin/actions/workflows/ci-cd.yaml)\n[![codecov](https://codecov.io/gh/gmazzo/gradle-buildconfig-plugin/branch/master/graph/badge.svg)](https://codecov.io/gh/gmazzo/gradle-buildconfig-plugin)\n[![Users](https://img.shields.io/badge/users_by-Sourcegraph-purple)](https://sourcegraph.com/search?q=content:com.github.gmazzo.buildconfig+-repo:github.com/gmazzo/gradle-buildconfig-plugin)\n\n# gradle-buildconfig-plugin\n\nA plugin for generating BuildConstants for any kind of Gradle projects: Java, Kotlin, Android, Groovy, etc.\nDesigned for KTS scripts, with *experimental* support for Kotlin's **multi-platform** plugin\n\n## Usage in KTS\n\nOn your `build.gradle.kts` add:\n\n```kotlin\nplugins {\n    id(\"org.jetbrains.kotlin.jvm\") version \"\u003ckotlin version\u003e\"\n    id(\"com.github.gmazzo.buildconfig\") version \"\u003ccurrent version\u003e\"\n}\n\nbuildConfig {\n    buildConfigField(\"APP_NAME\", project.name)\n    buildConfigField(\"APP_VERSION\", provider { \"\\\"${project.version}\\\"\" })\n    buildConfigField(\"APP_SECRET\", \"Z3JhZGxlLWphdmEtYnVpbGRjb25maWctcGx1Z2lu\")\n    buildConfigField\u003cString\u003e(\"OPTIONAL\", null)\n    buildConfigField(\"BUILD_TIME\", System.currentTimeMillis())\n    buildConfigField(\"FEATURE_ENABLED\", true)\n    buildConfigField(\"MAGIC_NUMBERS\", intArrayOf(1, 2, 3, 4))\n    buildConfigField(\"STRING_LIST\", arrayOf(\"a\", \"b\", \"c\"))\n    buildConfigField(\"MAP\", mapOf(\"a\" to 1, \"b\" to 2))\n    buildConfigField(\"FILE\", File(\"aFile\"))\n    buildConfigField(\"URI\", uri(\"https://example.io\"))\n    buildConfigField(\"com.github.gmazzo.buildconfig.demos.kts.SomeData\", \"DATA\", \"SomeData(\\\"a\\\", 1)\")\n\n}\n```\n\nWill generate `BuildConfig.kt`:\n\n```kotlin\ninternal object BuildConfig {\n    internal const val APP_NAME: String = \"kts\"\n    internal const val APP_VERSION: String = \"\\\"0.1.0-demo\\\"\"\n    internal const val APP_SECRET: String = \"Z3JhZGxlLWphdmEtYnVpbGRjb25maWctcGx1Z2lu\"\n    internal val OPTIONAL: String? = null\n    internal const val BUILD_TIME: Long = 1_702_559_872_137L\n    internal const val FEATURE_ENABLED: Boolean = true\n    internal val MAGIC_NUMBERS: IntArray = intArrayOf(1, 2, 3)\n    internal val STRING_LIST: Array\u003cString\u003e = arrayOf(\"a\", \"b\", \"c\")\n    internal val MAP: Map\u003cString, Int\u003e = mapOf(\"a\" to 1, \"b\" to 2)\n    internal val FILE: File = java.io.File(\"aFile\")\n    internal val URI: URI = java.net.URI.create(\"https://example.io\")\n    internal val DATA: SomeData = SomeData(\"a\", 1)\n}\n```\n\n\u003e [!IMPORTANT]\n\u003e Avoid generating `File` entries with `Project.file` API, as they are created with absolute paths, and it will produce\n\u003e cache misses.\n\u003e ```kotlin\n\u003e buildConfigField(\"FILE\", file(\"aFile\")) // will create a file targeting `/your/project/root/aFile` -\u003e DON'T!\n\u003e buildConfigField(\"FILE\", file(\"aFile\").relativeToOrSelf(projectDir)) // use this instead, for instance\n\u003e ```\n\n## Usage in Groovy\n\nOn your `build.gradle` add:\n\n```groovy\nplugins {\n    id 'java'\n    id 'com.github.gmazzo.buildconfig' version \u003c current version \u003e\n}\n\nbuildConfig {\n    buildConfigField(String, 'APP_NAME', project.name)\n    buildConfigField(String, \"APP_VERSION\", provider { project.version })\n    buildConfigField(String, 'APP_SECRET', \"Z3JhZGxlLWphdmEtYnVpbGRjb25maWctcGx1Z2lu\")\n    buildConfigField(String, 'OPTIONAL', null)\n    buildConfigField(long, 'BUILD_TIME', System.currentTimeMillis())\n    buildConfigField(boolean, 'FEATURE_ENABLED', true)\n    buildConfigField(int[], \"MAGIC_NUMBERS\", [1, 2, 3])\n    buildConfigField('List\u003cString\u003e', \"STRING_LIST\", [\"a\", \"b\", \"c\"])\n    buildConfigField(Map.class, \"MAP\", [a: 1, b: 2])\n    buildConfigField(File.class, \"FILE\", new File(\"aFile\"))\n    buildConfigField(URI.class, \"URI\", uri(\"https://example.io\"))\n    buildConfigField(\"com.github.gmazzo.buildconfig.demos.groovy.SomeData\", \"DATA\", \"new SomeData(\\\"a\\\", 1)\")\n}\n```\n\nWill generate `BuildConfig.java`:\n\n```java\nfinal class BuildConfig {\n    public static final String APP_NAME = \"groovy\";\n    public static final String APP_VERSION = \"0.1.0-demo\";\n    public static final String APP_SECRET = \"Z3JhZGxlLWphdmEtYnVpbGRjb25maWctcGx1Z2lu\";\n    public static final String OPTIONAL = null;\n    public static final long BUILD_TIME = 1702559872111L;\n    public static final boolean FEATURE_ENABLED = true;\n    public static final int[] MAGIC_NUMBERS = {1, 2, 3};\n    public static final String[] STRING_LIST = {\"a\", \"b\", \"c\"};\n    public static final Map\u003cString, Integer\u003e MAP = java.util.Map.of(\"a\", 1, \"b\", 2);\n    public static final File FILE = new java.io.File(\"aFile\");\n    public static final URI URI = java.net.URI.create(\"https://example.io\");\n    public static final SomeData DATA = new SomeData(\"a\", 1);\n}\n```\n\n## Customizing the class\n\nIf you add in your `build.gradle.kts`:\n\n```kotlin\nbuildConfig {\n    className(\"MyConfig\")   // forces the class name. Defaults to 'BuildConfig'\n    packageName(\"com.foo\")  // forces the package. Defaults to '${project.group}'\n\n    useJavaOutput()                                 // forces the outputType to 'java'\n    useKotlinOutput()                               // forces the outputType to 'kotlin', generating an `object`\n    useKotlinOutput {\n        topLevelConstants = true\n    }    // forces the outputType to 'kotlin', generating top-level declarations\n    useKotlinOutput { internalVisibility = false }  // makes `BuildConfig` class `public` (defaults to `internal`)\n}\n```\n\nWill generate `com.foo.MyConfig` in a `MyConfig.java` file.\n\n## Secondary classes\n\nSometimes one generated does not fits your needs or code style.\nYou may add multiple classes with the following syntax:\n\n```kotlin\nbuildConfig {\n    buildConfigField(\"FIELD1\", \"field1\")\n\n    forClass(\"OtherClass\") {\n        buildConfigField(\"FIELD2\", \"field2\")\n    }\n\n    forClass(packageName = \"\", className = \"RootConfig\") {\n        buildConfigField(\"FIELD3\", \"field3\")\n    }\n}\n```\n\nWill generate the files:\n\n- `com.github.gmazzo.BuildConfig`\n- `com.github.gmazzo.OtherClass`\n- `RootConfig` (in the root package)\n\n## Generate top-level constants\n\nOn your `build.gradle.kts` add:\n\n```kotlin\nbuildConfig {\n    useKotlinOutput { topLevelConstants = true }\n\n    buildConfigField(\"APP_NAME\", project.name)\n    buildConfigField(\"APP_VERSION\", provider { project.version })\n}\n```\n\nWill generate `BuildConfig.kt`:\n\n```kotlin\nconst val APP_NAME: String = \"example-kts\"\n\nconst val APP_VERSION: String = \"0.0.1\"\n```\n\n## Added documentation (JavaDoc / KDoc) to the generated class\n\nOn your `build.gradle.kts` add:\n\n```kotlin\nbuildConfig {\n    documentation.set(\"Generated by BuildConfig plugin\")\n}\n```\n\n\u003e [!NOTE]\n\u003e `documentation` applies independently for each generated class\n\n## Do not generate classes at Gradle Sync\n\nBy default, all `BuildConfigTask`s will be run as part of the Gradle Sync phase, to improve the developer experiece by\nhaving always an up-to-date version of the generated BuildConfig classes.\n\nYou can turn this behavior by setting the `com.github.gmazzo.buildconfig.generateAtSync` property to `false` in your\n`gradle.properties` file or by using the extension DSL:\n\n```kotlin\nbuildConfig {\n    generateAtSync = false\n}\n```\n\n## Advanced\n\n### Generate constants for 'test' sourceSet (or any)\n\nIf you add in your `build.gradle.kts`:\n\n```kotlin\nbuildConfig {\n    sourceSets.getByName(\"test\") {\n        buildConfigField(\"TEST_CONSTANT\", \"aTestValue\")\n    }\n}\n```\n\nWill generate in `TestBuildConfig.kt`:\n\n```kotlin\nobject TestBuildConfig {\n    const val TEST_CONSTANT: String = \"aTestValue\"\n}\n```\n\n#### Or in Groovy:\n\n```groovy\nsourceSets {\n    test {\n        buildConfig {\n            buildConfigField(String, 'TEST_CONSTANT', 'aTestValue')\n        }\n    }\n}\n```\n\n### Generate constants from resources files\n\nAssuming you have the following structure:\n\n```\nmyproject\n- src\n  - main\n    - resources\n      - config\n        - local.properties\n        - prod.properties\n      - file1.json\n      - file1.json\n```\n\nIf you add in your `build.gradle.kts`:\n\n```kotlin\nval buildResources = buildConfig.forClass(\"BuildResources\")\nval generateResourcesConstants by tasks.registering {\n    val resources = sourceSets[\"main\"].resources.asFileTree\n\n    inputs.files(resources)\n    doFirst {\n        resources.visit(Action\u003cFileVisitDetails\u003e {\n            val name = path.toUpperCase().replace(\"\\\\W\".toRegex(), \"_\")\n\n            buildResources.buildConfigField(\"java.io.File\", name, \"File(\\\"$path\\\")\")\n        })\n    }\n}\n\ntasks.generateBuildConfig {\n    dependsOn(generateResourcesConstants)\n}\n```\n\nWill generate in `BuildResources.kt`:\n\n```kotlin\nobject BuildResources {\n    val CONFIG_LOCAL_PROPERTIES: File = File(\"config/local.properties\")\n\n    val CONFIG_PROD_PROPERTIES: File = File(\"config/prod.properties\")\n\n    val FILE1_JSON: File = File(\"file1.json\")\n\n    val FILE2_JSON: File = File(\"file2.json\")\n}\n```\n\n#### Or in Groovy:\n\n```groovy\ndef buildResources = buildConfig.forClass(\"BuildResources\")\ndef generateResourcesConstants = tasks.register(\"generateResourcesConstants\") {\n    def resources = sourceSets[\"main\"].resources.asFileTree\n\n    inputs.files(resources)\n    doFirst {\n        resources.visit { file -\u003e\n            def name = file.path.toUpperCase().replaceAll(\"\\\\W\", \"_\")\n\n            buildResources.buildConfigField(\"java.io.File\", name, \"new File(\\\"$file.path\\\")\")\n        }\n    }\n}\n\ntasks.generateBuildConfig {\n    dependsOn(generateResourcesConstants)\n}\n```\n\nWill generate in `BuildResources.java`:\n\n```java\n  public static final File CONFIG_LOCAL_PROPERTIES = new File(\"config/local.properties\");\n\npublic static final File CONFIG_PROD_PROPERTIES = new File(\"config/prod.properties\");\n\npublic static final File FILE2_JSON = new File(\"file2.json\");\n\npublic static final File FILE1_JSON = new File(\"file1.json\");\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgmazzo%2Fgradle-buildconfig-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgmazzo%2Fgradle-buildconfig-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgmazzo%2Fgradle-buildconfig-plugin/lists"}