在 Windows 上为 Linux CentOS 构建 RPM

Build RPM for Linux CentOS on Windows

我正在尝试在 Windows 上使用 RPM Maven 插件构建一个 RPM,以便在 Linux 机器上部署。

OS of machine that's building RPM: Windows 7 Enterprise, Cygwin 2.5.1

OS of VM installing RPM: CentOS 7

我已经 运行 mvn clean install 命令并且构建成功。

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 48.273s
[INFO] Finished at: Sun Apr 24 21:08:51 BST 2016
[INFO] Final Memory: 10M/157M
[INFO] ------------------------------------------------------------------------

但是,当我将其 scp 到服务器并且 运行 yum install -y Core1337-1.0-1.noarch.rpm

我收到以下错误:

Transaction check error:   package Core1337-1.0-1.noarch is
 intended for a different operating system

以下是从插件的 Pom.XML 文件中截取的 XML。

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.core</groupId>
    <artifactId>Core1337</artifactId>
    <packaging>rpm</packaging>
    <version>1.0</version>
    <name>Core Libz</name>

    <properties>
        <targetOS>i386-linux</targetOS>
        <rpm-base>opt</rpm-base>
        <rpm-path>app</rpm-path>
        <base-dir>core</base-dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
    </dependencies>

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>rpm-maven-plugin</artifactId>
            <version>2.1.5</version>
            <extensions>true</extensions>
            <configuration>
                <license>GPL (c) 2005, SWWDC</license>
                <distribution>eSQuireDistro</distribution>
                <group>eSQuireDevel</group>
                <requires>
                    <require>at</require>
                    <require>wget</require>
                    <require>tree</require>
                    <require>cronie</require>
                    <require>openssh-server</require>
                </requires>
                <mappings>
                    <mapping>
                        <directory>/${rpm-base}/${rpm-path}/${base-dir}/python</directory>
                        <filemode>744</filemode>
                        <username>root</username>
                        <groupname>root</groupname>
                        <configuration>false</configuration>
                        <directoryIncluded>true</directoryIncluded>
                        <recurseDirectories>true</recurseDirectories>
                        <dependency />
                        <sources>
                            <source>
                                <location>src/main/python</location>
                            </source>
                        </sources>
                    </mapping>
                    <mapping>
                        <directory>/${rpm-base}/${rpm-path}/${base-dir}/scripts</directory>
                        <filemode>744</filemode>
                        <username>root</username>
                        <groupname>root</groupname>
                        <configuration>false</configuration>
                        <directoryIncluded>true</directoryIncluded>
                        <recurseDirectories>true</recurseDirectories>
                        <dependency />
                        <sources>
                            <source>
                                <location>src/main/scripts</location>
                            </source>
                        </sources>
                    </mapping>
                </mappings>
                <preinstallScriptlet>
                    <script>/bin/logger "installing"</script>
                </preinstallScriptlet>
                <postinstallScriptlet>
                    <script>/bin/logger "uninstalling"</script>
                </postinstallScriptlet>
            </configuration>
        </plugin>
    </plugins>

这是生成的 SPEC 文件:

%define __jar_repack 0
Name: Core1337
Version: 1.0
Release: 1
Summary: Core Libz
License: GPL (c) 2005, SWWDC
Distribution: eSQuireDistro
Group: eSQuireDevel
Requires: at
Requires: wget
Requires: tree
Requires: cronie
Requires: openssh-server
autoprov: yes
autoreq: yes
BuildArch: noarch
BuildRoot: /cygdrive/C/Users/ala/workspace/alan/Core/target/rpm/Core1337/buildroot

%description

%install

if [ -d $RPM_BUILD_ROOT ];
then
  mv /cygdrive/C/Users/ala/workspace/alan/Core/target/rpm/Core1337/tmp-buildroot/* $RPM_BUILD_ROOT
else
  mv /cygdrive/C/Users/ala/workspace/alan/Core/target/rpm/Core1337/tmp-buildroot $RPM_BUILD_ROOT
fi

%files

%attr(744,root,root)  "/opt/esquire/core/python/"
%attr(744,root,root)  "/opt/esquire/core/scripts/"

%pre
/bin/logger "installing"

%post
/bin/logger "uninstalling"

如何配置插件以使用 linux header 构建 RPM?非常感谢任何帮助!

在 windows 上构建 RPMS 以在 linux 机器上部署可能很复杂。我建议在将要部署的 OS 上构建 RPMS。

直接使用 rpmbuild 命令,可以使用 --target 选项覆盖目标 OS。

rpmbuild -bb hello.spec --target x86_64-linux

验证:

rpm -qp --qf '%{os}\n' hello-1-1.x86_64.rpm 
linux