ANT build Debian Package

309 篇文章 0 订阅
ANT build Debian Package

Create a Repo https://github.com/luohuazju/ant-debian-deploy

On my Virtual Machine
> ant -version
Apache Ant(TM) version 1.8.2 compiled on December 3 2011

Host Machine
> ant -version
Apache Ant(TM) version 1.10.3 compiled on March 24 2018

Current nginx is installed on /usr/local/nginx-1.11.9

I add this nginx file to /etc/init.d to enable the nginx service
#!/bin/bash

### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO

# An init.d script that works under Ubuntu 12.04 FFS
# load with sudo /usr/sbin/update-rc.d -f nginx defaults

# CHANGE ME.
PATH=/usr/local/nginx-1.11.9/sbin:$PATH
DAEMON=/usr/local/nginx-1.11.9/sbin/nginx
NAME=nginx
DESC=nginx-1.11.9

test -x $DAEMON || exit 0

set -e

function start {
echo -n "Starting $DESC: "
start-stop-daemon --make --start --pidfile /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
}

function stop {
echo -n "Stopping $DESC: "
$DAEMON -s stop
echo "$NAME."
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart}" >&2
exit 1
;;
esac

exit 0

Enable the permission
> sudo chmod a+x /etc/init.d/nginx

Then we can stop and start the nginx with our service command
> sudo service nginx stop

And then run this to enable the service command, it will add the service to the startup script
> sudo /usr/sbin/update-rc.d -f nginx defaults

Remove from the service
> sudo /usr/sbin/update-rc.d -f nginx remove

Debian Information
DEBIAN/control - basic information about the Service

DEBIAN/preinst - operations before installation, for example, stop the service first

DEBIAN/postinst - operations after installation, for example, start the service

Sample project is named as ant-debian-deploy.
The ANT task configuration build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="help" name="Sillycat Debian">

<property name="build.dir" value="build" />
<property name="sys.dir" value="${build.dir}/nginx/etc/init.d" />
<property name="debian.dir" value="${build.dir}/nginx/DEBIAN" />
<property name="app.dir" value="${build.dir}/nginx/usr/local" />
<property name="build.deb" value="${build.dir}/build-deb" />
<property name="package.name" value="nginx-1.11.9.deb" />
<property name="debian.src" value="${build.dir}/nginx" />
<property name="build.output.dir" value="dist" />
<property name="nginx.dist" value="install" />

<target name="clean">
<echo message="Cleaning dist, package and modules folders" />
<delete dir="${build.dir}" quiet="true" />
</target>

<target name="build" depends="clean">
<!-- SYSTEM -->
<copy todir="${sys.dir}">
<fileset dir="debian/sys" />
</copy>
<!-- DEBIAN -->
<copy todir="${debian.dir}">
<fileset dir="debian/DEBIAN" />
</copy>
<!-- Application -->
<mkdir dir="${app.dir}" />
<gunzip src="${nginx.dist}/nginx-1.11.9-bin.tar.gz" dest="${nginx.dist}"/>
<untar src="${nginx.dist}/nginx-1.11.9-bin.tar" dest="${app.dir}"/>
<!-- Permission -->
<chmod file="${debian.dir}/postinst" perm="775"/>
<chmod file="${debian.dir}/preinst" perm="775"/>
<chmod file="${app.dir}/nginx-1.11.9/sbin/nginx" perm="777"/>
<chmod file="${sys.dir}/nginx" perm="777"/>
</target>

<target name="package" description="Package .deb file" depends="build">
<mkdir dir="${build.deb}" />
<exec executable="fakeroot">
<arg value="dpkg" />
<arg value="-b" />
<arg value="${debian.src}" />
<arg value="${build.deb}/${package.name}" />
</exec>
</target>

<target name="release" description="Copy to dist" depends="package">
<delete dir="${build.output.dir}" />
<mkdir dir="${build.output.dir}" />

<echo message="Moving ${package.name} to ${build.output.dir}" />
<move file="${build.deb}/${package.name}" todir="${build.output.dir}" failοnerrοr="true" />
</target>

<target name="help">
<echo message=" =============================" />
<echo message=" Usage: " />
<echo message=" ant clean removes all the generated resources and classes" />
<echo message=" ant dist creates a ready-to-be-packaged build folder" />
<echo message=" ant package creates a DIP installer package" />
<echo message="=============================" />
</target>
</project>

Here is the steps to run the commands
# ant-debian-deploy

first find nginx-1.11.9-bin.tar.gz in the install directory

ant release to build the deb package

sudo dpkg -i dist/nginx-1.11.9.deb

In the debian/sys/nginx, that is the script to put into /etc/init.d/
#!/bin/bash

### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO

# An init.d script that works under Ubuntu 12.04 FFS
# load with sudo /usr/sbin/update-rc.d -f nginx defaults

# CHANGE ME.
PATH=/usr/local/nginx-1.11.9/sbin:$PATH
DAEMON=/usr/local/nginx-1.11.9/sbin/nginx
NAME=nginx
DESC=nginx-1.11.9

test -x $DAEMON || exit 0

set -e

function start {
echo -n "Starting $DESC: "
start-stop-daemon --make --start --pidfile /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
}

function stop {
echo -n "Stopping $DESC: "
$DAEMON -s stop
echo "$NAME."
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart}" >&2
exit 1
;;
esac

exit 0

In the debian/DEBIAN/control
Package: nginx
Version: 1.11.9
Section: web
Priority: optional
Architecture: all
Essential: no
Depends:
Pre-Depends:
Recommends: mozilla | netscape
Maintainer: Sillycat
Description: Sillycat Pure Nginx

In the debian/DEBIAN/postinst
#!/bin/sh

if [ -x "/etc/init.d/nginx" ]; then
update-rc.d nginx defaults >/dev/null
if [ -e /var/run/nginx.pid ]; then
echo "nginx is running, restarting...";
/etc/init.d/nginx restart
else
/etc/init.d/nginx start
fi
fi

In the debian/DEBIAN/preinst, it is empty right now.


References:
https://stackoverflow.com/questions/6583115/create-debian-package-using-apache-ant
https://blog.kghost.info/2011/02/11/%E4%BD%BF%E7%94%A8fakeroot%E6%A8%A1%E6%8B%9Froot%E6%9D%83%E9%99%90%E6%89%A7%E8%A1%8C%E7%A8%8B%E5%BA%8F/
https://gist.github.com/squarism/3400259
https://github.com/JasonGiedymin/nginx-init-ubuntu
Debian
https://www.debian.org/doc/manuals/debian-faq/ch-pkg_basics.zh-cn.html
https://blog.csdn.net/sjin_1314/article/details/17394327
http://rocksaying.tw/archives/11239791.html
https://09jianfeng.github.io/2016/01/11/dpkg-deb%E6%89%93%E5%8C%85%E4%B8%80%E4%B8%AA%E5%85%B7%E6%9C%89root%E6%9D%83%E9%99%90%E7%9A%84App/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值