├── .gitignore ├── examples ├── spring-boot │ ├── src │ │ ├── main │ │ │ ├── resources │ │ │ │ └── application.properties │ │ │ └── kotlin │ │ │ │ └── com │ │ │ │ └── example │ │ │ │ └── demo │ │ │ │ └── DemoApplication.kt │ │ └── test │ │ │ └── kotlin │ │ │ └── com │ │ │ └── example │ │ │ └── demo │ │ │ └── DemoApplicationTests.kt │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── settings.gradle.kts │ ├── .gitignore │ ├── package.nix │ ├── build.gradle.kts │ ├── gradlew.bat │ └── gradlew ├── funky-dependencies │ ├── gradle.properties │ ├── gradle │ │ ├── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ │ └── verification-metadata.xml │ ├── settings.gradle.kts │ ├── .gitignore │ ├── src │ │ └── main │ │ │ └── kotlin │ │ │ └── Main.kt │ ├── package.nix │ ├── build.gradle.kts │ ├── gradlew.bat │ └── gradlew ├── hello-world │ ├── gradle │ │ ├── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ │ └── verification-metadata.xml │ ├── src │ │ └── main │ │ │ └── kotlin │ │ │ └── Main.kt │ ├── gradle.properties │ ├── settings.gradle.kts │ ├── .gitignore │ ├── package.nix │ ├── build.gradle.kts │ ├── gradlew.bat │ └── gradlew └── default.nix ├── update-verification-metadata ├── listIncludedBuildsRelative.init.gradle.kts ├── update-verification-metadata.py ├── default.nix └── merge-verification-metadata.py ├── .github └── workflows │ ├── update-flake-lock.yml │ └── ci.yml ├── fetchArtefact ├── default.nix └── builder.bash ├── LICENSE ├── flake.lock ├── flake.nix ├── gradleFromWrapper └── default.nix ├── README.md └── docs └── buildGradleApplication-flow.drawio.svg /.gitignore: -------------------------------------------------------------------------------- 1 | result -------------------------------------------------------------------------------- /examples/spring-boot/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /examples/funky-dependencies/gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official 2 | # implicitly verified by the nix build 3 | org.gradle.dependency.verification=off 4 | -------------------------------------------------------------------------------- /examples/hello-world/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raphiz/buildGradleApplication/HEAD/examples/hello-world/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /examples/hello-world/src/main/kotlin/Main.kt: -------------------------------------------------------------------------------- 1 | import kotlin.random.Random; 2 | 3 | fun main() { 4 | println("Hello Kotlin!") 5 | println(Random.nextInt(2)) 6 | } 7 | -------------------------------------------------------------------------------- /examples/spring-boot/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raphiz/buildGradleApplication/HEAD/examples/spring-boot/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /examples/funky-dependencies/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/raphiz/buildGradleApplication/HEAD/examples/funky-dependencies/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /examples/hello-world/gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official 2 | org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 3 | org.gradle.parallel=true 4 | 5 | -------------------------------------------------------------------------------- /examples/spring-boot/src/test/kotlin/com/example/demo/DemoApplicationTests.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo 2 | 3 | import org.junit.jupiter.api.Test 4 | import org.springframework.boot.test.context.SpringBootTest 5 | 6 | @SpringBootTest 7 | class DemoApplicationTests { 8 | 9 | @Test 10 | fun contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /examples/spring-boot/settings.gradle.kts: -------------------------------------------------------------------------------- 1 | rootProject.name = "demo" 2 | pluginManagement { 3 | repositories { 4 | gradlePluginPortal() 5 | } 6 | } 7 | 8 | dependencyResolutionManagement { 9 | repositories { 10 | mavenCentral() 11 | } 12 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 13 | } 14 | -------------------------------------------------------------------------------- /examples/spring-boot/src/main/kotlin/com/example/demo/DemoApplication.kt: -------------------------------------------------------------------------------- 1 | package com.example.demo 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication 4 | import org.springframework.boot.runApplication 5 | 6 | @SpringBootApplication 7 | class DemoApplication 8 | 9 | fun main(args: Array) { 10 | runApplication(*args) 11 | } 12 | -------------------------------------------------------------------------------- /examples/hello-world/settings.gradle.kts: -------------------------------------------------------------------------------- 1 | rootProject.name = "example" 2 | enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") 3 | 4 | pluginManagement { 5 | repositories { 6 | gradlePluginPortal() 7 | } 8 | } 9 | 10 | dependencyResolutionManagement { 11 | repositories { 12 | mavenCentral() 13 | } 14 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 15 | } 16 | 17 | -------------------------------------------------------------------------------- /examples/hello-world/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip 4 | distributionSha256Sum=f397b287023acdba1e9f6fc5ea72d22dd63669d59ed4a289a29b1a76eee151c6 5 | networkTimeout=10000 6 | validateDistributionUrl=true 7 | zipStoreBase=GRADLE_USER_HOME 8 | zipStorePath=wrapper/dists 9 | -------------------------------------------------------------------------------- /examples/spring-boot/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip 4 | distributionSha256Sum=f397b287023acdba1e9f6fc5ea72d22dd63669d59ed4a289a29b1a76eee151c6 5 | networkTimeout=10000 6 | validateDistributionUrl=true 7 | zipStoreBase=GRADLE_USER_HOME 8 | zipStorePath=wrapper/dists 9 | -------------------------------------------------------------------------------- /examples/funky-dependencies/settings.gradle.kts: -------------------------------------------------------------------------------- 1 | rootProject.name = "example" 2 | enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS") 3 | 4 | pluginManagement { 5 | repositories { 6 | gradlePluginPortal() 7 | } 8 | } 9 | 10 | dependencyResolutionManagement { 11 | repositories { 12 | mavenCentral() 13 | } 14 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 15 | } 16 | 17 | -------------------------------------------------------------------------------- /examples/funky-dependencies/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip 4 | distributionSha256Sum=f397b287023acdba1e9f6fc5ea72d22dd63669d59ed4a289a29b1a76eee151c6 5 | networkTimeout=10000 6 | validateDistributionUrl=true 7 | zipStoreBase=GRADLE_USER_HOME 8 | zipStorePath=wrapper/dists 9 | -------------------------------------------------------------------------------- /update-verification-metadata/listIncludedBuildsRelative.init.gradle.kts: -------------------------------------------------------------------------------- 1 | gradle.rootProject { 2 | tasks.register("listIncludedBuilds") { 3 | doLast { 4 | val rootPath = rootDir.toPath().toAbsolutePath().normalize() 5 | gradle.includedBuilds.forEach { build -> 6 | val includedPath = build.projectDir.toPath().toAbsolutePath().normalize() 7 | println(rootPath.relativize(includedPath)) 8 | } 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /examples/default.nix: -------------------------------------------------------------------------------- 1 | {config, ...}: let 2 | overlay = config.flake.overlays.default; 3 | in { 4 | perSystem = { 5 | lib, 6 | pkgs, 7 | self', 8 | ... 9 | }: let 10 | pkgs' = pkgs.extend overlay; 11 | exampleApps = 12 | lib.attrNames (lib.filterAttrs (name: pathType: pathType == "directory") (builtins.readDir ./.)); 13 | examplePkgFor = name: 14 | pkgs'.callPackage ./${name}/package.nix {}; 15 | in { 16 | checks = lib.listToAttrs (map 17 | (name: { 18 | name = "example-${name}"; 19 | value = examplePkgFor name; 20 | }) 21 | exampleApps); 22 | }; 23 | } 24 | -------------------------------------------------------------------------------- /examples/spring-boot/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | .gradle 3 | build/ 4 | !gradle/wrapper/gradle-wrapper.jar 5 | !**/src/main/**/build/ 6 | !**/src/test/**/build/ 7 | 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | bin/ 17 | !**/src/main/**/bin/ 18 | !**/src/test/**/bin/ 19 | 20 | ### IntelliJ IDEA ### 21 | .idea 22 | *.iws 23 | *.iml 24 | *.ipr 25 | out/ 26 | !**/src/main/**/out/ 27 | !**/src/test/**/out/ 28 | 29 | ### NetBeans ### 30 | /nbproject/private/ 31 | /nbbuild/ 32 | /dist/ 33 | /nbdist/ 34 | /.nb-gradle/ 35 | 36 | ### VS Code ### 37 | .vscode/ 38 | -------------------------------------------------------------------------------- /.github/workflows/update-flake-lock.yml: -------------------------------------------------------------------------------- 1 | name: Update flake.lock 2 | 3 | on: 4 | workflow_dispatch: # allows manual triggering 5 | schedule: 6 | - cron: '0 0 * * 0' # runs weekly on Sunday at 00:00 7 | 8 | jobs: 9 | update: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/checkout@v4.1.0 13 | - uses: DeterminateSystems/nix-installer-action@v2 14 | - uses: DeterminateSystems/magic-nix-cache-action@v2 15 | - uses: DeterminateSystems/update-flake-lock@v20 16 | with: 17 | pr-title: "Update flake.lock" 18 | 19 | permissions: 20 | contents: write 21 | pull-requests: write 22 | -------------------------------------------------------------------------------- /fetchArtefact/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | stdenvNoCC, 3 | curl, 4 | nix, 5 | cacert, 6 | }: { 7 | # A list of URLs specifying alternative download locations. They are tried in order. 8 | urls, 9 | # SRI hash. 10 | hash, 11 | # Name of the file. 12 | name, 13 | }: 14 | stdenvNoCC.mkDerivation { 15 | inherit name hash; 16 | outputHash = hash; 17 | outputHashMode = "flat"; 18 | 19 | builder = ./builder.bash; 20 | nativeBuildInputs = [curl nix]; 21 | SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt"; 22 | inherit urls; 23 | 24 | # Doing the download on a remote machine just duplicates network 25 | # traffic, so don't do that 26 | preferLocalBuild = true; 27 | } 28 | -------------------------------------------------------------------------------- /examples/hello-world/.gitignore: -------------------------------------------------------------------------------- 1 | ### Nix ### 2 | /result 3 | 4 | .gradle 5 | build/ 6 | !gradle/wrapper/gradle-wrapper.jar 7 | !**/src/main/**/build/ 8 | !**/src/test/**/build/ 9 | 10 | ### IntelliJ IDEA ### 11 | .idea/ 12 | *.iws 13 | *.iml 14 | *.ipr 15 | out/ 16 | !**/src/main/**/out/ 17 | !**/src/test/**/out/ 18 | 19 | ### Eclipse ### 20 | .apt_generated 21 | .classpath 22 | .factorypath 23 | .project 24 | .settings 25 | .springBeans 26 | .sts4-cache 27 | bin/ 28 | !**/src/main/**/bin/ 29 | !**/src/test/**/bin/ 30 | 31 | ### NetBeans ### 32 | /nbproject/private/ 33 | /nbbuild/ 34 | /dist/ 35 | /nbdist/ 36 | /.nb-gradle/ 37 | 38 | ### VS Code ### 39 | .vscode/ 40 | 41 | ### Mac OS ### 42 | .DS_Store 43 | -------------------------------------------------------------------------------- /examples/funky-dependencies/.gitignore: -------------------------------------------------------------------------------- 1 | ### Nix ### 2 | /result 3 | 4 | .gradle 5 | build/ 6 | !gradle/wrapper/gradle-wrapper.jar 7 | !**/src/main/**/build/ 8 | !**/src/test/**/build/ 9 | 10 | ### IntelliJ IDEA ### 11 | .idea/ 12 | *.iws 13 | *.iml 14 | *.ipr 15 | out/ 16 | !**/src/main/**/out/ 17 | !**/src/test/**/out/ 18 | 19 | ### Eclipse ### 20 | .apt_generated 21 | .classpath 22 | .factorypath 23 | .project 24 | .settings 25 | .springBeans 26 | .sts4-cache 27 | bin/ 28 | !**/src/main/**/bin/ 29 | !**/src/test/**/bin/ 30 | 31 | ### NetBeans ### 32 | /nbproject/private/ 33 | /nbbuild/ 34 | /dist/ 35 | /nbdist/ 36 | /.nb-gradle/ 37 | 38 | ### VS Code ### 39 | .vscode/ 40 | 41 | ### Mac OS ### 42 | .DS_Store 43 | -------------------------------------------------------------------------------- /examples/spring-boot/package.nix: -------------------------------------------------------------------------------- 1 | { 2 | lib, 3 | jdk, 4 | version ? "1.0.0", 5 | buildGradleApplication, 6 | gradleFromWrapper, 7 | }: let 8 | gradle = gradleFromWrapper { 9 | wrapperPropertiesPath = ./gradle/wrapper/gradle-wrapper.properties; 10 | defaultJava = jdk; 11 | }; 12 | in 13 | buildGradleApplication { 14 | inherit gradle version jdk; 15 | pname = "spring-boot"; 16 | src = ./.; 17 | meta = with lib; { 18 | description = "Spring Boot Example Application"; 19 | longDescription = '' 20 | Will start a server at Port 8080 21 | ''; 22 | sourceProvenance = with sourceTypes; [ 23 | fromSource 24 | binaryBytecode 25 | ]; 26 | platforms = platforms.unix; 27 | }; 28 | } 29 | -------------------------------------------------------------------------------- /examples/funky-dependencies/src/main/kotlin/Main.kt: -------------------------------------------------------------------------------- 1 | import com.github.ajalt.clikt.core.CliktCommand 2 | import com.github.ajalt.clikt.core.main 3 | import com.github.ajalt.clikt.parameters.options.default 4 | import com.github.ajalt.clikt.parameters.options.help 5 | import com.github.ajalt.clikt.parameters.options.option 6 | import com.github.ajalt.clikt.parameters.options.prompt 7 | import com.github.ajalt.clikt.parameters.types.int 8 | 9 | class Hello : CliktCommand() { 10 | val count: Int by option().int().default(1).help("Number of greetings") 11 | val name: String by option().prompt("Your name").help("The person to greet") 12 | 13 | override fun run() { 14 | repeat(count) { 15 | echo("Hello $name!") 16 | } 17 | } 18 | } 19 | 20 | fun main(args: Array) = Hello().main(args) -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: [ push ] 4 | 5 | jobs: 6 | check: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 10 | - uses: nixbuild/nix-quick-install-action@2c9db80fb984ceb1bcaa77cdda3fdf8cfba92035 # v34 11 | - uses: nix-community/cache-nix-action@135667ec418502fa5a3598af6fb9eb733888ce6a # v6 12 | with: 13 | primary-key: nix-${{ runner.os }}-${{ hashFiles('**/*.nix', '**/flake.lock') }} 14 | restore-prefixes-first-match: nix-${{ runner.os }}- 15 | gc-max-store-size-linux: 3.5G 16 | 17 | # purge caches 18 | purge: true 19 | purge-prefixes: nix-${{ runner.os }}- 20 | purge-created: 0 21 | purge-primary-key: never 22 | gc-max-store-size: 0 23 | - run: nix flake check 24 | -------------------------------------------------------------------------------- /update-verification-metadata/update-verification-metadata.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import xml.etree.ElementTree as ET 3 | 4 | verification_file = sys.argv[1] 5 | whitelist = sys.argv[1:] 6 | whitelist_set = set(tuple(item.split(':')) for item in whitelist) 7 | 8 | namespaces={'': "https://schema.gradle.org/dependency-verification"} 9 | ET.register_namespace("", namespaces['']) 10 | 11 | tree = ET.parse(verification_file) 12 | 13 | root = tree.getroot() 14 | components = root.find('components', namespaces) 15 | if (components is not None): 16 | for component in list(components): 17 | group = component.get('group') 18 | name = component.get('name') 19 | version = component.get('version') 20 | if (group, name, version) not in whitelist_set: 21 | components.remove(component) 22 | 23 | tree.write(verification_file, encoding='UTF-8', xml_declaration=True) 24 | 25 | -------------------------------------------------------------------------------- /examples/hello-world/package.nix: -------------------------------------------------------------------------------- 1 | { 2 | lib, 3 | jdk, 4 | version ? "1.0.0", 5 | buildGradleApplication, 6 | gradleFromWrapper, 7 | }: let 8 | gradle = gradleFromWrapper { 9 | wrapperPropertiesPath = ./gradle/wrapper/gradle-wrapper.properties; 10 | defaultJava = jdk; 11 | }; 12 | in 13 | buildGradleApplication { 14 | inherit gradle version jdk; 15 | pname = "hello-world"; 16 | src = lib.cleanSourceWith { 17 | src = lib.cleanSource ./.; 18 | filter = path: type: let 19 | ignore = builtins.elem (baseNameOf path); 20 | in 21 | ! ignore [ 22 | "package.nix" 23 | "gradlew.bat" 24 | "gradlew" 25 | ]; 26 | }; 27 | 28 | meta = with lib; { 29 | description = "Hello World Application"; 30 | longDescription = '' 31 | Not much to say here... 32 | ''; 33 | sourceProvenance = with sourceTypes; [ 34 | fromSource 35 | binaryBytecode 36 | ]; 37 | platforms = platforms.unix; 38 | }; 39 | } 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Raphael Zimmermann 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /examples/hello-world/build.gradle.kts: -------------------------------------------------------------------------------- 1 | import org.jetbrains.kotlin.gradle.dsl.JvmTarget 2 | import org.jetbrains.kotlin.gradle.dsl.KotlinVersion 3 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile 4 | 5 | plugins { 6 | kotlin("jvm") version "2.1.0" 7 | application 8 | } 9 | 10 | application { 11 | mainClass.set("MainKt") 12 | } 13 | 14 | group = "org.example" 15 | version = System.getenv("APP_VERSION") ?: "dirty" 16 | 17 | dependencies { 18 | implementation(kotlin("stdlib-jdk8")) 19 | testImplementation(kotlin("test")) 20 | } 21 | 22 | tasks.test { 23 | useJUnitPlatform() 24 | } 25 | 26 | val javaVersion = JavaLanguageVersion.of("21") 27 | 28 | java { 29 | toolchain { 30 | languageVersion.set(javaVersion) 31 | } 32 | } 33 | 34 | tasks.withType { 35 | compilerOptions { 36 | freeCompilerArgs.set(listOf("-Xjsr305=strict")) 37 | jvmTarget.set(JvmTarget.fromTarget(javaVersion.toString())) 38 | } 39 | } 40 | 41 | 42 | tasks.withType().configureEach { 43 | isPreserveFileTimestamps = false 44 | isReproducibleFileOrder = true 45 | } 46 | 47 | configurations.all { 48 | resolutionStrategy { 49 | failOnNonReproducibleResolution() 50 | } 51 | } -------------------------------------------------------------------------------- /examples/funky-dependencies/package.nix: -------------------------------------------------------------------------------- 1 | { 2 | lib, 3 | jdk, 4 | version ? "1.0.0", 5 | buildGradleApplication, 6 | gradleFromWrapper, 7 | }: let 8 | gradle = gradleFromWrapper { 9 | wrapperPropertiesPath = ./gradle/wrapper/gradle-wrapper.properties; 10 | defaultJava = jdk; 11 | }; 12 | in 13 | buildGradleApplication { 14 | inherit gradle version jdk; 15 | pname = "funky-dependencies"; 16 | src = ./.; 17 | meta = with lib; { 18 | description = "Example project with funky dependencies"; 19 | longDescription = '' 20 | Lots of libraries have references to (metadata) jars that have a different url than the name in the verification.xml file, for example: 21 | - kasechange-metadata-1.4.1.jar (net.pearx.kasechange:kasechange) 22 | - kotlin-result-metadata-2.0.0.jar (com.michael-bull.kotlin-result:kotlin-result) 23 | - kotlinx-serialization-core-metadata-1.7.0.jar (org.jetbrains.kotlinx:kotlinx-serialization-core) 24 | To get the proper URL, the corresponding gradle module metadata (.module) must first be analyzed to get the correct URL. 25 | ''; 26 | sourceProvenance = with sourceTypes; [ 27 | fromSource 28 | binaryBytecode 29 | ]; 30 | platforms = platforms.unix; 31 | }; 32 | } 33 | -------------------------------------------------------------------------------- /update-verification-metadata/default.nix: -------------------------------------------------------------------------------- 1 | { 2 | lib, 3 | python3, 4 | git, 5 | writeShellApplication, 6 | updateAction ? "dependencies", 7 | gradle, 8 | cmd ? "${lib.getExe gradle} --refresh-dependencies --write-verification-metadata sha256 ${updateAction}", 9 | verificationFile ? "gradle/verification-metadata.xml", 10 | whitelist ? [], 11 | }: 12 | writeShellApplication { 13 | name = "update-verification-metadata"; 14 | 15 | runtimeInputs = [python3 git]; 16 | text = '' 17 | verificationFile=''${1:-${verificationFile}} 18 | if [ ! -f "$verificationFile" ] 19 | then 20 | echo "WARNING: $verificationFile does not (yet) exist." 1>&2 21 | fi 22 | 23 | update_metadata() { 24 | if [ -f "$verificationFile" ]; then 25 | echo "Removing all component entries from $build/$verificationFile for build $build" 26 | fi 27 | python ${./update-verification-metadata.py} "$verificationFile" ${builtins.toString (builtins.map lib.escapeShellArg whitelist)} 28 | echo "(Re)generating gradle verification data for build $build" 29 | ${cmd} 30 | } 31 | 32 | 33 | echo "Locating included builds" 34 | includedBuilds=$(${lib.getExe gradle} -q -Dorg.gradle.unsafe.isolated-projects=false --no-configuration-cache --init-script ${./listIncludedBuildsRelative.init.gradle.kts} listIncludedBuilds) 35 | for build in $includedBuilds; do 36 | pushd "$build" > /dev/null 37 | update_metadata 38 | popd > /dev/null 39 | done 40 | 41 | build=. update_metadata 42 | 43 | python ${./merge-verification-metadata.py} "$verificationFile" "''${includedBuilds[@]}" 44 | ''; 45 | } 46 | -------------------------------------------------------------------------------- /examples/spring-boot/build.gradle.kts: -------------------------------------------------------------------------------- 1 | import org.jetbrains.kotlin.gradle.dsl.JvmTarget 2 | import org.jetbrains.kotlin.gradle.dsl.KotlinVersion 3 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile 4 | 5 | plugins { 6 | id("org.springframework.boot") version "3.4.0" 7 | id("io.spring.dependency-management") version "1.1.6" 8 | kotlin("jvm") version "2.1.0" 9 | kotlin("plugin.spring") version "2.1.0" 10 | application 11 | } 12 | 13 | application { 14 | mainClass.set("com.example.demo.DemoApplicationKt") 15 | } 16 | 17 | group = "com.example" 18 | version = "0.0.1-SNAPSHOT" 19 | 20 | dependencies { 21 | implementation("org.springframework.boot:spring-boot-starter-actuator") 22 | implementation("org.springframework.boot:spring-boot-starter-web") 23 | implementation("com.fasterxml.jackson.module:jackson-module-kotlin") 24 | implementation("org.jetbrains.kotlin:kotlin-reflect") 25 | testImplementation("org.springframework.boot:spring-boot-starter-test") 26 | } 27 | 28 | val javaVersion = JavaLanguageVersion.of("21") 29 | 30 | java { 31 | toolchain { 32 | languageVersion.set(javaVersion) 33 | } 34 | } 35 | 36 | tasks.withType { 37 | compilerOptions { 38 | freeCompilerArgs.set(listOf("-Xjsr305=strict")) 39 | jvmTarget.set(JvmTarget.fromTarget(javaVersion.toString())) 40 | } 41 | } 42 | 43 | tasks.withType { 44 | useJUnitPlatform() 45 | } 46 | 47 | tasks.withType().configureEach { 48 | isPreserveFileTimestamps = false 49 | isReproducibleFileOrder = true 50 | } 51 | 52 | configurations.all { 53 | resolutionStrategy { 54 | failOnNonReproducibleResolution() 55 | } 56 | } -------------------------------------------------------------------------------- /examples/funky-dependencies/build.gradle.kts: -------------------------------------------------------------------------------- 1 | import org.jetbrains.kotlin.gradle.dsl.JvmTarget 2 | import org.jetbrains.kotlin.gradle.dsl.KotlinVersion 3 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile 4 | 5 | plugins { 6 | kotlin("jvm") version "2.1.0" 7 | application 8 | } 9 | 10 | application { 11 | mainClass.set("MainKt") 12 | } 13 | 14 | group = "org.example" 15 | version = System.getenv("APP_VERSION") ?: "dirty" 16 | 17 | dependencies { 18 | implementation(kotlin("stdlib-jdk8")) 19 | 20 | // Dependency with artifact that does not follow the maven default convention 21 | // https://github.com/raphiz/buildGradleApplication/issues/30 22 | implementation("com.github.ajalt.clikt:clikt:5.0.1") 23 | 24 | // implicitly requires kotlinx-serialization-core-metadata-x.y.z.jar which has multiple gradle variants (and hence URLs) 25 | implementation("org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:1.7.3") 26 | 27 | testImplementation(kotlin("test")) 28 | } 29 | 30 | tasks.test { 31 | useJUnitPlatform() 32 | } 33 | 34 | val javaVersion = JavaLanguageVersion.of("21") 35 | 36 | java { 37 | toolchain { 38 | languageVersion.set(javaVersion) 39 | } 40 | } 41 | 42 | tasks.withType { 43 | compilerOptions { 44 | freeCompilerArgs.set(listOf("-Xjsr305=strict")) 45 | jvmTarget.set(JvmTarget.fromTarget(javaVersion.toString())) 46 | } 47 | } 48 | 49 | 50 | tasks.withType().configureEach { 51 | isPreserveFileTimestamps = false 52 | isReproducibleFileOrder = true 53 | } 54 | 55 | configurations.all { 56 | resolutionStrategy { 57 | failOnNonReproducibleResolution() 58 | } 59 | } -------------------------------------------------------------------------------- /flake.lock: -------------------------------------------------------------------------------- 1 | { 2 | "nodes": { 3 | "flake-parts": { 4 | "inputs": { 5 | "nixpkgs-lib": [ 6 | "nixpkgs" 7 | ] 8 | }, 9 | "locked": { 10 | "lastModified": 1763759067, 11 | "narHash": "sha256-LlLt2Jo/gMNYAwOgdRQBrsRoOz7BPRkzvNaI/fzXi2Q=", 12 | "owner": "hercules-ci", 13 | "repo": "flake-parts", 14 | "rev": "2cccadc7357c0ba201788ae99c4dfa90728ef5e0", 15 | "type": "github" 16 | }, 17 | "original": { 18 | "owner": "hercules-ci", 19 | "repo": "flake-parts", 20 | "type": "github" 21 | } 22 | }, 23 | "nixpkgs": { 24 | "locked": { 25 | "lastModified": 1763678758, 26 | "narHash": "sha256-+hBiJ+kG5IoffUOdlANKFflTT5nO3FrrR2CA3178Y5s=", 27 | "owner": "NixOS", 28 | "repo": "nixpkgs", 29 | "rev": "117cc7f94e8072499b0a7aa4c52084fa4e11cc9b", 30 | "type": "github" 31 | }, 32 | "original": { 33 | "owner": "NixOS", 34 | "ref": "nixos-unstable", 35 | "repo": "nixpkgs", 36 | "type": "github" 37 | } 38 | }, 39 | "root": { 40 | "inputs": { 41 | "flake-parts": "flake-parts", 42 | "nixpkgs": "nixpkgs", 43 | "systems": "systems" 44 | } 45 | }, 46 | "systems": { 47 | "locked": { 48 | "lastModified": 1681028828, 49 | "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", 50 | "owner": "nix-systems", 51 | "repo": "default", 52 | "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", 53 | "type": "github" 54 | }, 55 | "original": { 56 | "owner": "nix-systems", 57 | "repo": "default", 58 | "type": "github" 59 | } 60 | } 61 | }, 62 | "root": "root", 63 | "version": 7 64 | } 65 | -------------------------------------------------------------------------------- /fetchArtefact/builder.bash: -------------------------------------------------------------------------------- 1 | if [ -e .attrs.sh ]; then source .attrs.sh; fi 2 | source $stdenv/setup 3 | 4 | curl=( 5 | curl 6 | --location 7 | --max-redirs 20 8 | --retry 3 9 | --disable-epsv 10 | --cookie-jar cookies 11 | ) 12 | 13 | check_hash() { 14 | local file="$1" 15 | local expected_hash="$2" 16 | echo "~> $0 $1 $2 $3" 17 | # extract algorithm name from SRI-formatted hash 18 | hash_algorithm=$(echo "$expected_hash" | cut -d "-" -f 1) 19 | 20 | actual_hash=$(nix --extra-experimental-features nix-command hash file --type "$hash_algorithm" --sri "$file") 21 | if [ "$actual_hash" == "$expected_hash" ]; then 22 | return 0 23 | else 24 | echo "Hash does not match: expected $expected_hash - actual $actual_hash" 25 | return 1 26 | fi 27 | } 28 | 29 | # expected variables to be set: 30 | name="${name:?}" 31 | out="${out:?}" 32 | urls="${urls:?}" 33 | hash="${hash:?}" 34 | 35 | for url in $urls; do 36 | echo "Downloading $name from $url" 37 | 38 | if "${curl[@]}" --retry 0 --connect-timeout "${NIX_CONNECT_TIMEOUT:-15}" \ 39 | --fail --silent --show-error --head "$url" \ 40 | --write-out "%{http_code}" --output /dev/null > code 2> log; then 41 | 42 | # Continue with download of partially downloaded file 43 | curl_exit_code=18; 44 | while [ $curl_exit_code -eq 18 ]; do 45 | if "${curl[@]}" -C - --fail "$url" --output "$out"; then 46 | break 47 | else 48 | curl_exit_code=$?; 49 | fi 50 | done 51 | 52 | if check_hash "$out" "$hash"; then 53 | echo "File $name successfully downloaded"; 54 | exit 0; 55 | else 56 | rm "$out" 57 | fi 58 | else 59 | echo "error checking the existence of $url:" 60 | cat log 61 | fi 62 | done 63 | 64 | echo "File $name was not found with hash $hash on any of the given urls" 65 | exit 1 66 | -------------------------------------------------------------------------------- /update-verification-metadata/merge-verification-metadata.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | import xml.etree.ElementTree as ET 4 | 5 | main_file = sys.argv[1] 6 | included_dirs = sys.argv[2:] 7 | 8 | namespaces = {'': "https://schema.gradle.org/dependency-verification"} 9 | ET.register_namespace("", namespaces['']) 10 | 11 | def parse_components(path): 12 | if not os.path.isfile(path): 13 | return [] 14 | tree = ET.parse(path) 15 | root = tree.getroot() 16 | components = root.find('components', namespaces) 17 | return components if components is not None else [] 18 | 19 | 20 | # Load main file 21 | tree = ET.parse(main_file) 22 | root = tree.getroot() 23 | main_components = root.find('components', namespaces) 24 | if main_components is None: 25 | main_components = ET.SubElement(root, 'components') 26 | 27 | # Track seen component triples and artifact names 28 | seen_components = {} 29 | def component_key(c): return (c.get('group'), c.get('name'), c.get('version')) 30 | def artifact_key(a): return a.get('name') 31 | 32 | # Index existing components 33 | for component in list(main_components): 34 | key = component_key(component) 35 | seen_components[key] = component 36 | 37 | # Merge each included build's verification-metadata.xml 38 | for build_dir in included_dirs: 39 | included_file = os.path.join(build_dir, main_file) 40 | included_components = parse_components(included_file) 41 | for component in included_components: 42 | key = component_key(component) 43 | if key in seen_components: 44 | existing = seen_components[key] 45 | existing_artifacts = {artifact_key(a): a for a in existing.findall('artifact', namespaces)} 46 | for artifact in component.findall('artifact', namespaces): 47 | a_key = artifact_key(artifact) 48 | if a_key not in existing_artifacts: 49 | existing.append(artifact) 50 | else: 51 | seen_components[key] = component 52 | main_components.append(component) 53 | 54 | # Write back merged result 55 | tree.write(main_file, encoding='UTF-8', xml_declaration=True) 56 | -------------------------------------------------------------------------------- /flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | description = "A Nix builder function for packaging Gradle applications"; 3 | 4 | inputs = { 5 | nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; 6 | 7 | systems.url = "github:nix-systems/default"; 8 | 9 | flake-parts.url = "github:hercules-ci/flake-parts"; 10 | flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs"; 11 | }; 12 | 13 | outputs = inputs @ { 14 | self, 15 | nixpkgs, 16 | flake-parts, 17 | ... 18 | }: 19 | flake-parts.lib.mkFlake {inherit inputs;} { 20 | imports = [ 21 | ./examples 22 | ]; 23 | 24 | flake = { 25 | overlays = { 26 | default = final: prev: { 27 | fetchArtifact = prev.callPackage ./fetchArtefact/default.nix {}; 28 | mkM2Repository = prev.callPackage ./buildGradleApplication/mkM2Repository.nix {}; 29 | buildGradleApplication = prev.callPackage ./buildGradleApplication/default.nix {}; 30 | updateVerificationMetadata = prev.callPackage ./update-verification-metadata/default.nix {}; 31 | gradleFromWrapper = import ./gradleFromWrapper final; 32 | }; 33 | }; 34 | }; 35 | 36 | systems = import inputs.systems; 37 | 38 | perSystem = { 39 | config, 40 | system, 41 | ... 42 | }: let 43 | pkgs = nixpkgs.legacyPackages.${system}; 44 | in { 45 | formatter = pkgs.alejandra; 46 | legacyPackages = let 47 | fetchArtifact = pkgs.callPackage ./fetchArtefact/default.nix {}; 48 | mkM2Repository = pkgs.callPackage ./buildGradleApplication/mkM2Repository.nix { 49 | inherit fetchArtifact; 50 | }; 51 | updateVerificationMetadata = pkgs.callPackage ./update-verification-metadata/default.nix {}; 52 | buildGradleApplication = pkgs.callPackage ./buildGradleApplication/default.nix { 53 | inherit mkM2Repository updateVerificationMetadata; 54 | }; 55 | gradleFromWrapper = import ./gradleFromWrapper pkgs; 56 | in { 57 | inherit fetchArtifact mkM2Repository buildGradleApplication updateVerificationMetadata gradleFromWrapper; 58 | }; 59 | }; 60 | }; 61 | } 62 | -------------------------------------------------------------------------------- /gradleFromWrapper/default.nix: -------------------------------------------------------------------------------- 1 | pkgs: args: let 2 | inherit (pkgs) lib; 3 | safeArgs = 4 | if (lib.isString args || lib.isPath args) 5 | then 6 | lib.warn 7 | "DEPRECATED: gradleFromWrapper now expects an attrset like gradleFromWrapper { wrapperPropertiesPath = ${args}; defaultJava = pkgs.jdk;}." 8 | { 9 | wrapperPropertiesPath = args; 10 | defaultJava = pkgs.jdk; 11 | } 12 | else if builtins.isAttrs args 13 | then args 14 | else throw "Expected an attrset, path (deprecated) or string (deprecated), got: ${builtins.typeOf args}"; 15 | 16 | wrapperProperties = builtins.readFile safeArgs.wrapperPropertiesPath; 17 | lines = pkgs.lib.strings.splitString "\n" wrapperProperties; 18 | 19 | # Extract version from gradle-wrapper.properties 20 | distributionUrlMatches = builtins.filter (line: builtins.match "distributionUrl=(.*)" line != null) lines; 21 | distributionUrlLine = 22 | if distributionUrlMatches == [] 23 | then throw ''Expected to find a line distributionUrl=... in ${safeArgs.wrapperPropertiesPath} but none was found.'' 24 | else builtins.head distributionUrlMatches; 25 | rawDistributionUrl = builtins.head (builtins.match "distributionUrl=(.*)" distributionUrlLine); 26 | distributionUrl = builtins.replaceStrings ["\\:"] [":"] rawDistributionUrl; 27 | version = builtins.head (builtins.match ".*/gradle-([^-]*)-(bin|all).zip" distributionUrl); 28 | 29 | # Extract hash from gradle-wrapper.properties 30 | distributionSha256SumLine = builtins.filter (line: builtins.match "distributionSha256Sum=.*" line != null) lines; 31 | sha256SumLine = 32 | if distributionSha256SumLine == [] 33 | then throw ''Expected to find a line distributionSha256Sum=... in ${safeArgs.wrapperPropertiesPath} but none was found.'' 34 | else builtins.head distributionSha256SumLine; 35 | sha256Hex = builtins.head (builtins.match "distributionSha256Sum=(.*)" sha256SumLine); 36 | hash = "sha256:" + sha256Hex; 37 | in 38 | (pkgs.gradle-packages.mkGradle { 39 | inherit version hash; 40 | inherit (safeArgs) defaultJava; 41 | }).overrideAttrs { 42 | src = pkgs.fetchurl { 43 | url = distributionUrl; 44 | inherit hash; 45 | }; 46 | } 47 | -------------------------------------------------------------------------------- /examples/hello-world/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | @rem SPDX-License-Identifier: Apache-2.0 17 | @rem 18 | 19 | @if "%DEBUG%"=="" @echo off 20 | @rem ########################################################################## 21 | @rem 22 | @rem Gradle startup script for Windows 23 | @rem 24 | @rem ########################################################################## 25 | 26 | @rem Set local scope for the variables with windows NT shell 27 | if "%OS%"=="Windows_NT" setlocal 28 | 29 | set DIRNAME=%~dp0 30 | if "%DIRNAME%"=="" set DIRNAME=. 31 | @rem This is normally unused 32 | set APP_BASE_NAME=%~n0 33 | set APP_HOME=%DIRNAME% 34 | 35 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 36 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 37 | 38 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 39 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 40 | 41 | @rem Find java.exe 42 | if defined JAVA_HOME goto findJavaFromJavaHome 43 | 44 | set JAVA_EXE=java.exe 45 | %JAVA_EXE% -version >NUL 2>&1 46 | if %ERRORLEVEL% equ 0 goto execute 47 | 48 | echo. 1>&2 49 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 50 | echo. 1>&2 51 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 52 | echo location of your Java installation. 1>&2 53 | 54 | goto fail 55 | 56 | :findJavaFromJavaHome 57 | set JAVA_HOME=%JAVA_HOME:"=% 58 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 59 | 60 | if exist "%JAVA_EXE%" goto execute 61 | 62 | echo. 1>&2 63 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 64 | echo. 1>&2 65 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 66 | echo location of your Java installation. 1>&2 67 | 68 | goto fail 69 | 70 | :execute 71 | @rem Setup the command line 72 | 73 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 74 | 75 | 76 | @rem Execute Gradle 77 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 78 | 79 | :end 80 | @rem End local scope for the variables with windows NT shell 81 | if %ERRORLEVEL% equ 0 goto mainEnd 82 | 83 | :fail 84 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 85 | rem the _cmd.exe /c_ return code! 86 | set EXIT_CODE=%ERRORLEVEL% 87 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 88 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 89 | exit /b %EXIT_CODE% 90 | 91 | :mainEnd 92 | if "%OS%"=="Windows_NT" endlocal 93 | 94 | :omega 95 | -------------------------------------------------------------------------------- /examples/spring-boot/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | @rem SPDX-License-Identifier: Apache-2.0 17 | @rem 18 | 19 | @if "%DEBUG%"=="" @echo off 20 | @rem ########################################################################## 21 | @rem 22 | @rem Gradle startup script for Windows 23 | @rem 24 | @rem ########################################################################## 25 | 26 | @rem Set local scope for the variables with windows NT shell 27 | if "%OS%"=="Windows_NT" setlocal 28 | 29 | set DIRNAME=%~dp0 30 | if "%DIRNAME%"=="" set DIRNAME=. 31 | @rem This is normally unused 32 | set APP_BASE_NAME=%~n0 33 | set APP_HOME=%DIRNAME% 34 | 35 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 36 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 37 | 38 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 39 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 40 | 41 | @rem Find java.exe 42 | if defined JAVA_HOME goto findJavaFromJavaHome 43 | 44 | set JAVA_EXE=java.exe 45 | %JAVA_EXE% -version >NUL 2>&1 46 | if %ERRORLEVEL% equ 0 goto execute 47 | 48 | echo. 1>&2 49 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 50 | echo. 1>&2 51 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 52 | echo location of your Java installation. 1>&2 53 | 54 | goto fail 55 | 56 | :findJavaFromJavaHome 57 | set JAVA_HOME=%JAVA_HOME:"=% 58 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 59 | 60 | if exist "%JAVA_EXE%" goto execute 61 | 62 | echo. 1>&2 63 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 64 | echo. 1>&2 65 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 66 | echo location of your Java installation. 1>&2 67 | 68 | goto fail 69 | 70 | :execute 71 | @rem Setup the command line 72 | 73 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 74 | 75 | 76 | @rem Execute Gradle 77 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 78 | 79 | :end 80 | @rem End local scope for the variables with windows NT shell 81 | if %ERRORLEVEL% equ 0 goto mainEnd 82 | 83 | :fail 84 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 85 | rem the _cmd.exe /c_ return code! 86 | set EXIT_CODE=%ERRORLEVEL% 87 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 88 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 89 | exit /b %EXIT_CODE% 90 | 91 | :mainEnd 92 | if "%OS%"=="Windows_NT" endlocal 93 | 94 | :omega 95 | -------------------------------------------------------------------------------- /examples/funky-dependencies/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | @rem SPDX-License-Identifier: Apache-2.0 17 | @rem 18 | 19 | @if "%DEBUG%"=="" @echo off 20 | @rem ########################################################################## 21 | @rem 22 | @rem Gradle startup script for Windows 23 | @rem 24 | @rem ########################################################################## 25 | 26 | @rem Set local scope for the variables with windows NT shell 27 | if "%OS%"=="Windows_NT" setlocal 28 | 29 | set DIRNAME=%~dp0 30 | if "%DIRNAME%"=="" set DIRNAME=. 31 | @rem This is normally unused 32 | set APP_BASE_NAME=%~n0 33 | set APP_HOME=%DIRNAME% 34 | 35 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 36 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 37 | 38 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 39 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 40 | 41 | @rem Find java.exe 42 | if defined JAVA_HOME goto findJavaFromJavaHome 43 | 44 | set JAVA_EXE=java.exe 45 | %JAVA_EXE% -version >NUL 2>&1 46 | if %ERRORLEVEL% equ 0 goto execute 47 | 48 | echo. 1>&2 49 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 50 | echo. 1>&2 51 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 52 | echo location of your Java installation. 1>&2 53 | 54 | goto fail 55 | 56 | :findJavaFromJavaHome 57 | set JAVA_HOME=%JAVA_HOME:"=% 58 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 59 | 60 | if exist "%JAVA_EXE%" goto execute 61 | 62 | echo. 1>&2 63 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 64 | echo. 1>&2 65 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 66 | echo location of your Java installation. 1>&2 67 | 68 | goto fail 69 | 70 | :execute 71 | @rem Setup the command line 72 | 73 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 74 | 75 | 76 | @rem Execute Gradle 77 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 78 | 79 | :end 80 | @rem End local scope for the variables with windows NT shell 81 | if %ERRORLEVEL% equ 0 goto mainEnd 82 | 83 | :fail 84 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 85 | rem the _cmd.exe /c_ return code! 86 | set EXIT_CODE=%ERRORLEVEL% 87 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 88 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 89 | exit /b %EXIT_CODE% 90 | 91 | :mainEnd 92 | if "%OS%"=="Windows_NT" endlocal 93 | 94 | :omega 95 | -------------------------------------------------------------------------------- /examples/hello-world/gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | # SPDX-License-Identifier: Apache-2.0 19 | # 20 | 21 | ############################################################################## 22 | # 23 | # Gradle start up script for POSIX generated by Gradle. 24 | # 25 | # Important for running: 26 | # 27 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 28 | # noncompliant, but you have some other compliant shell such as ksh or 29 | # bash, then to run this script, type that shell name before the whole 30 | # command line, like: 31 | # 32 | # ksh Gradle 33 | # 34 | # Busybox and similar reduced shells will NOT work, because this script 35 | # requires all of these POSIX shell features: 36 | # * functions; 37 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 38 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 39 | # * compound commands having a testable exit status, especially «case»; 40 | # * various built-in commands including «command», «set», and «ulimit». 41 | # 42 | # Important for patching: 43 | # 44 | # (2) This script targets any POSIX shell, so it avoids extensions provided 45 | # by Bash, Ksh, etc; in particular arrays are avoided. 46 | # 47 | # The "traditional" practice of packing multiple parameters into a 48 | # space-separated string is a well documented source of bugs and security 49 | # problems, so this is (mostly) avoided, by progressively accumulating 50 | # options in "$@", and eventually passing that to Java. 51 | # 52 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 53 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 54 | # see the in-line comments for details. 55 | # 56 | # There are tweaks for specific operating systems such as AIX, CygWin, 57 | # Darwin, MinGW, and NonStop. 58 | # 59 | # (3) This script is generated from the Groovy template 60 | # https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 61 | # within the Gradle project. 62 | # 63 | # You can find Gradle at https://github.com/gradle/gradle/. 64 | # 65 | ############################################################################## 66 | 67 | # Attempt to set APP_HOME 68 | 69 | # Resolve links: $0 may be a link 70 | app_path=$0 71 | 72 | # Need this for daisy-chained symlinks. 73 | while 74 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 75 | [ -h "$app_path" ] 76 | do 77 | ls=$( ls -ld "$app_path" ) 78 | link=${ls#*' -> '} 79 | case $link in #( 80 | /*) app_path=$link ;; #( 81 | *) app_path=$APP_HOME$link ;; 82 | esac 83 | done 84 | 85 | # This is normally unused 86 | # shellcheck disable=SC2034 87 | APP_BASE_NAME=${0##*/} 88 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) 89 | APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s 90 | ' "$PWD" ) || exit 91 | 92 | # Use the maximum available, or set MAX_FD != -1 to use that value. 93 | MAX_FD=maximum 94 | 95 | warn () { 96 | echo "$*" 97 | } >&2 98 | 99 | die () { 100 | echo 101 | echo "$*" 102 | echo 103 | exit 1 104 | } >&2 105 | 106 | # OS specific support (must be 'true' or 'false'). 107 | cygwin=false 108 | msys=false 109 | darwin=false 110 | nonstop=false 111 | case "$( uname )" in #( 112 | CYGWIN* ) cygwin=true ;; #( 113 | Darwin* ) darwin=true ;; #( 114 | MSYS* | MINGW* ) msys=true ;; #( 115 | NONSTOP* ) nonstop=true ;; 116 | esac 117 | 118 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 119 | 120 | 121 | # Determine the Java command to use to start the JVM. 122 | if [ -n "$JAVA_HOME" ] ; then 123 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 124 | # IBM's JDK on AIX uses strange locations for the executables 125 | JAVACMD=$JAVA_HOME/jre/sh/java 126 | else 127 | JAVACMD=$JAVA_HOME/bin/java 128 | fi 129 | if [ ! -x "$JAVACMD" ] ; then 130 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 131 | 132 | Please set the JAVA_HOME variable in your environment to match the 133 | location of your Java installation." 134 | fi 135 | else 136 | JAVACMD=java 137 | if ! command -v java >/dev/null 2>&1 138 | then 139 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 140 | 141 | Please set the JAVA_HOME variable in your environment to match the 142 | location of your Java installation." 143 | fi 144 | fi 145 | 146 | # Increase the maximum file descriptors if we can. 147 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 148 | case $MAX_FD in #( 149 | max*) 150 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 151 | # shellcheck disable=SC2039,SC3045 152 | MAX_FD=$( ulimit -H -n ) || 153 | warn "Could not query maximum file descriptor limit" 154 | esac 155 | case $MAX_FD in #( 156 | '' | soft) :;; #( 157 | *) 158 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 159 | # shellcheck disable=SC2039,SC3045 160 | ulimit -n "$MAX_FD" || 161 | warn "Could not set maximum file descriptor limit to $MAX_FD" 162 | esac 163 | fi 164 | 165 | # Collect all arguments for the java command, stacking in reverse order: 166 | # * args from the command line 167 | # * the main class name 168 | # * -classpath 169 | # * -D...appname settings 170 | # * --module-path (only if needed) 171 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 172 | 173 | # For Cygwin or MSYS, switch paths to Windows format before running java 174 | if "$cygwin" || "$msys" ; then 175 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 176 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 177 | 178 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 179 | 180 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 181 | for arg do 182 | if 183 | case $arg in #( 184 | -*) false ;; # don't mess with options #( 185 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 186 | [ -e "$t" ] ;; #( 187 | *) false ;; 188 | esac 189 | then 190 | arg=$( cygpath --path --ignore --mixed "$arg" ) 191 | fi 192 | # Roll the args list around exactly as many times as the number of 193 | # args, so each arg winds up back in the position where it started, but 194 | # possibly modified. 195 | # 196 | # NB: a `for` loop captures its iteration list before it begins, so 197 | # changing the positional parameters here affects neither the number of 198 | # iterations, nor the values presented in `arg`. 199 | shift # remove old arg 200 | set -- "$@" "$arg" # push replacement arg 201 | done 202 | fi 203 | 204 | 205 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 206 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 207 | 208 | # Collect all arguments for the java command: 209 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, 210 | # and any embedded shellness will be escaped. 211 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be 212 | # treated as '${Hostname}' itself on the command line. 213 | 214 | set -- \ 215 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 216 | -classpath "$CLASSPATH" \ 217 | org.gradle.wrapper.GradleWrapperMain \ 218 | "$@" 219 | 220 | # Stop when "xargs" is not available. 221 | if ! command -v xargs >/dev/null 2>&1 222 | then 223 | die "xargs is not available" 224 | fi 225 | 226 | # Use "xargs" to parse quoted args. 227 | # 228 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 229 | # 230 | # In Bash we could simply go: 231 | # 232 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 233 | # set -- "${ARGS[@]}" "$@" 234 | # 235 | # but POSIX shell has neither arrays nor command substitution, so instead we 236 | # post-process each arg (as a line of input to sed) to backslash-escape any 237 | # character that might be a shell metacharacter, then use eval to reverse 238 | # that process (while maintaining the separation between arguments), and wrap 239 | # the whole thing up as a single "set" statement. 240 | # 241 | # This will of course break if any of these variables contains a newline or 242 | # an unmatched quote. 243 | # 244 | 245 | eval "set -- $( 246 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 247 | xargs -n1 | 248 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 249 | tr '\n' ' ' 250 | )" '"$@"' 251 | 252 | exec "$JAVACMD" "$@" 253 | -------------------------------------------------------------------------------- /examples/spring-boot/gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | # SPDX-License-Identifier: Apache-2.0 19 | # 20 | 21 | ############################################################################## 22 | # 23 | # Gradle start up script for POSIX generated by Gradle. 24 | # 25 | # Important for running: 26 | # 27 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 28 | # noncompliant, but you have some other compliant shell such as ksh or 29 | # bash, then to run this script, type that shell name before the whole 30 | # command line, like: 31 | # 32 | # ksh Gradle 33 | # 34 | # Busybox and similar reduced shells will NOT work, because this script 35 | # requires all of these POSIX shell features: 36 | # * functions; 37 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 38 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 39 | # * compound commands having a testable exit status, especially «case»; 40 | # * various built-in commands including «command», «set», and «ulimit». 41 | # 42 | # Important for patching: 43 | # 44 | # (2) This script targets any POSIX shell, so it avoids extensions provided 45 | # by Bash, Ksh, etc; in particular arrays are avoided. 46 | # 47 | # The "traditional" practice of packing multiple parameters into a 48 | # space-separated string is a well documented source of bugs and security 49 | # problems, so this is (mostly) avoided, by progressively accumulating 50 | # options in "$@", and eventually passing that to Java. 51 | # 52 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 53 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 54 | # see the in-line comments for details. 55 | # 56 | # There are tweaks for specific operating systems such as AIX, CygWin, 57 | # Darwin, MinGW, and NonStop. 58 | # 59 | # (3) This script is generated from the Groovy template 60 | # https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 61 | # within the Gradle project. 62 | # 63 | # You can find Gradle at https://github.com/gradle/gradle/. 64 | # 65 | ############################################################################## 66 | 67 | # Attempt to set APP_HOME 68 | 69 | # Resolve links: $0 may be a link 70 | app_path=$0 71 | 72 | # Need this for daisy-chained symlinks. 73 | while 74 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 75 | [ -h "$app_path" ] 76 | do 77 | ls=$( ls -ld "$app_path" ) 78 | link=${ls#*' -> '} 79 | case $link in #( 80 | /*) app_path=$link ;; #( 81 | *) app_path=$APP_HOME$link ;; 82 | esac 83 | done 84 | 85 | # This is normally unused 86 | # shellcheck disable=SC2034 87 | APP_BASE_NAME=${0##*/} 88 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) 89 | APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s 90 | ' "$PWD" ) || exit 91 | 92 | # Use the maximum available, or set MAX_FD != -1 to use that value. 93 | MAX_FD=maximum 94 | 95 | warn () { 96 | echo "$*" 97 | } >&2 98 | 99 | die () { 100 | echo 101 | echo "$*" 102 | echo 103 | exit 1 104 | } >&2 105 | 106 | # OS specific support (must be 'true' or 'false'). 107 | cygwin=false 108 | msys=false 109 | darwin=false 110 | nonstop=false 111 | case "$( uname )" in #( 112 | CYGWIN* ) cygwin=true ;; #( 113 | Darwin* ) darwin=true ;; #( 114 | MSYS* | MINGW* ) msys=true ;; #( 115 | NONSTOP* ) nonstop=true ;; 116 | esac 117 | 118 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 119 | 120 | 121 | # Determine the Java command to use to start the JVM. 122 | if [ -n "$JAVA_HOME" ] ; then 123 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 124 | # IBM's JDK on AIX uses strange locations for the executables 125 | JAVACMD=$JAVA_HOME/jre/sh/java 126 | else 127 | JAVACMD=$JAVA_HOME/bin/java 128 | fi 129 | if [ ! -x "$JAVACMD" ] ; then 130 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 131 | 132 | Please set the JAVA_HOME variable in your environment to match the 133 | location of your Java installation." 134 | fi 135 | else 136 | JAVACMD=java 137 | if ! command -v java >/dev/null 2>&1 138 | then 139 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 140 | 141 | Please set the JAVA_HOME variable in your environment to match the 142 | location of your Java installation." 143 | fi 144 | fi 145 | 146 | # Increase the maximum file descriptors if we can. 147 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 148 | case $MAX_FD in #( 149 | max*) 150 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 151 | # shellcheck disable=SC2039,SC3045 152 | MAX_FD=$( ulimit -H -n ) || 153 | warn "Could not query maximum file descriptor limit" 154 | esac 155 | case $MAX_FD in #( 156 | '' | soft) :;; #( 157 | *) 158 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 159 | # shellcheck disable=SC2039,SC3045 160 | ulimit -n "$MAX_FD" || 161 | warn "Could not set maximum file descriptor limit to $MAX_FD" 162 | esac 163 | fi 164 | 165 | # Collect all arguments for the java command, stacking in reverse order: 166 | # * args from the command line 167 | # * the main class name 168 | # * -classpath 169 | # * -D...appname settings 170 | # * --module-path (only if needed) 171 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 172 | 173 | # For Cygwin or MSYS, switch paths to Windows format before running java 174 | if "$cygwin" || "$msys" ; then 175 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 176 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 177 | 178 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 179 | 180 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 181 | for arg do 182 | if 183 | case $arg in #( 184 | -*) false ;; # don't mess with options #( 185 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 186 | [ -e "$t" ] ;; #( 187 | *) false ;; 188 | esac 189 | then 190 | arg=$( cygpath --path --ignore --mixed "$arg" ) 191 | fi 192 | # Roll the args list around exactly as many times as the number of 193 | # args, so each arg winds up back in the position where it started, but 194 | # possibly modified. 195 | # 196 | # NB: a `for` loop captures its iteration list before it begins, so 197 | # changing the positional parameters here affects neither the number of 198 | # iterations, nor the values presented in `arg`. 199 | shift # remove old arg 200 | set -- "$@" "$arg" # push replacement arg 201 | done 202 | fi 203 | 204 | 205 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 206 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 207 | 208 | # Collect all arguments for the java command: 209 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, 210 | # and any embedded shellness will be escaped. 211 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be 212 | # treated as '${Hostname}' itself on the command line. 213 | 214 | set -- \ 215 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 216 | -classpath "$CLASSPATH" \ 217 | org.gradle.wrapper.GradleWrapperMain \ 218 | "$@" 219 | 220 | # Stop when "xargs" is not available. 221 | if ! command -v xargs >/dev/null 2>&1 222 | then 223 | die "xargs is not available" 224 | fi 225 | 226 | # Use "xargs" to parse quoted args. 227 | # 228 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 229 | # 230 | # In Bash we could simply go: 231 | # 232 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 233 | # set -- "${ARGS[@]}" "$@" 234 | # 235 | # but POSIX shell has neither arrays nor command substitution, so instead we 236 | # post-process each arg (as a line of input to sed) to backslash-escape any 237 | # character that might be a shell metacharacter, then use eval to reverse 238 | # that process (while maintaining the separation between arguments), and wrap 239 | # the whole thing up as a single "set" statement. 240 | # 241 | # This will of course break if any of these variables contains a newline or 242 | # an unmatched quote. 243 | # 244 | 245 | eval "set -- $( 246 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 247 | xargs -n1 | 248 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 249 | tr '\n' ' ' 250 | )" '"$@"' 251 | 252 | exec "$JAVACMD" "$@" 253 | -------------------------------------------------------------------------------- /examples/funky-dependencies/gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | # SPDX-License-Identifier: Apache-2.0 19 | # 20 | 21 | ############################################################################## 22 | # 23 | # Gradle start up script for POSIX generated by Gradle. 24 | # 25 | # Important for running: 26 | # 27 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 28 | # noncompliant, but you have some other compliant shell such as ksh or 29 | # bash, then to run this script, type that shell name before the whole 30 | # command line, like: 31 | # 32 | # ksh Gradle 33 | # 34 | # Busybox and similar reduced shells will NOT work, because this script 35 | # requires all of these POSIX shell features: 36 | # * functions; 37 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 38 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 39 | # * compound commands having a testable exit status, especially «case»; 40 | # * various built-in commands including «command», «set», and «ulimit». 41 | # 42 | # Important for patching: 43 | # 44 | # (2) This script targets any POSIX shell, so it avoids extensions provided 45 | # by Bash, Ksh, etc; in particular arrays are avoided. 46 | # 47 | # The "traditional" practice of packing multiple parameters into a 48 | # space-separated string is a well documented source of bugs and security 49 | # problems, so this is (mostly) avoided, by progressively accumulating 50 | # options in "$@", and eventually passing that to Java. 51 | # 52 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 53 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 54 | # see the in-line comments for details. 55 | # 56 | # There are tweaks for specific operating systems such as AIX, CygWin, 57 | # Darwin, MinGW, and NonStop. 58 | # 59 | # (3) This script is generated from the Groovy template 60 | # https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 61 | # within the Gradle project. 62 | # 63 | # You can find Gradle at https://github.com/gradle/gradle/. 64 | # 65 | ############################################################################## 66 | 67 | # Attempt to set APP_HOME 68 | 69 | # Resolve links: $0 may be a link 70 | app_path=$0 71 | 72 | # Need this for daisy-chained symlinks. 73 | while 74 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 75 | [ -h "$app_path" ] 76 | do 77 | ls=$( ls -ld "$app_path" ) 78 | link=${ls#*' -> '} 79 | case $link in #( 80 | /*) app_path=$link ;; #( 81 | *) app_path=$APP_HOME$link ;; 82 | esac 83 | done 84 | 85 | # This is normally unused 86 | # shellcheck disable=SC2034 87 | APP_BASE_NAME=${0##*/} 88 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) 89 | APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s 90 | ' "$PWD" ) || exit 91 | 92 | # Use the maximum available, or set MAX_FD != -1 to use that value. 93 | MAX_FD=maximum 94 | 95 | warn () { 96 | echo "$*" 97 | } >&2 98 | 99 | die () { 100 | echo 101 | echo "$*" 102 | echo 103 | exit 1 104 | } >&2 105 | 106 | # OS specific support (must be 'true' or 'false'). 107 | cygwin=false 108 | msys=false 109 | darwin=false 110 | nonstop=false 111 | case "$( uname )" in #( 112 | CYGWIN* ) cygwin=true ;; #( 113 | Darwin* ) darwin=true ;; #( 114 | MSYS* | MINGW* ) msys=true ;; #( 115 | NONSTOP* ) nonstop=true ;; 116 | esac 117 | 118 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 119 | 120 | 121 | # Determine the Java command to use to start the JVM. 122 | if [ -n "$JAVA_HOME" ] ; then 123 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 124 | # IBM's JDK on AIX uses strange locations for the executables 125 | JAVACMD=$JAVA_HOME/jre/sh/java 126 | else 127 | JAVACMD=$JAVA_HOME/bin/java 128 | fi 129 | if [ ! -x "$JAVACMD" ] ; then 130 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 131 | 132 | Please set the JAVA_HOME variable in your environment to match the 133 | location of your Java installation." 134 | fi 135 | else 136 | JAVACMD=java 137 | if ! command -v java >/dev/null 2>&1 138 | then 139 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 140 | 141 | Please set the JAVA_HOME variable in your environment to match the 142 | location of your Java installation." 143 | fi 144 | fi 145 | 146 | # Increase the maximum file descriptors if we can. 147 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 148 | case $MAX_FD in #( 149 | max*) 150 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 151 | # shellcheck disable=SC2039,SC3045 152 | MAX_FD=$( ulimit -H -n ) || 153 | warn "Could not query maximum file descriptor limit" 154 | esac 155 | case $MAX_FD in #( 156 | '' | soft) :;; #( 157 | *) 158 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 159 | # shellcheck disable=SC2039,SC3045 160 | ulimit -n "$MAX_FD" || 161 | warn "Could not set maximum file descriptor limit to $MAX_FD" 162 | esac 163 | fi 164 | 165 | # Collect all arguments for the java command, stacking in reverse order: 166 | # * args from the command line 167 | # * the main class name 168 | # * -classpath 169 | # * -D...appname settings 170 | # * --module-path (only if needed) 171 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 172 | 173 | # For Cygwin or MSYS, switch paths to Windows format before running java 174 | if "$cygwin" || "$msys" ; then 175 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 176 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 177 | 178 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 179 | 180 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 181 | for arg do 182 | if 183 | case $arg in #( 184 | -*) false ;; # don't mess with options #( 185 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 186 | [ -e "$t" ] ;; #( 187 | *) false ;; 188 | esac 189 | then 190 | arg=$( cygpath --path --ignore --mixed "$arg" ) 191 | fi 192 | # Roll the args list around exactly as many times as the number of 193 | # args, so each arg winds up back in the position where it started, but 194 | # possibly modified. 195 | # 196 | # NB: a `for` loop captures its iteration list before it begins, so 197 | # changing the positional parameters here affects neither the number of 198 | # iterations, nor the values presented in `arg`. 199 | shift # remove old arg 200 | set -- "$@" "$arg" # push replacement arg 201 | done 202 | fi 203 | 204 | 205 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 206 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 207 | 208 | # Collect all arguments for the java command: 209 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, 210 | # and any embedded shellness will be escaped. 211 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be 212 | # treated as '${Hostname}' itself on the command line. 213 | 214 | set -- \ 215 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 216 | -classpath "$CLASSPATH" \ 217 | org.gradle.wrapper.GradleWrapperMain \ 218 | "$@" 219 | 220 | # Stop when "xargs" is not available. 221 | if ! command -v xargs >/dev/null 2>&1 222 | then 223 | die "xargs is not available" 224 | fi 225 | 226 | # Use "xargs" to parse quoted args. 227 | # 228 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 229 | # 230 | # In Bash we could simply go: 231 | # 232 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 233 | # set -- "${ARGS[@]}" "$@" 234 | # 235 | # but POSIX shell has neither arrays nor command substitution, so instead we 236 | # post-process each arg (as a line of input to sed) to backslash-escape any 237 | # character that might be a shell metacharacter, then use eval to reverse 238 | # that process (while maintaining the separation between arguments), and wrap 239 | # the whole thing up as a single "set" statement. 240 | # 241 | # This will of course break if any of these variables contains a newline or 242 | # an unmatched quote. 243 | # 244 | 245 | eval "set -- $( 246 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 247 | xargs -n1 | 248 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 249 | tr '\n' ' ' 250 | )" '"$@"' 251 | 252 | exec "$JAVACMD" "$@" 253 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # `buildGradleApplication` 2 | 3 | [![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) 4 | 5 | **`buildGradleApplication`** is a [Nix](https://nixos.org/nix/) builder function for packaging [Gradle](https://gradle.org/) applications. 6 | 7 | `buildGradleApplication` drastically simplifies the integration of Gradle projects in the Nix ecosystem by defining a set of rules/constraints that a Gradle project must follow. 8 | 9 | ## Goals 10 | 11 | - For now, the focus is on packaging Gradle applications, not libraries. 12 | - Using the builder function should feel idiomatic to Nix. It should provide the same experience as `buildPythonPackage` or `buildPerlPackage` but with fewer options. 13 | - The rules imposed on the Gradle build should be idiomatic to Gradle and ideally promote Gradle best practices. 14 | - Support automatic updates with tools such as renovate. 15 | - All dependencies (jars) should be packaged into discrete derivations (and linked in the final result) to facilitate efficient deployments and [layered OIC images](https://ryantm.github.io/nixpkgs/builders/images/dockertools/#ssec-pkgs-dockerTools-buildLayeredImage). 16 | - This project should be small and simple. 17 | 18 | ## Non-Goals 19 | 20 | - `buildGradleApplication` is _not_ a general purpose solution for building arbitrary Gradle projects. If you want to do that, check out [gradle2nix](https://github.com/tadfisher/gradle2nix) instead. 21 | - Do not try to replicate Gradle's behaviour, e.g. to construct a runtime classpath. Instead, use the Gradle built-ins to produce these results. 22 | - Android. But if you have experience in Android, talk to me! It might not be that hard to support android instead (by breaking/adopting Rule #5). 23 | 24 | ## Installation (via flakes) 25 | 26 | ```nix 27 | { 28 | inputs = { 29 | build-gradle-application.url = "github:raphiz/buildGradleApplication"; 30 | # ... 31 | }; 32 | 33 | # ... 34 | outputs = { 35 | nixpkgs, 36 | build-gradle-application, 37 | ... 38 | }: { 39 | # ... 40 | pkgs = import nixpkgs { 41 | inherit system; 42 | overlays = [build-gradle-application.overlays.default]; 43 | }; 44 | # ... 45 | }; 46 | } 47 | 48 | ``` 49 | 50 | ## Usage 51 | 52 | The usage of `buildGradleApplication` should be straight forward once your build follows the outlined rules below. Here is a very minimal example: 53 | 54 | ```nix 55 | # package.nix 56 | { 57 | lib, 58 | version, 59 | buildGradleApplication, 60 | }: 61 | buildGradleApplication { 62 | pname = "hello-world"; 63 | version = version; 64 | src = ./.; 65 | meta = with lib; { 66 | description = "Hello World Application"; 67 | }; 68 | } 69 | 70 | ``` 71 | 72 | For further examples, checkout the [examples directory](./examples) 73 | 74 | All available parameters of `buildGradleApplication` are documented in the [source code](https://github.com/raphiz/buildGradleApplication/blob/main/buildGradleApplication/default.nix) 75 | 76 | ## Rules 77 | 78 | ### Rule #1: Requires Checksum Verification (`verification-metadata.xml`) 79 | 80 | Using Gradle's built-in Mechanism for [dependency verification](https://docs.gradle.org/current/userguide/dependency_verification.html) is not only a security best practice, but also allows `buildGradleApplication` to fetch an fixed version (as a [fixed-output derivations](https://nixos.org/manual/nix/stable/language/advanced-attributes.html#adv-attr-outputHash)) of a dependency and its metadata. 81 | 82 | While it should be straight forward to generate a `verification-metadata.xml` file by following the [documentation](https://docs.gradle.org/current/userguide/dependency_verification.html), take extra care that Gradle version and JDK version align! This should not be a problem when using Nix for your development environment. 83 | 84 | Here is an example command to let Gradle add all dependency artifacts to your `verification-metadata.xml`: 85 | 86 | ```bash 87 | gradle --refresh-dependencies --write-verification-metadata sha256 --write-locks dependencies 88 | ``` 89 | 90 | Gradle does not remove any artefacts from the `verification-metadata.xml` even if they are not used anymore. This can lead to a unnecessary large file. The `updateVerificationMetadata` package from this flake can be used to re-generate the file while keeping the `` section. Again: You must ensure that the Gradle version and JDK version align. 91 | 92 | ```bash 93 | update-verification-metadata 94 | ``` 95 | 96 | **Tip**: [Renovate can and will append updated dependencies to this file](https://docs.renovatebot.com/modules/manager/gradle/#dependency-verification) - Yay 🎉 97 | 98 | #### Dependency Verification and IntelliJ IDEA 99 | 100 | Gradle's `verification-metadata.xml` file enforces that only explicitly listed artifacts are downloaded during builds. However, this can lead to issues when using IDEs like IntelliJ IDEA, [which will download additional artifacts (Javadoc, source files and more) that are not included in the verification metadata](https://youtrack.jetbrains.com/issue/IDEA-258328). 101 | 102 | To handle this issue, you have two options. The one you choose depends on how important dependency verification is to you compared to the effort required to maintain it: 103 | 104 | #### Option 1: Disable Dependency Verification for Development (low effort) 105 | 106 | Simplify the development process by disabling Gradle's dependency verification. Add the following line to your `gradle.properties` file: 107 | 108 | ```properties 109 | org.gradle.dependency.verification=off 110 | ``` 111 | 112 | **Note**: You still need the `verification-metadata.xml` file to download the required artifacts and build the Nix package. However, disabling dependency verification prevents you from having to deal with these quirks during development. 113 | 114 | #### 2. Manually Add Missing Dependencies (more secure when done properly) 115 | 116 | Manually identify and add additional dependencies required by IntelliJ IDEA into the verification-metadata.xml file. I use a [script to simplify that](https://gist.github.com/raphiz/3e03f54cf2b81047e8cdcdd264b56010). 117 | 118 | ##### 2b. Automatically Trust Javadoc and Source Artifacts 119 | 120 | Update your `verification-metadata.xml` file to automatically trust Javadoc and source files, allowing IntelliJ IDEA to fetch them without verification errors. Here's an example configuration: 121 | 122 | ```xml 123 | 124 | 125 | 126 | 127 | true 128 | false 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | ``` 142 | 143 | ### Rule #2: Maven Repositories Only 144 | 145 | `buildGradleApplication` only supports Maven repositories to fetch dependencies. Ivy is not supported. 146 | 147 | ### Rule #3: No Downloads 148 | 149 | Nix uses a sandbox which prevents internet access during build time (for a good reason). All other (implicit) build dependencies must be provided via Nix instead. `buildGradleApplication` takes care of downloading and providing the Maven dependencies. Everything else is specific to your build and must be handled by you. 150 | 151 | Let's take the [`gradle-node` plugin](https://github.com/node-gradle/gradle-node-plugin/blob/master/docs/usage.md) as an example. It can be configured to download and install a specific version of Node.js. This will fail for the reason given above. Instead, provide Node.js as `nativeBuildInput` instead: 152 | 153 | ```nix 154 | buildGradleApplication { 155 | # ... 156 | nativeBuildInputs = [pkgs.nodejs]; 157 | } 158 | ``` 159 | 160 | ### Rule #4: Centralized Repository 161 | 162 | Because Nix uses a sandbox which prevents internet access during build time, `buildGradleApplication` needs to pre fetch all required artifacts. These are then made available to the offline build using a local maven repository. The location of this repository depends on [it's contents](https://nixos.org/guides/nix-pills/nix-store-paths) and is provided to the Gradle build via the `MAVEN_SOURCE_REPOSITORY` Environment Variable. 163 | 164 | It's a Gradle best practice to [centralize repositories declarations](https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:centralized-repository-declaration). 165 | 166 | `buildGradleApplication` assumes that all repository declarations are located in your `settings.gradle(.kts)` files. It will then replace these declarations during build time with the location of the offline repository (using a [Gradle init script](./buildGradleApplication/init.gradle.kts)) 167 | 168 | Here is an example of how your Gradle build should declare it's repositories: 169 | 170 | ```kotlin 171 | // settings.gradle.kts 172 | pluginManagement { 173 | repositories { 174 | gradlePluginPortal() 175 | } 176 | } 177 | 178 | dependencyResolutionManagement { 179 | repositories { 180 | mavenCentral() 181 | } 182 | 183 | // Highly recommended, see https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:centralized-repository-declaration 184 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 185 | } 186 | ``` 187 | 188 | Note that repository declarations must be defined for each [included build](https://docs.gradle.org/current/userguide/composite_builds.html) as well. 189 | 190 | Also Note that `buildGradleApplication` is (currently) unable to extract the declared repositories from your Gradle build. If you use different or additional repositories, you must provide it to `buildGradleApplication` using the `repositories` parameter: 191 | 192 | ```nix 193 | buildGradleApplication { 194 | # ... 195 | repositories = ["https://plugins.gradle.org/m2/" "https://repo1.maven.org/maven2/" "https://example.com/maven2/"]; 196 | } 197 | ``` 198 | 199 | ### Rule #5: Using the `application` Plugin 200 | 201 | Currently, the focus of this tool is to package Gradle applications. In order to launch a java application, we need both an main class as an entry point and a runtime classpath. The latter must contain both third-party dependencies fetched from a maven repository and project local libraries generated from other projects within the same build. 202 | 203 | Gradle provides exactly that (a so called [Distribution](https://docs.gradle.org/current/userguide/distribution_plugin.html#distribution_plugin)) with the built in [`application` plugin](https://docs.gradle.org/current/userguide/application_plugin.html). The required configuration is quite reasonable: 204 | 205 | ```kotlin 206 | plugins { 207 | application 208 | // ... 209 | } 210 | 211 | application { 212 | mainClass.set("org.gradle.sample.Main") 213 | } 214 | // ... 215 | 216 | ``` 217 | 218 | Checkout the [`application` plugin documentation](https://docs.gradle.org/current/userguide/application_plugin.html) for any further details. 219 | 220 | ### Rule #6: Tell gradle to be more reproducible (Gradle Versions 8 and lower) 221 | 222 | Gradle has [a few switches to make builds more reproducible](https://docs.gradle.org/current/userguide/working_with_files.html#sec:reproducible_archives). These must be set in Gradle Versions < 9 to ensure proper reproducibility of the genereated `.jar` files. 223 | [Gradle 9 and onwards produces reproducible archives by default](https://gradle.org/whats-new/gradle-9/#reproducible-archives-by-default) 224 | 225 | ```kotlin 226 | // Important: This configuration is probably not complete for your project! 227 | tasks.withType().configureEach { 228 | isPreserveFileTimestamps = false 229 | isReproducibleFileOrder = true 230 | dirPermissions { unix("755") } 231 | filePermissions { unix("644") } 232 | } 233 | ``` 234 | 235 | Alternatively, you might use [the Reproducible Builds plugin](https://github.com/gradlex-org/reproducible-builds/) to achive the same. 236 | 237 | ### Rule #7: Making sure dependency resolution is reproducible 238 | 239 | Gradle's dependency resolution _can_ be unstable in the following cases: 240 | 241 | - [dynamic dependency versions](https://docs.gradle.org/current/userguide/rich_versions.html) are used (version ranges, latest.release, 1.+, ...) 242 | - Changing versions (SNAPSHOTs, fixed version with changing contents, ...) 243 | 244 | The recommended way to use `buildGradleApplication` is to prevent the use of non reproducible dependencies: 245 | 246 | ```kotlin 247 | configurations.all { 248 | resolutionStrategy { 249 | failOnNonReproducibleResolution() 250 | } 251 | } 252 | 253 | ``` 254 | 255 | If you _must_ use these features (please, don't!), use [dependency locking](https://docs.gradle.org/current/userguide/dependency_locking.html#dependency-locking). 256 | 257 | For more details, see the ["Making sure resolution is reproducible" section in the Gradle Docs](https://docs.gradle.org/current/userguide/resolution_strategy_tuning.html#reproducible-resolution). 258 | 259 | ## How it works 260 | 261 | ![Flow diagram of buildGradleApplication internals](docs/buildGradleApplication-flow.drawio.svg) 262 | 263 | ## Additional Information 264 | 265 | ### Maven Repositories are not Mirrors 266 | 267 | Sadly, many Maven repositories contain the same artifacts but with different metadata. One such example is the Kotlin JVM Gradle Plugin ([Maven Central](https://repo1.maven.org/maven2/org/jetbrains/kotlin/jvm/org.jetbrains.kotlin.jvm.gradle.plugin/1.7.10/org.jetbrains.kotlin.jvm.gradle.plugin-1.7.10.pom) vs. [gradle plugin portal](https://plugins.gradle.org/m2/org/jetbrains/kotlin/jvm/org.jetbrains.kotlin.jvm.gradle.plugin/1.7.10/org.jetbrains.kotlin.jvm.gradle.plugin-1.7.10.pom)). To work around this, `buildGradleApplication` uses a special `fetchArtifact` builder instead of the classic [`fetchurl` fetchers](https://ryantm.github.io/nixpkgs/builders/fetchers/). `fetchArtifact` will try to download a given artifact with a given hash from all provided urls. If the checksum of the downloaded artifact differs from the expected one, it is quietly ignored and the next url is tried instead. 268 | 269 | ### Very slow first build 270 | 271 | The first build with `buildGradleApplication` might be very slow. The reason for this is, that each maven artifact is a dedicated derivation and derivations are not built in parallel by default. 272 | You can speed up the first build by enabling concurrent builds, for example: 273 | 274 | ```bash 275 | nix build -j 15 276 | ``` 277 | 278 | ## Other useful tools 279 | 280 | ### `gradleFromWrapper` 281 | 282 | The [recommended way to execute any Gradle build is with the help of the Gradle Wrapper](https://docs.gradle.org/current/userguide/gradle_wrapper.html). 283 | It's main motivations are to have a standardised version per project and to make it easy to deploy in different execution environments. 284 | When using Nix, these motivations are largely obsolete. 285 | 286 | There may still be reasons to use the wrapper even when using Nix. 287 | In these cases, it's inconvenient to keep both versions (nix and wrapper) in sync. 288 | 289 | To simplify this case, you can use the url and checksum from the `gradle-wrapper.properties` file to build exactly the same gradle package with the `gradleFromWrapper` builder function: 290 | 291 | ```nix 292 | gradle = pkgs.gradleFromWrapper { 293 | wrapperPropertiesPath = ./gradle/wrapper/gradle-wrapper.properties; 294 | defaultJava = pkgs.jdk21; 295 | }; 296 | ``` 297 | 298 | NOTE: This utility _only_ works with nixpkgs 25.11 and above, since it is based on changes made to `gradle-packages.mkGradle`. 299 | 300 | ## Contributing 301 | 302 | Feel free to [create an issue](https://github.com/raphiz/buildGradleApplication/issues/new) or submit a pull request. 303 | 304 | Feedback is also very welcome! 305 | 306 | ## License 307 | 308 | `buildGradleApplication` is licensed under the MIT License. 309 | -------------------------------------------------------------------------------- /docs/buildGradleApplication-flow.drawio.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
parse verification metadata 
(input from derivation)
parse verification metadata...
verification-metadata.xml
verification-metadata.xml
artifact metadata + checksums
artifact metadata + checksums
repository list
(function input  parameter)
repository list...
assemble offline maven repository
assemble offline maven repository
artifact metadata with sources (jar, pom, module, ...)
artifact metadata with sources (...
run gradle build,
link referenced jars,
add metadata,
wrap
run gradle build,...
offline maven repository 
for nix sandbox
offline maven repository...
final derivation
final derivation
fetch artifacts
(fixed-output derivations)
fetch artifacts...
Text is not SVG - cannot display
-------------------------------------------------------------------------------- /examples/funky-dependencies/gradle/verification-metadata.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | false 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | -------------------------------------------------------------------------------- /examples/hello-world/gradle/verification-metadata.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | false 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | --------------------------------------------------------------------------------