summary refs log tree commit diff
path: root/pkgs/build-support/build-maven.nix
diff options
context:
space:
mode:
authorFarid Zakaria <fmzakari@google.com>2020-10-15 12:14:59 -0700
committerFarid Zakaria <fmzakari@google.com>2020-10-15 12:16:02 -0700
commit09abb91d144afc26689d557afa0933e319b6eda6 (patch)
tree4325ce2efd87fb1da79d47ce1cf05b26991c9d9d /pkgs/build-support/build-maven.nix
parent8b4d70ca94e3ebb959d8737925d2958ea6576c81 (diff)
downloadnixpkgs-09abb91d144afc26689d557afa0933e319b6eda6.tar
nixpkgs-09abb91d144afc26689d557afa0933e319b6eda6.tar.gz
nixpkgs-09abb91d144afc26689d557afa0933e319b6eda6.tar.bz2
nixpkgs-09abb91d144afc26689d557afa0933e319b6eda6.tar.lz
nixpkgs-09abb91d144afc26689d557afa0933e319b6eda6.tar.xz
nixpkgs-09abb91d144afc26689d557afa0933e319b6eda6.tar.zst
nixpkgs-09abb91d144afc26689d557afa0933e319b6eda6.zip
buildMaven: Update buildMaven to pure Nix
Change the code that builds the maven repository to one using pure nix
rather than a bash script.
Diffstat (limited to 'pkgs/build-support/build-maven.nix')
-rw-r--r--pkgs/build-support/build-maven.nix85
1 files changed, 46 insertions, 39 deletions
diff --git a/pkgs/build-support/build-maven.nix b/pkgs/build-support/build-maven.nix
index f47e3ebc61c..7ac8afdde22 100644
--- a/pkgs/build-support/build-maven.nix
+++ b/pkgs/build-support/build-maven.nix
@@ -1,52 +1,60 @@
-{ stdenv, maven, runCommand, writeText, fetchurl, lib, requireFile }:
-/* 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.
- */
-infoFile: let
+{ stdenv, maven, runCommand, writeText, fetchurl, lib, requireFile, linkFarm }:
+# 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;
 
-  script = writeText "build-maven-repository.sh" ''
-    ${lib.concatStrings (map (dep: let
+  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 "";
 
-      fetch = if (url != "") then ((if authenticated then requireFile else fetchurl) {
-        inherit url sha1;
-      }) else "";
+      fetch = if (url != "") then
+        ((if authenticated then requireFile else fetchurl) {
+          inherit url sha1;
+        })
+      else
+        "";
 
       fetchMetadata = (if authenticated then requireFile else fetchurl) {
         inherit (metadata) url sha1;
       };
-    in ''
-      dir=$out/$(echo ${groupId} | sed 's|\.|/|g')/${artifactId}/${versionDir}
-      mkdir -p $dir
 
-      ${lib.optionalString (fetch != "") ''
-        ln -sv ${fetch} $dir/${fetch.name}
-      ''}
-      ${lib.optionalString (dep ? metadata) ''
-        ln -svf ${fetchMetadata} $dir/maven-metadata-${repository-id}.xml
-        ${lib.optionalString (fetch != "") ''
-          ln -sv ${fetch} $dir/$(echo ${fetch.name} | sed 's|${version}|${dep.unresolved-version}|')
-        ''}
-      ''}
-    '') info.dependencies)}
-  '';
+      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);
 
-  repo = runCommand "maven-repository" {} ''
-    bash ${script}
-  '';
+  repo = linkFarm "maven-repository" (lib.forEach dependencies (dependency: {
+    name = dependency.layout;
+    path = dependency.drv;
+  }));
 
   settings = writeText "settings.xml" ''
     <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
@@ -65,9 +73,8 @@ in {
     name = "${info.project.artifactId}-${info.project.version}.jar";
 
     src = builtins.filterSource (path: type:
-      (toString path) != (toString (src + "/target")) &&
-        (toString path) != (toString (src + "/.git"))
-    ) src;
+      (toString path) != (toString (src + "/target")) && (toString path)
+      != (toString (src + "/.git"))) src;
 
     buildInputs = [ maven ];