用java调用rpmbuild 报错,同一条命令直接复制到终端却能运行

发布于 2022-09-05 14:59:30 字数 4051 浏览 16 评论 0

用java调用rpmbuild 报错,同一条命令直接复制到终端却能运行。
命令如下:

rpmbuild --define "_topdir /var/lib/jenkins/workspace/compositor-build-CI/target/rpmbuild/compositor" --define "service_desc compositor" --define "service_name compositor" --define "service_release CI.last" -ba SPECS/rpmbuild.spec --clean --target i686-redhat-linux

java调用代码如下:

String rpmBuildScript = String.format("rpmbuild --define \"_topdir %s\" --define \"service_desc %s\" --define \"service_name %s\" --define \"service_release %s\" -ba SPECS/rpmbuild.spec --clean --target i686-redhat-linux", new File(privateRpmBuild).getAbsolutePath(), serviceName, serviceName, version);
getLog().info("RPM build command: " + rpmBuildScript);
Process exec = Runtime.getRuntime().exec(rpmBuildScript, null, new File(privateRpmBuild));
exec.waitFor();
BufferedReader in = new BufferedReader(new InputStreamReader(exec.getErrorStream()));
String line;
while ((line = in.readLine()) != null) {
    getLog().info(line);
}

错误输出:

[INFO] RPM build command: rpmbuild --define "_topdir /var/lib/jenkins/workspace/compositor-build-CI/target/rpmbuild/compositor" --define "service_desc compositor" --define "service_name compositor" --define "service_release CI.last" -ba SPECS/rpmbuild.spec --clean --target i686-redhat-linux
[INFO] error: Macro % has illegal name (%define)
[INFO] error: Macro % has illegal name (%define)
[INFO] error: Macro % has illegal name (%define)
[INFO] error: Macro % has illegal name (%define)
[INFO] error: Macro % has illegal name (%define)
[INFO] error: Macro % has illegal name (%define)
[INFO] error: Macro % has illegal name (%define)
[INFO] error: Macro % has illegal name (%define)
[INFO] error: Macro % has illegal name (%define)
[INFO] error: failed to stat /var/lib/jenkins/workspace/compositor-build-CI/target/rpmbuild/compositor": No such file or directory

其中的"error: failed to stat /var/lib/jenkins/workspace/compositor-build-CI/target/rpmbuild/compositor": No such file or directory",这条报错语句有点诡异,我直接用Jenkins用户进去能找到这个目录,并且复制rpmbuild命令并执行能生成正确的rpm文件。

附上rpmbuild.spec文件

%define _builddir       %{_topdir}/BUILD
%define _rpmdir         %{_topdir}/RPMS
%define _sourcedir      %{_topdir}/SOURCES
%define _specdir        %{_topdir}/SPECS
%define _srcrpmdir      %{_topdir}/SRPMS
%define _buildrootdir   %{_topdir}/BUILDROOT

Summary:  %{service_desc}
Name:   %{service_name}
Version:  1.0
Release: %{service_release}
License: Restricted
Group:  Applications/System
Packager: Alain
BuildRoot: %{_buildrootdir}/%{name}-%{version}_%{release}
BuildArchitectures: noarch

%description
This is a sample SPEC file for the RPM project
demonstrating how to build, package, install(deploy)


%prep
# extract the tar file containing all the sources, to the build directory.
echo service_desc = %{service_desc}
echo service_name = %{service_name}
echo service_release = %{service_release}
echo _builddir = %{_builddir}
echo _rpmdir = %{_rpmdir}
echo _sourcedir = %{_sourcedir}
echo _specdir = %{_specdir}
echo _srcrpmdir = %{_srcrpmdir}
echo _buildrootdir = %{_buildrootdir}


%build
echo "Building the project..."


%install
# This is the hierarchy which is going to be inside the package (RPM/Deb) eventually.
echo "Install phase..."
echo %{_topdir}
echo %{buildroot}
mkdir -p %{buildroot}/services/apps
mv %{_builddir}/tar_expand_temp_dir/%{name} %{buildroot}/services/apps


%post
#This runs post the install - maybe you want to execute the application already then
echo "Post install.."
echo "add service.."
cp /services/apps/%{name}/bin/%{name} /etc/init.d
chkconfig --add %{name}
echo "add service succeed"


%postun
#this runs after the uninstall
echo "Post Uninstall..."
echo "remove service"
chkconfig --del %{name}
rm /etc/init.d/%{name}


%files
# tells which files to contain in the package and with what access rights
# the triplet contains of (<file mode>, <user>, <group>). Make the necessary changes.
%defattr(-,track,track)
/*


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2022-09-12 14:59:30

悟了.

String[] command={"/bin/bash", "-c", rpmBuildScript};
Process exec = Runtime.getRuntime().exec(command, null, new File(privateRpmBuild));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文