ANT build Debian Package

本文介绍如何使用Apache Ant构建Nginx Debian安装包。通过配置Ant任务来自动化创建系统初始化脚本、Debian控制文件等,并利用dpkg工具打包成deb文件。适合希望了解自动化部署流程的开发者。
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}" failonerror="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.youkuaiyun.com/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/
### 关于 `dpkg-buildpackage` 错误的解决方案 在使用 `dpkg-buildpackage` 构建 Debian 包时,如果出现 `debian/rules binary` 子进程退出状态为 2 的错误,通常表示构建过程中某个步骤未能成功完成。以下是可能的原因及解决方法: #### 1. 检查 `debian/rules` 文件 `debian/rules` 是一个 Makefile 样式的脚本,定义了如何构建软件包。如果该文件中的命令失败,则会导致整个构建过程终止并返回错误代码 2。需要检查以下内容: - 确保 `debian/rules` 文件语法正确,并且所有依赖项已安装。 - 验证是否所有必需的工具和库都已安装。例如,某些构建可能需要特定版本的编译器或链接器[^1]。 ```bash # 检查 debian/rules 文件是否存在语法错误 make -f debian/rules binary ``` #### 2. 缺少必要的文件或目录 构建过程中可能需要某些生成的文件(如 `System.map` 或 `vmlinux`),如果这些文件不存在,则会导致错误。例如,在内核构建中,如果未正确执行第三步,则可能导致缺少关键文件[^1]。 ```bash # 确保所有必需的文件存在 ls -l arch/x86/boot/bzImage System.map vmlinux ``` 如果这些文件缺失,请重新运行构建命令以确保所有阶段完成。 #### 3. 检查构建日志 构建过程中生成的日志文件可以提供有关错误的详细信息。通常,日志会指出具体的失败原因。可以通过以下方式查看日志: ```bash # 查看最近的构建日志 less debian/*.log ``` #### 4. 验证依赖关系 如果构建过程中报告缺少依赖项,则需要安装这些依赖项。可以使用 `debuild` 工具自动检测并安装所需的依赖项。 ```bash # 安装 debuild 并自动处理依赖关系 sudo apt-get install devscripts debuild -d -us -uc ``` #### 5. 检查权限问题 某些构建步骤可能需要超级用户权限。如果权限不足,可能会导致错误。可以尝试使用 `fakeroot` 来模拟 root 权限。 ```bash # 使用 fakeroot 运行 dpkg-buildpackage fakeroot dpkg-buildpackage -us -uc ``` #### 6. 清理构建环境 有时,旧的构建残留物可能导致冲突。清理构建环境可以避免此类问题。 ```bash # 清理构建目录 make clean ``` #### 7. 更新工具链 确保使用的工具链是最新的。过时的工具可能导致不兼容问题。 ```bash # 更新系统和构建工具 sudo apt-get update && sudo apt-get upgrade ``` --- ### 示例代码:调试 `debian/rules` 以下是一个简单的 `debian/rules` 文件示例,展示如何定义构建规则: ```makefile #!/usr/bin/make -f # -*- makefile -*- %: dh $@ --with python3 override_dh_auto_build: python3 setup.py build override_dh_auto_test: python3 setup.py test ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值