core/pkgs/build-support/build-maven.nix

112 lines
2.8 KiB
Nix
Raw Normal View History

2024-06-30 08:12:46 +00:00
{
stdenv,
maven,
runCommand,
writeText,
fetchurl,
lib,
requireFile,
linkFarm,
}:
2024-05-02 00:46:19 +00:00
# Takes an info file generated by mvn2nix
# (https://github.com/NixOS/mvn2nix-maven-plugin) and builds the maven
# project with it.
#
# repo: A local maven repository with the project's dependencies.
#
# settings: A settings.xml to pass to maven to use the repo.
#
# build: A simple build derivation that uses mvn compile and package to build
# the project.
#
# @example
# project = pkgs.buildMaven ./project-info.json
infoFile:
let
info = lib.importJSON infoFile;
2024-06-30 08:12:46 +00:00
dependencies = lib.flatten (
map (
dep:
let
inherit (dep)
sha1
groupId
artifactId
version
metadata
repository-id
;
versionDir = dep.unresolved-version or version;
authenticated = dep.authenticated or false;
url = dep.url or "";
2024-05-02 00:46:19 +00:00
2024-06-30 08:12:46 +00:00
fetch =
if (url != "") then
((if authenticated then requireFile else fetchurl) { inherit url sha1; })
else
"";
2024-05-02 00:46:19 +00:00
2024-06-30 08:12:46 +00:00
fetchMetadata = (if authenticated then requireFile else fetchurl) { inherit (metadata) url sha1; };
2024-05-02 00:46:19 +00:00
2024-06-30 08:12:46 +00:00
layout = "${builtins.replaceStrings [ "." ] [ "/" ] groupId}/${artifactId}/${versionDir}";
in
lib.optional (url != "") {
layout = "${layout}/${fetch.name}";
drv = fetch;
}
++ lib.optionals (dep ? metadata) (
[
{
layout = "${layout}/maven-metadata-${repository-id}.xml";
drv = fetchMetadata;
}
]
++ lib.optional (fetch != "") {
layout = "${layout}/${builtins.replaceStrings [ version ] [ dep.unresolved-version ] fetch.name}";
drv = fetch;
}
)
) info.dependencies
);
2024-05-02 00:46:19 +00:00
2024-06-30 08:12:46 +00:00
repo = linkFarm "maven-repository" (
lib.forEach dependencies (dependency: {
name = dependency.layout;
path = dependency.drv;
})
);
2024-05-02 00:46:19 +00:00
settings = writeText "settings.xml" ''
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>${repo}</localRepository>
</settings>
'';
src = dirOf infoFile;
2024-06-30 08:12:46 +00:00
in
{
2024-05-02 00:46:19 +00:00
inherit repo settings info;
build = stdenv.mkDerivation {
name = "${info.project.artifactId}-${info.project.version}.jar";
2024-06-30 08:12:46 +00:00
src = builtins.filterSource (
path: type:
(toString path) != (toString (src + "/target")) && (toString path) != (toString (src + "/.git"))
) src;
2024-05-02 00:46:19 +00:00
buildInputs = [ maven ];
buildPhase = "mvn --offline --settings ${settings} compile";
installPhase = ''
mvn --offline --settings ${settings} package
mv target/*.jar $out
'';
};
}