How to create .deb Debian package for Java web application

Last updated on July 11, 2020 by Dan Nanni

Suppose you have built a Java web application as a Java servlet. Now you want to create .deb Debian package for the Java web application, such that when you install the .deb package file, it will automatically install any prerequisite packages (e.g., Java and Tomcat web server), and deploy the Java servlet on Apache Tomcat.

I assume that you already built and exported a Java servlet into .war file. Then the Debian package will simply consist of a .war blob, and any dependency information. Here is guide on how to create .deb package from .war file.

First, install any necessary packages to build a Debian package.

$ sudo apt-get install dh-make debhelper devscripts fakeroot

Create a directory for your application.

$ mkdir myapp-0.1

Next, copy your .war application file to the directory.

$ cp myapp.war myapp-0.1 
$ cd myapp-0.1

Create skeleton files needed for packaging.

$ dh_make -s --indep --createorig 
Maintainer name  : My Name
Email-Address    : [email protected] 
Date             : Tue, 02 Apr 2020 11:33:28 -0400
Package Name     : myapp
Version          : 0.1
License          : blank
Type of Package  : Independent
Hit  to confirm: 

Once you hit <enter>, it will create several skeleton files in myapp-0.1/debian/

Since you are packaging a pre-built .war file, you don't need any build process within packaging rules. Remove any reference to makefile from debian/rules.

$ grep -v makefile debian/rules > debian/rules.temp
$ mv debian/rules.temp debian/rules 

The next step is to modify debian/install. Here you specify a list of files to install, as well as their respective installation directories. In our example, files to install correspond to .war file, and installation directory is the webapps directory of Tomcat. Assuming that .war file was built for Tomcat7 web server, you modify debian/install as follows.

$ echo myapp.war /var/lib/tomcat7/webapps > debian/install

Modify debian/source/format as we will not be building a quilt based package.

$ echo "1.0" > debian/source/format 

Remove unnecessary example files.

$ rm debian/*.ex

Modify control information of your package. Importantly, you need to specify tomcat7 as dependent package (in "Depends:" field). You don't need to specify Java as dependent since Java dependency is implicitly implied by adding tomcat7 which in turn depends on Java.

$ vi debian/control
Source: myapp
Section: unknown
Priority: extra
Maintainer: My Name 
Build-Depends: debhelper (>= 8.0.0)
Standards-Version: 3.9.3
Homepage: 
#Vcs-Git: git://git.debian.org/collab-maint/indoornav.git
#Vcs-Browser: http://git.debian.org/?p=collab-maint/indoornav.git;a=summary

Package: myapp
Architecture: all
Depends: tomcat7
Description: This is my web application.
This is a long description of my web application.

Finally, build the package.

$ debuild -us -uc

After building is completed, you will have .deb file generated in the parent directory.

$ ls ../*deb
myapp_0.1-1_all.deb

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean