From fda4e2ec329fa5697105e7cfd2c4a269ea185ba1 Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Wed, 26 Aug 2020 19:30:09 -0700 Subject: [PATCH 1/7] Added direnv integration --- .envrc | 1 + .gitignore | 1 + 2 files changed, 2 insertions(+) create mode 100644 .envrc diff --git a/.envrc b/.envrc new file mode 100644 index 00000000..1d953f4b --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use nix diff --git a/.gitignore b/.gitignore index b4c2b674..1e131c06 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ target/ .idea/ *.iml result +.direnv/ From 05610743429f544bae866bb3ec4e2be99e5159d6 Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Wed, 26 Aug 2020 19:31:05 -0700 Subject: [PATCH 2/7] Added a jdk8 and jdk11 attribute for mvn2nix --- default.nix | 4 +++- derivation.nix | 11 +++++------ overlay.nix | 14 ++++++++++++-- shell.nix | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/default.nix b/default.nix index 0b170568..2620d11a 100644 --- a/default.nix +++ b/default.nix @@ -13,7 +13,9 @@ let ]; }; in { - mvn2nix = pkgs.mvn2nix; + mvn2nix = pkgs.mvn2nix-jdk11; + mvn2nix-jdk11 = pkgs.mvn2nix-jdk11; + mvn2nix-jdk8 = pkgs.mvn2nix-jdk8; buildMavenRepository = pkgs.buildMavenRepository; } diff --git a/derivation.nix b/derivation.nix index a9d18990..185859a8 100644 --- a/derivation.nix +++ b/derivation.nix @@ -1,10 +1,10 @@ -{ lib, stdenv, jdk, maven, makeWrapper, gitignoreSource }: +{ lib, stdenv, jdk11, maven, makeWrapper, gitignoreSource, maven-jdk11 }: with stdenv; let version = "0.1"; dependencies = mkDerivation { name = "mvn2nix-${version}-dependencies"; - buildInputs = [ jdk maven ]; + buildInputs = [ maven-jdk11 ]; src = gitignoreSource ./.; buildPhase = '' while mvn package -Dmaven.repo.local=$out/.m2 -Dmaven.wagon.rto=5000; [ $? = 1 ]; do @@ -28,7 +28,7 @@ in mkDerivation rec { inherit version; name = "${pname}-${version}"; src = gitignoreSource ./.; - buildInputs = [ jdk maven makeWrapper ]; + buildInputs = [ maven-jdk11 makeWrapper ]; buildPhase = '' # 'maven.repo.local' must be writable so copy it out of nix store mvn package --offline -Dmaven.repo.local=${dependencies}/.m2 @@ -48,10 +48,9 @@ in mkDerivation rec { # create a wrapper that will automatically set the classpath # this should be the paths from the dependency derivation - makeWrapper ${jdk}/bin/java $out/bin/${pname} \ + makeWrapper ${jdk11}/bin/java $out/bin/${pname} \ --add-flags "-jar $out/${name}.jar" \ - --set M2_HOME ${maven} \ - --set JAVA_HOME ${jdk} + --set M2_HOME ${maven} ''; meta = with stdenv.lib; { diff --git a/overlay.nix b/overlay.nix index 9269aa20..c7e83c90 100644 --- a/overlay.nix +++ b/overlay.nix @@ -1,7 +1,17 @@ self: super: { - jdk = super.jdk11_headless; + maven-jdk11 = super.maven.override { jdk = super.jdk11; }; - mvn2nix = self.callPackage ./derivation.nix { }; + maven-jdk8 = super.maven.override { jdk = super.jdk8; }; + + mvn2nix-jdk8 = self.callPackage ./derivation.nix { + jdk = super.jdk8; + maven = self.maven-jdk8; + }; + + mvn2nix-jdk11 = self.callPackage ./derivation.nix { + jdk = super.jdk11; + maven = self.maven-jdk11; + }; buildMavenRepository = self.callPackage ./maven.nix { }; } diff --git a/shell.nix b/shell.nix index 278eae3c..950d2ca7 100644 --- a/shell.nix +++ b/shell.nix @@ -16,5 +16,5 @@ let }; in pkgs.mkShell { name = "mvn2nix-shell"; - buildInputs = with pkgs; [ jdk11_headless maven gh-md-toc]; + buildInputs = with pkgs; [ jdk11_headless maven gh-md-toc ]; } From a07b44f81d269197bb80ed3aa0f2a3203982748f Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Wed, 26 Aug 2020 19:33:41 -0700 Subject: [PATCH 3/7] Added README --- README.md | 7 +++++++ overlay.nix | 2 -- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 81bb4380..4599bc3b 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,13 @@ $ nix run -f https://github.com/fzakaria/mvn2nix/archive/master.tar.gz --command If you have [cachix](https://cachix.org/) installed, you can leverage our prebuilt binary. > cachix use fzakaria +If your build builds only with **Java8** (the deafult is Java11); you can execute the alternative +*mvn2nix* attribute. + +```bash +$ nix run -f https://github.com/fzakaria/mvn2nix/archive/master.tar.gz mvn2nix-jdk8 --command mvn2nix +``` + ### Generating the Nix dependencies file In the same spirit of [bundix](https://github.com/nix-community/bundix), **mvn2nix** creates a Nix set with the diff --git a/overlay.nix b/overlay.nix index c7e83c90..922b86c0 100644 --- a/overlay.nix +++ b/overlay.nix @@ -4,12 +4,10 @@ self: super: { maven-jdk8 = super.maven.override { jdk = super.jdk8; }; mvn2nix-jdk8 = self.callPackage ./derivation.nix { - jdk = super.jdk8; maven = self.maven-jdk8; }; mvn2nix-jdk11 = self.callPackage ./derivation.nix { - jdk = super.jdk11; maven = self.maven-jdk11; }; From e46f963b8d376e3d2138ae68aab0b2cee65b3860 Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Wed, 26 Aug 2020 19:58:40 -0700 Subject: [PATCH 4/7] Add safeguard for non artifacts --- .../java/com/fzakaria/mvn2nix/maven/Maven.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/fzakaria/mvn2nix/maven/Maven.java b/src/main/java/com/fzakaria/mvn2nix/maven/Maven.java index 02e167d1..eb213740 100644 --- a/src/main/java/com/fzakaria/mvn2nix/maven/Maven.java +++ b/src/main/java/com/fzakaria/mvn2nix/maven/Maven.java @@ -22,6 +22,7 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.Collection; +import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.StreamSupport; @@ -139,6 +140,15 @@ public Collection collectAllArtifactsInLocalRepository() { String nameAndVersionAndClassifier = com.google.common.io.Files.getNameWithoutExtension(layout.toFile().getName()); String version = layout.getParent().toFile().getName(); String name = layout.getParent().getParent().toFile().getName(); + + /* + * This is an easy safeguard to make sure we only capture correct artifacts regardless + * of the extension. The artifact must follow the pattern ${name}-${version} at the very least + */ + if (!(nameAndVersionAndClassifier.contains(name) && nameAndVersionAndClassifier.contains(version))) { + return null; + } + String classifier = nameAndVersionAndClassifier .replaceAll(name + "-" + version, "") .replaceFirst("^-", ""); @@ -154,7 +164,9 @@ public Collection collectAllArtifactsInLocalRepository() { .setVersion(version) .setExtension(extension) .build(); - }).collect(Collectors.toList()); + }) + .filter(Objects::nonNull) + .collect(Collectors.toList()); } catch (IOException e) { throw new UncheckedIOException(e); } From 42ded71cb014af8ad0488bacdc3579a112a92d8c Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Wed, 26 Aug 2020 20:15:10 -0700 Subject: [PATCH 5/7] Filter unpacked and asc --- src/main/java/com/fzakaria/mvn2nix/maven/Maven.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/fzakaria/mvn2nix/maven/Maven.java b/src/main/java/com/fzakaria/mvn2nix/maven/Maven.java index eb213740..8d4f0f0c 100644 --- a/src/main/java/com/fzakaria/mvn2nix/maven/Maven.java +++ b/src/main/java/com/fzakaria/mvn2nix/maven/Maven.java @@ -133,6 +133,8 @@ public Collection collectAllArtifactsInLocalRepository() { .filter(f -> !f.toFile().getName().equals("_remote.repositories")) .filter(f -> !f.toFile().getName().equals("resolver-status.properties")) .filter(f -> !f.toFile().getName().endsWith("lastUpdated")) + .filter(f -> !f.toFile().getName().endsWith("asc")) + .filter(f -> !f.toFile().getName().endsWith("unpacked")) .map(file -> { Path layout = localRepository.toPath().relativize(file); From d86201fe4f78b925f52d384332ea37acabc5f8e6 Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Wed, 26 Aug 2020 20:40:35 -0700 Subject: [PATCH 6/7] Fix github workflow --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ba0e1777..c4a1b5ac 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,6 +14,6 @@ jobs: with: name: fzakaria signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}' - - run: nix-build + - run: nix-build -A mvn2nix - run: ./result/bin/mvn2nix > dependencies.nix - run: nix-build -A buildMavenRepository --arg dependencies "import ./dependencies.nix" \ No newline at end of file From 41cc1c464350af2afb9db151b6642de7b7f96540 Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Wed, 26 Aug 2020 20:55:14 -0700 Subject: [PATCH 7/7] Set JAVA_HOME explicitly Maven only sets JAVA_HOME if its not set. I suspect that GitHub actions are setting JAVA_HOME; so it's not getting the JDK11 version and failing to build. --- derivation.nix | 5 +++-- examples/mvn2nix/default.nix | 2 +- overlay.nix | 2 ++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/derivation.nix b/derivation.nix index 185859a8..b8cf7e2d 100644 --- a/derivation.nix +++ b/derivation.nix @@ -1,4 +1,4 @@ -{ lib, stdenv, jdk11, maven, makeWrapper, gitignoreSource, maven-jdk11 }: +{ lib, stdenv, jdk, jdk11, maven, makeWrapper, gitignoreSource, maven-jdk11 }: with stdenv; let version = "0.1"; @@ -50,7 +50,8 @@ in mkDerivation rec { # this should be the paths from the dependency derivation makeWrapper ${jdk11}/bin/java $out/bin/${pname} \ --add-flags "-jar $out/${name}.jar" \ - --set M2_HOME ${maven} + --set M2_HOME ${maven} \ + --set JAVA_HOME ${jdk} ''; meta = with stdenv.lib; { diff --git a/examples/mvn2nix/default.nix b/examples/mvn2nix/default.nix index 2d188fb0..9971c212 100644 --- a/examples/mvn2nix/default.nix +++ b/examples/mvn2nix/default.nix @@ -28,7 +28,7 @@ in mkDerivation rec { mkdir -p $out/bin # create a symbolic link for the lib directory - ln -s ${mavenRepository}/.m2 $out/lib + ln -s ${mavenRepository} $out/lib # copy out the JAR # Maven already setup the classpath to use m2 repository layout diff --git a/overlay.nix b/overlay.nix index 922b86c0..c7e83c90 100644 --- a/overlay.nix +++ b/overlay.nix @@ -4,10 +4,12 @@ self: super: { maven-jdk8 = super.maven.override { jdk = super.jdk8; }; mvn2nix-jdk8 = self.callPackage ./derivation.nix { + jdk = super.jdk8; maven = self.maven-jdk8; }; mvn2nix-jdk11 = self.callPackage ./derivation.nix { + jdk = super.jdk11; maven = self.maven-jdk11; };