Debian Binary Package Building

In this example I am going to create a deb package for Java SE Development Kit 6 Update 31 using the dpkg-deb command.

dpkg-deb creates a debian archive from the filesystem tree stored in directory. directory must have a DEBIAN subdirectory, which contains the control information files such as the control file itself. This directory will not appear in the binary package's filesystem archive, but instead the files in it will be put in the binary package's control information area.

First lets download the JDK from Oracle and extract it:

[root@server1 ~] cd /usr/src
[root@server1 ~] wget http://download.oracle.com/otn-pub/java/jdk/6u31-b04/jdk-6u31-linux-x64.bin
[root@server1 ~] bash jdk-6u31-linux-x64.bin
view raw gistfile1.sh hosted with ❤ by GitHub

Since we are going to install JDK on Unutu, jdk-6u31-linux-x64 needs to go to /usr/lib/jvm, and the following symlinks needs to exist:

[root@server1 ~] ls -la /usr/bin/java
lrwxrwxrwx 1 root root 22 2012-02-17 19:24 /usr/bin/java -> /etc/alternatives/java
[root@server1 ~] ls -la /etc/alternatives/java
lrwxrwxrwx 1 root root 37 2012-02-17 19:24 /etc/alternatives/java -> /usr/lib/jvm/jdk1.6.0_31/jre/bin/java
view raw gistfile1.sh hosted with ❤ by GitHub

This means that our Debian package needs to extract the JDK distribution and create the symlinks (also remove them first if there's an older version of JDK already installed, as the symlinks will be pointing to it).

Lest create our packaging infrastructure:

[root@server1 ~] cd /usr/src
[root@server1 ~] mkdir -p sun-java6-jdk/usr/lib/jvm/
[root@server1 ~] cp -rv /usr/src/jdk1.6.0_31/ sun-java6-jdk/usr/lib/jvm/
[root@server1 ~] mkdir sun-java6-jdk/DEBIAN
view raw gistfile1.sh hosted with ❤ by GitHub

The DEBIAN directory will contain the two main files that tell dpkg-deb how to build the package and what scripts to execute if any:

[root@server1 ~] cat sun-java6-jdk/DEBIAN/control
Package: jdk-6u31-linux-x64
Version: 1.6.0.31
Priority: optional
Architecture: all
Maintainer: Your Name
Description: Oracle JDK 1.6.0_31
[root@server1 ~] cat sun-java6-jdk/DEBIAN/postinst
#!/bin/bash
rm /usr/bin/java
rm /etc/alternatives/java
ln -s /usr/lib/jvm/jdk1.6.0_31/jre/bin/java /etc/alternatives/java
ln -s /etc/alternatives/java /usr/bin/java
view raw gistfile1.sh hosted with ❤ by GitHub

Change the permissions of the postinst file, or dpkg-deb will complain:
[root@server1 ~] chmod 0755 sun-java6-jdk/DEBIAN/postinst
view raw gistfile1.sh hosted with ❤ by GitHub

Now that we have everything in place let's build the package:

[root@server1 ~] dpkg-deb --build sun-java6-jdk
view raw gistfile1.sh hosted with ❤ by GitHub

To install the package run:

[root@server1 ~] dpkg -i sun-java6-jdk.deb
view raw gistfile1.sh hosted with ❤ by GitHub

This will extract the files to /usr/lib/jvm, and create the symlinks specified in the postinst script.