Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JDK8 support #19

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use nix
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added direnv; unrelated to the fix.

2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ jobs:
with:
name: fzakaria
signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
- run: nix-build
- run: nix-build -A mvn2nix
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure we build the JDK11 version

- run: ./result/bin/mvn2nix > dependencies.nix
- run: nix-build -A buildMavenRepository --arg dependencies "import ./dependencies.nix"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ target/
.idea/
*.iml
result
.direnv/
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Comment on lines +29 to +34
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bbigras You will have to run the JDK8 command here.


### Generating the Nix dependencies file

In the same spirit of [bundix](https://github.com/nix-community/bundix), **mvn2nix** creates a Nix set with the
Expand Down
4 changes: 3 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
11 changes: 5 additions & 6 deletions derivation.nix
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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; {
Expand Down
12 changes: 10 additions & 2 deletions overlay.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
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 {
maven = self.maven-jdk8;
};

mvn2nix-jdk11 = self.callPackage ./derivation.nix {
maven = self.maven-jdk11;
};

buildMavenRepository = self.callPackage ./maven.nix { };
}
2 changes: 1 addition & 1 deletion shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
Comment on lines 18 to +19
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nixfmt formatting

}
16 changes: 15 additions & 1 deletion src/main/java/com/fzakaria/mvn2nix/maven/Maven.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -132,13 +133,24 @@ public Collection<Artifact> 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);

String extension = com.google.common.io.Files.getFileExtension(layout.toString());
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("^-", "");
Expand All @@ -154,7 +166,9 @@ public Collection<Artifact> collectAllArtifactsInLocalRepository() {
.setVersion(version)
.setExtension(extension)
.build();
}).collect(Collectors.toList());
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down