Add a setup hook that automatically sets up $CLASSPATH

All JARs in $pkg/share/java (for each $pkg in the build inputs) are
added to $CLASSPATH.  Thus, you can say

  buildInputs = [ setJavaClassPath someJavaDependency ];

and the JARs in someJavaDependency will be found automatically by
tools like javac or ant.

Note that the manual used to say that JARs should be installed in
lib/java; this is now share/java, following the Debian policy:

  http://www.debian.org/doc/packaging-manuals/java-policy/x110.html

The directory share/java makes more sense because JARs are
architecture-independent.  (Also, a quick grep shows that we were not
exactly consistent about this in Nixpkgs.)
This commit is contained in:
Eelco Dolstra 2014-01-06 13:42:37 +01:00
parent e36427fd4e
commit 5d6259a973
4 changed files with 22 additions and 11 deletions

View File

@ -236,7 +236,7 @@ twisted = buildPythonPackage {
<section><title>Java</title>
<para>Java packages should install JAR files in
<filename>$out/lib/java</filename>.</para>
<filename>$out/share/java</filename>.</para>
</section>

View File

@ -0,0 +1,13 @@
# This setup hook adds every JAR in the share/java subdirectories of
# the build inputs to $CLASSPATH.
export CLASSPATH
addPkgToClassPath () {
local jar
for jar in $1/share/java/*.jar; do
export CLASSPATH=''${CLASSPATH}''${CLASSPATH:+:}''${jar}
done
}
envHooks=(''${envHooks[@]} addPkgToClassPath)

View File

@ -1,4 +1,4 @@
{ stdenv, fetchurl, unzip, ant, jdk }:
{ stdenv, fetchurl, unzip, ant, jdk, makeWrapper }:
let
version = "3.7.2";
@ -13,7 +13,7 @@ stdenv.mkDerivation rec {
sha256 = "0swyysbyfmv068x8q1c5jqpwk5zb4xahg17aypx5rwb660f8fpbm";
};
buildInputs = [ unzip ant jdk ];
buildInputs = [ unzip ant jdk makeWrapper ];
unpackPhase = ''
mkdir "${name}"
@ -25,16 +25,12 @@ stdenv.mkDerivation rec {
buildPhase = "ant build";
installPhase = ''
mkdir -pv $out/lib/java
cp -v *.jar $out/lib/java
mkdir -pv $out/share/java
cp -v *.jar $out/share/java
mkdir -pv $out/bin
cat > $out/bin/ecj <<EOF
#! /bin/sh
exec ${jdk.jre}/bin/java -cp $out/lib/java/ecj.jar org.eclipse.jdt.internal.compiler.batch.Main "\$@"
EOF
chmod u+x $out/bin/ecj
makeWrapper ${jdk.jre}/bin/java $out/bin/ecj \
--add-flags "-cp $out/share/java/ecj.jar org.eclipse.jdt.internal.compiler.batch.Main"
'';
meta = {

View File

@ -385,6 +385,8 @@ let
platforms = import ./platforms.nix;
setJavaClassPath = makeSetupHook { } ../build-support/setup-hooks/set-java-classpath.sh;
### TOOLS