Skip to content

Commit

Permalink
Add gradleFromWrapper utility
Browse files Browse the repository at this point in the history
  • Loading branch information
raphiz committed Oct 13, 2024
1 parent d9cce82 commit 64297c9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,27 @@ For further examples, checkout the [example repository](https://github.com/raphi

All available parameters of `buildGradleApplication` are documented in the [source code](https://github.com/raphiz/buildGradleApplication/blob/main/buildGradleApplication/default.nix)

### Other useful tools

#### `gradleFromWrapper`

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).
It's main motivations are to have a standardised version per project and to make it easy to deploy in different execution environments.
When using Nix, these motivations are largely obsolete.

There may still be reasons to use the wrapper even when using Nix.
In these cases, it's inconvenient to keep both versions (nix and wrapper) in sync.

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:

```nix
gradle = pkgs.gradleFromWrapper {
wrapperPropertiesPath = ./gradle/wrapper/gradle-wrapper.properties;
};
```

NOTE: This utility _only_ works with nixpkgs 24.11 and above, since it is based on changes made to `gradleGen` in [this PR](https://github.com/NixOS/nixpkgs/pull/277721).

## Additional Information

### Maven Repositories are not Mirrors
Expand Down
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
mkM2Repository = prev.callPackage ./buildGradleApplication/mkM2Repository.nix {};
buildGradleApplication = prev.callPackage ./buildGradleApplication/default.nix {};
updateVerificationMetadata = prev.callPackage ./update-verification-metadata/default.nix {};

gradleFromWrapper = import ./gradleFromWrapper final;
};
};
};
Expand Down
34 changes: 34 additions & 0 deletions gradleFromWrapper/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
pkgs: {
wrapperPropertiesPath,
defaultJava ? pkgs.jdk,
}:
pkgs.callPackage (pkgs.gradleGen (let
wrapperProperties = builtins.readFile wrapperPropertiesPath;
lines = pkgs.lib.strings.splitString "\n" wrapperProperties;
in {
inherit defaultJava;

# Extract version from gradle-wrapper.properties
version = let
distributionUrlLine = builtins.head (builtins.filter (
line:
builtins.match "distributionUrl=.*" line != null
)
lines);
versionMatch = builtins.match ".*/gradle-([^-]*)-bin.zip" distributionUrlLine;
versionValue = builtins.elemAt versionMatch 0;
in
versionValue;

# Extract hash from gradle-wrapper.properties
hash = let
sha256SumLine = builtins.head (builtins.filter (
line:
builtins.match "distributionSha256Sum=.*" line != null
)
lines);
sha256Hex = builtins.elemAt (builtins.match "distributionSha256Sum=(.*)" sha256SumLine) 0;
formattedHash = "sha256:" + sha256Hex;
in
formattedHash;
})) {}

0 comments on commit 64297c9

Please sign in to comment.