Proper way to package java app with dependencies

I’m trying to package PDF4teachers GitHub - ClementGre/PDF4Teachers: PDF editing software for teachers, focused on productivity. PDF4Teachers keeps recorded previous annotations, and offers features like marking scale, PDF conversion, vectorial drawing... which is a java app with some external dependencies. Using mitmCache to deal with them, it seems like I can compile the package, but since I’m a newby in java packaging for NixOS I don’t know how to do the install phase. The package indeed has multiple gradle build tasks:

  • if I call build I have a .jar file that appears in build/libs, but after unpacking this file it seems like it does not contain all dependencies.
  • if I call jlink then the build/image folder contains something that seems useful, but first I can’t get it to work (errors about famous library not found) but anyway I feel like this is not the way to go since it packs a java executable while I feel like I should use nix’s provided java here.
{ lib
, stdenv
, fetchFromGitHub
, gradle_8
, makeWrapper
, jre
, jdk
, tree
, wrapGAppsHook3
, gobject-introspection
}:

stdenv.mkDerivation (finalAttrs: {
  pname = "PDF4Teachers";
  version = "1.4.4";

  src = fetchFromGitHub {
    owner = "ClementGre";
    repo = finalAttrs.pname;
    rev = "${finalAttrs.version}";
    sha256 = "sha256-0J/RVC8NXdyO1kFQOnA6TRKUBsQ6hr4bC4Ztaq1v63c=";
  };

  postPatch = ''
    # Disable update check
    substituteInPlace build.gradle \
      --replace "javaHome.set('/home/clement/.jdks/corretto-21.0.4')" "javaHome.set('${jdk}/lib/openjdk')"
    cat build.gradle
  '';

  
  nativeBuildInputs = [
    gradle_8
    makeWrapper
    wrapGAppsHook3
    gobject-introspection
  ];
 
  buildInputs = [
    
  ];
  
  # if the package has dependencies, mitmCache must be set
  mitmCache = gradle_8.fetchDeps {
    # inherit (finalAttrs) pname;
    pkg = finalAttrs.finalPackage;
    data = ./deps.json;
  };

  # this is required for using mitm-cache on Darwin
  __darwinAllowLocalNetworking = true;

  gradleFlags = [
    "-Dfile.encoding=utf-8"
  ];

  # defaults to "assemble"
  #gradleBuildTask = "shadowJar";
  #gradleBuildTask = "build";
  gradleBuildTask = "jlink";
  #gradleBuildTask = "jar";

  installPhase = ''
    ${tree}/bin/tree
    mkdir -p $out/{bin,lib,conf}
    #cp -r build/image/PDF4Teachers-lin/bin/PDF4Teachers $out/bin
    cp -r build/image/PDF4Teachers-lin/bin/* $out/bin
    cp -r build/image/PDF4Teachers-lin/conf/* $out/conf
    cp -r build/image/PDF4Teachers-lin/lib/* $out/lib
    #cp -r build/ $out
    # mkdir -p $out/{bin,share/pdftk}
    # cp build/libs/pdftk-all.jar $out/share/pdftk

    # makeWrapper ${lib.getExe jre} $out/bin/pdftk \
    #   --add-flags "-jar $out/share/pdftk/pdftk-all.jar"

    # cp ${finalAttrs.src}/pdftk.1 $out/share/man/man1
  '';


  postFixup = ''
  
  '';
  
  meta = with lib; {
    homepage = "https://pdf4teachers.org/";
    description = "Rediscover the annotation and grading of assessments";
    license = licenses.asl20;
    maintainers = with maintainers; [ tobiasBora ];
    platforms = platforms.unix;
    sourceProvenance = with lib.sourceTypes; [
      fromSource
      binaryBytecode # mitm cache
    ];
  };
})