Merge pull request #100634 from fzakaria/faridzakaria/buildmaven-nix

buildMaven: Update buildMaven to pure Nix

authored by Shea Levy and committed by GitHub ee7722a7 56f935ef

+46 -39
+46 -39
pkgs/build-support/build-maven.nix
··· 1 - { stdenv, maven, runCommand, writeText, fetchurl, lib, requireFile }: 2 - /* Takes an info file generated by mvn2nix 3 - * (https://github.com/NixOS/mvn2nix-maven-plugin) and builds the maven 4 - * project with it. 5 - * 6 - * repo: A local maven repository with the project's dependencies. 7 - * 8 - * settings: A settings.xml to pass to maven to use the repo. 9 - * 10 - * build: A simple build derivation that uses mvn compile and package to build 11 - * the project. 12 - */ 13 - infoFile: let 1 + { stdenv, maven, runCommand, writeText, fetchurl, lib, requireFile, linkFarm }: 2 + # Takes an info file generated by mvn2nix 3 + # (https://github.com/NixOS/mvn2nix-maven-plugin) and builds the maven 4 + # project with it. 5 + # 6 + # repo: A local maven repository with the project's dependencies. 7 + # 8 + # settings: A settings.xml to pass to maven to use the repo. 9 + # 10 + # build: A simple build derivation that uses mvn compile and package to build 11 + # the project. 12 + # 13 + # @example 14 + # project = pkgs.buildMaven ./project-info.json 15 + infoFile: 16 + let 14 17 info = lib.importJSON infoFile; 15 18 16 - script = writeText "build-maven-repository.sh" '' 17 - ${lib.concatStrings (map (dep: let 19 + dependencies = lib.flatten (map (dep: 20 + let 18 21 inherit (dep) sha1 groupId artifactId version metadata repository-id; 19 - 20 22 versionDir = dep.unresolved-version or version; 21 23 authenticated = dep.authenticated or false; 22 24 url = dep.url or ""; 23 25 24 - fetch = if (url != "") then ((if authenticated then requireFile else fetchurl) { 25 - inherit url sha1; 26 - }) else ""; 26 + fetch = if (url != "") then 27 + ((if authenticated then requireFile else fetchurl) { 28 + inherit url sha1; 29 + }) 30 + else 31 + ""; 27 32 28 33 fetchMetadata = (if authenticated then requireFile else fetchurl) { 29 34 inherit (metadata) url sha1; 30 35 }; 31 - in '' 32 - dir=$out/$(echo ${groupId} | sed 's|\.|/|g')/${artifactId}/${versionDir} 33 - mkdir -p $dir 34 36 35 - ${lib.optionalString (fetch != "") '' 36 - ln -sv ${fetch} $dir/${fetch.name} 37 - ''} 38 - ${lib.optionalString (dep ? metadata) '' 39 - ln -svf ${fetchMetadata} $dir/maven-metadata-${repository-id}.xml 40 - ${lib.optionalString (fetch != "") '' 41 - ln -sv ${fetch} $dir/$(echo ${fetch.name} | sed 's|${version}|${dep.unresolved-version}|') 42 - ''} 43 - ''} 44 - '') info.dependencies)} 45 - ''; 37 + layout = "${ 38 + builtins.replaceStrings [ "." ] [ "/" ] groupId 39 + }/${artifactId}/${versionDir}"; 40 + in lib.optional (url != "") { 41 + layout = "${layout}/${fetch.name}"; 42 + drv = fetch; 43 + } ++ lib.optionals (dep ? metadata) ([{ 44 + layout = "${layout}/maven-metadata-${repository-id}.xml"; 45 + drv = fetchMetadata; 46 + }] ++ lib.optional (fetch != "") { 47 + layout = "${layout}/${ 48 + builtins.replaceStrings [ version ] [ dep.unresolved-version ] 49 + fetch.name 50 + }"; 51 + drv = fetch; 52 + })) info.dependencies); 46 53 47 - repo = runCommand "maven-repository" {} '' 48 - bash ${script} 49 - ''; 54 + repo = linkFarm "maven-repository" (lib.forEach dependencies (dependency: { 55 + name = dependency.layout; 56 + path = dependency.drv; 57 + })); 50 58 51 59 settings = writeText "settings.xml" '' 52 60 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" ··· 65 73 name = "${info.project.artifactId}-${info.project.version}.jar"; 66 74 67 75 src = builtins.filterSource (path: type: 68 - (toString path) != (toString (src + "/target")) && 69 - (toString path) != (toString (src + "/.git")) 70 - ) src; 76 + (toString path) != (toString (src + "/target")) && (toString path) 77 + != (toString (src + "/.git"))) src; 71 78 72 79 buildInputs = [ maven ]; 73 80