Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
at 20.09 63 lines 2.9 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 nativeBuildInputs = [ jdk ant ]; 14 15 buildPhase = "ant"; 16} 17</programlisting> 18 Note that <varname>jdk</varname> is an alias for the OpenJDK (self-built where available, or pre-built via Zulu). Platforms with OpenJDK not (yet) in Nixpkgs (<literal>Aarch32</literal>, <literal>Aarch64</literal>) point to the (unfree) <literal>oraclejdk</literal>. 19 </para> 20 21 <para> 22 JAR files that are intended to be used by other packages should be installed in <filename>$out/share/java</filename>. JDKs have a stdenv setup hook that add any JARs in the <filename>share/java</filename> directories of the build inputs to the <envar>CLASSPATH</envar> environment variable. For instance, if the package <literal>libfoo</literal> installs a JAR named <filename>foo.jar</filename> in its <filename>share/java</filename> directory, and another package declares the attribute 23<programlisting> 24buildInputs = [ libfoo ]; 25nativeBuildInputs = [ jdk ]; 26</programlisting> 27 then <envar>CLASSPATH</envar> will be set to <filename>/nix/store/...-libfoo/share/java/foo.jar</filename>. 28 </para> 29 30 <para> 31 Private JARs should be installed in a location like <filename>$out/share/<replaceable>package-name</replaceable></filename>. 32 </para> 33 34 <para> 35 If your Java package provides a program, you need to generate a wrapper script to run it using the OpenJRE. You can use <literal>makeWrapper</literal> for this: 36<programlisting> 37nativeBuildInputs = [ makeWrapper ]; 38 39installPhase = 40 '' 41 mkdir -p $out/bin 42 makeWrapper ${jre}/bin/java $out/bin/foo \ 43 --add-flags "-cp $out/share/java/foo.jar org.foo.Main" 44 ''; 45</programlisting> 46 Note the use of <literal>jre</literal>, which is the part of the OpenJDK package that contains the Java Runtime Environment. By using <literal>${jre}/bin/java</literal> instead of <literal>${jdk}/bin/java</literal>, you prevent your package from depending on the JDK at runtime. 47 </para> 48 49 <para> 50 Note all JDKs passthru <literal>home</literal>, so if your application requires environment variables like <envar>JAVA_HOME</envar> being set, that can be done in a generic fashion with the <literal>--set</literal> argument of <literal>makeWrapper</literal>: 51<programlisting> 52--set JAVA_HOME ${jdk.home} 53</programlisting> 54 </para> 55 56 <para> 57 It is possible to use a different Java compiler than <command>javac</command> from the OpenJDK. For instance, to use the GNU Java Compiler: 58<programlisting> 59nativeBuildInputs = [ gcj ant ]; 60</programlisting> 61 Here, Ant will automatically use <command>gij</command> (the GNU Java Runtime) instead of the OpenJRE. 62 </para> 63</section>