Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
at release-18.03 78 lines 2.7 kB view raw
1<section xmlns="http://docbook.org/ns/docbook" 2 xmlns:xlink="http://www.w3.org/1999/xlink" 3 xml:id="sec-language-java"> 4 <title>Java</title> 5 6 <para> 7 Ant-based Java packages are typically built from source as follows: 8<programlisting> 9stdenv.mkDerivation { 10 name = "..."; 11 src = fetchurl { ... }; 12 13 buildInputs = [ jdk ant ]; 14 15 buildPhase = "ant"; 16} 17</programlisting> 18 Note that <varname>jdk</varname> is an alias for the OpenJDK. 19 </para> 20 21 <para> 22 JAR files that are intended to be used by other packages should be installed 23 in <filename>$out/share/java</filename>. The OpenJDK has a stdenv setup hook 24 that adds any JARs in the <filename>share/java</filename> directories of the 25 build inputs to the <envar>CLASSPATH</envar> environment variable. For 26 instance, if the package <literal>libfoo</literal> installs a JAR named 27 <filename>foo.jar</filename> in its <filename>share/java</filename> 28 directory, and another package declares the attribute 29<programlisting> 30buildInputs = [ jdk libfoo ]; 31</programlisting> 32 then <envar>CLASSPATH</envar> will be set to 33 <filename>/nix/store/...-libfoo/share/java/foo.jar</filename>. 34 </para> 35 36 <para> 37 Private JARs should be installed in a location like 38 <filename>$out/share/<replaceable>package-name</replaceable></filename>. 39 </para> 40 41 <para> 42 If your Java package provides a program, you need to generate a wrapper 43 script to run it using the OpenJRE. You can use 44 <literal>makeWrapper</literal> for this: 45<programlisting> 46buildInputs = [ makeWrapper ]; 47 48installPhase = 49 '' 50 mkdir -p $out/bin 51 makeWrapper ${jre}/bin/java $out/bin/foo \ 52 --add-flags "-cp $out/share/java/foo.jar org.foo.Main" 53 ''; 54</programlisting> 55 Note the use of <literal>jre</literal>, which is the part of the OpenJDK 56 package that contains the Java Runtime Environment. By using 57 <literal>${jre}/bin/java</literal> instead of 58 <literal>${jdk}/bin/java</literal>, you prevent your package from depending 59 on the JDK at runtime. 60 </para> 61 62 <para> 63 It is possible to use a different Java compiler than <command>javac</command> 64 from the OpenJDK. For instance, to use the Eclipse Java Compiler: 65<programlisting> 66buildInputs = [ jre ant ecj ]; 67</programlisting> 68 (Note that here you don’t need the full JDK as an input, but just the JRE.) 69 The ECJ has a stdenv setup hook that sets some environment variables to cause 70 Ant to use ECJ, but this doesn’t work with all Ant files. Similarly, you 71 can use the GNU Java Compiler: 72<programlisting> 73buildInputs = [ gcj ant ]; 74</programlisting> 75 Here, Ant will automatically use <command>gij</command> (the GNU Java 76 Runtime) instead of the OpenJRE. 77 </para> 78</section>