about summary refs log tree commit diff
path: root/pkgs/by-name/ma
diff options
context:
space:
mode:
authorAnderson Torres <torres.anderson.85@protonmail.com>2024-06-02 20:15:40 -0300
committerAnderson Torres <torres.anderson.85@protonmail.com>2024-06-04 21:56:17 -0300
commitd1402093675d1314f86cce1471cb3d2ffa22c08d (patch)
tree51dd714cd89d20b78f6f75aeedd896c1a4a41ecf /pkgs/by-name/ma
parent8b1530b937c6d1a82bddaa1594b31558921ab98e (diff)
maven: migrate to by-name
Diffstat (limited to 'pkgs/by-name/ma')
-rw-r--r--pkgs/by-name/ma/maven/build-maven.nix88
-rw-r--r--pkgs/by-name/ma/maven/build-package.nix98
-rw-r--r--pkgs/by-name/ma/maven/package.nix55
3 files changed, 241 insertions, 0 deletions
diff --git a/pkgs/by-name/ma/maven/build-maven.nix b/pkgs/by-name/ma/maven/build-maven.nix
new file mode 100644
index 0000000000000..7ac8afdde225a
--- /dev/null
+++ b/pkgs/by-name/ma/maven/build-maven.nix
@@ -0,0 +1,88 @@
+{ 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;
+
+  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
+        "";
+
+      fetchMetadata = (if authenticated then requireFile else fetchurl) {
+        inherit (metadata) url sha1;
+      };
+
+      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 = 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"
+      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;
+in {
+  inherit repo settings info;
+
+  build = stdenv.mkDerivation {
+    name = "${info.project.artifactId}-${info.project.version}.jar";
+
+    src = builtins.filterSource (path: type:
+      (toString path) != (toString (src + "/target")) && (toString path)
+      != (toString (src + "/.git"))) src;
+
+    buildInputs = [ maven ];
+
+    buildPhase = "mvn --offline --settings ${settings} compile";
+
+    installPhase = ''
+      mvn --offline --settings ${settings} package
+      mv target/*.jar $out
+    '';
+  };
+}
diff --git a/pkgs/by-name/ma/maven/build-package.nix b/pkgs/by-name/ma/maven/build-package.nix
new file mode 100644
index 0000000000000..f9ff54696dfe9
--- /dev/null
+++ b/pkgs/by-name/ma/maven/build-package.nix
@@ -0,0 +1,98 @@
+{ lib
+, stdenv
+, maven
+}:
+
+{ src
+, sourceRoot ? null
+, buildOffline ? false
+, doCheck ? true
+, patches ? [ ]
+, pname
+, version
+, mvnHash ? ""
+, mvnFetchExtraArgs ? { }
+, mvnDepsParameters ? ""
+, manualMvnArtifacts ? [ ]
+, manualMvnSources ? [ ]
+, mvnParameters ? ""
+, ...
+} @args:
+
+# originally extracted from dbeaver
+# created to allow using maven packages in the same style as rust
+
+let
+  mvnSkipTests = lib.optionalString (!doCheck) "-DskipTests";
+  fetchedMavenDeps = stdenv.mkDerivation ({
+    name = "${pname}-${version}-maven-deps";
+    inherit src sourceRoot patches;
+
+    nativeBuildInputs = [
+      maven
+    ] ++ args.nativeBuildInputs or [ ];
+
+    buildPhase = ''
+      runHook preBuild
+    '' + lib.optionalString buildOffline ''
+      mvn de.qaware.maven:go-offline-maven-plugin:1.2.8:resolve-dependencies -Dmaven.repo.local=$out/.m2 ${mvnDepsParameters}
+
+      for artifactId in ${builtins.toString manualMvnArtifacts}
+      do
+        echo "downloading manual $artifactId"
+        mvn dependency:get -Dartifact="$artifactId" -Dmaven.repo.local=$out/.m2
+      done
+
+      for artifactId in ${builtins.toString manualMvnSources}
+      do
+        group=$(echo $artifactId | cut -d':' -f1)
+        artifact=$(echo $artifactId | cut -d':' -f2)
+        echo "downloading manual sources $artifactId"
+        mvn dependency:sources -DincludeGroupIds="$group" -DincludeArtifactIds="$artifact" -Dmaven.repo.local=$out/.m2
+      done
+    '' + lib.optionalString (!buildOffline) ''
+      mvn package -Dmaven.repo.local=$out/.m2 ${mvnSkipTests} ${mvnParameters}
+    '' + ''
+      runHook postBuild
+    '';
+
+    # keep only *.{pom,jar,sha1,nbm} and delete all ephemeral files with lastModified timestamps inside
+    installPhase = ''
+      runHook preInstall
+
+      find $out -type f \( \
+        -name \*.lastUpdated \
+        -o -name resolver-status.properties \
+        -o -name _remote.repositories \) \
+        -delete
+
+      runHook postInstall
+    '';
+
+    # don't do any fixup
+    dontFixup = true;
+    outputHashAlgo = if mvnHash != "" then null else "sha256";
+    outputHashMode = "recursive";
+    outputHash = mvnHash;
+  } // mvnFetchExtraArgs);
+in
+stdenv.mkDerivation (builtins.removeAttrs args [ "mvnFetchExtraArgs" ] // {
+  inherit fetchedMavenDeps;
+
+  nativeBuildInputs = args.nativeBuildInputs or [ ] ++ [
+    maven
+  ];
+
+  buildPhase = ''
+    runHook preBuild
+
+    mvnDeps=$(cp -dpR ${fetchedMavenDeps}/.m2 ./ && chmod +w -R .m2 && pwd)
+    mvn package -o -nsu "-Dmaven.repo.local=$mvnDeps/.m2" ${mvnSkipTests} ${mvnParameters}
+
+    runHook postBuild
+  '';
+
+  meta = args.meta or { } // {
+    platforms = args.meta.platforms or maven.meta.platforms;
+  };
+})
diff --git a/pkgs/by-name/ma/maven/package.nix b/pkgs/by-name/ma/maven/package.nix
new file mode 100644
index 0000000000000..388973782e53a
--- /dev/null
+++ b/pkgs/by-name/ma/maven/package.nix
@@ -0,0 +1,55 @@
+{ lib
+, stdenvNoCC
+, fetchurl
+, jdk
+, makeWrapper
+, callPackage
+}:
+
+assert jdk != null;
+
+stdenvNoCC.mkDerivation (finalAttrs: {
+  pname = "apache-maven";
+  version = "3.9.6";
+
+  src = fetchurl {
+    url = "mirror://apache/maven/maven-3/${finalAttrs.version}/binaries/${finalAttrs.pname}-${finalAttrs.version}-bin.tar.gz";
+    hash = "sha256-bu3SyuNibWrTpcnuMkvSZYU9ZCl/B/AzQwdVvQ4MOks=";
+  };
+
+  sourceRoot = ".";
+
+  nativeBuildInputs = [ makeWrapper ];
+
+  installPhase = ''
+    runHook preInstall
+
+    mkdir -p $out/maven
+    cp -r ${finalAttrs.pname}-${finalAttrs.version}/* $out/maven
+
+    makeWrapper $out/maven/bin/mvn $out/bin/mvn \
+      --set-default JAVA_HOME "${jdk}"
+    makeWrapper $out/maven/bin/mvnDebug $out/bin/mvnDebug \
+      --set-default JAVA_HOME "${jdk}"
+
+    runHook postInstall
+  '';
+
+  passthru = {
+    buildMaven = callPackage ./build-maven.nix {
+      maven = finalAttrs.finalPackage;
+    };
+    buildMavenPackage = callPackage ./build-package.nix {
+      maven = finalAttrs.finalPackage;
+    };
+  };
+
+  meta = with lib; {
+    mainProgram = "mvn";
+    description = "Build automation tool (used primarily for Java projects)";
+    homepage = "https://maven.apache.org/";
+    license = licenses.asl20;
+    platforms = platforms.unix;
+    maintainers = with maintainers; [ cko ];
+  };
+})