如何使用 NuGet 打包单个 EXE

How to package a single EXE with NuGet

我有一个项目需要的大型 EXE,但我不想将大型 EXE 提交到 Git。因此,在我看来,一个可行的选择是通过 NuGet 将其引入。很简单,我创建了一个 .nuspec 文件,其中包含我的一个 EXE:

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <id>Traefik</id>
        <version>1.5.1</version>
        <authors>Containous</authors>
        <owners>MikeC</owners>
        <projectUrl>https://github.com/containous/traefik</projectUrl>
        <license type="expression">MIT</license>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>Traefik binaries</description>
        <copyright>Copyright ©2020 Containous</copyright>
    </metadata>

    <files>
        <file src="traefik.exe" target="lib" />
    </files>
</package>

然后我 运行 nuget pack 并创建一个 .nupkg 文件。一切正确:

但是,当我尝试在我的项目中引用我的 NuGet 包时,单击“安装”按钮时出现以下错误:

Severity    Code    Description Project File    Line    Suppression State
Error       Could not install package 'Traefik 1.5.1'. You are trying to install this package into a project that targets 'Unsupported,Version=v0.0', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.              

这是一个 NuGet 包,没有 程序集,也没有任何参考。它只是我想使用 post 构建操作拉入并复制到某处的内容。是否可以创建一个 NuGet 包,它没有可引用的程序集并且是纯内容(在本例中为单个 EXE 文件)?如果是这样,在文件结构中放置它的正确方法是什么?我已经尝试将 EXE 放在几个地方,例如“bin”、“tools”、“content”等。无论如何都会出现同样的错误。谢谢!

lib 文件夹不适合这个,因为它用于安装程序包时您的项目 references 的程序集。除此之外,文件夹结构必须包含一个 suitable target framework 文件夹,如 net472。您不能将程序集直接放在 lib.

<file src="traefik.exe" target="lib/net472"/>

如果你想把你的可执行文件当作一个纯内容文件,你可以使用contentFiles.

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <id>Traefik</id>
        <version>1.5.1</version>
        <authors>Containous</authors>
        <owners>MikeC</owners>
        <projectUrl>https://github.com/containous/traefik</projectUrl>
        <license type="expression">MIT</license>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>Traefik binaries</description>
        <copyright>Copyright ©2020 Containous</copyright>
        <contentFiles>
            <files include="any/any/traefik.exe" buildAction="None" copyToOutput="true"/>
        </contentFiles>
    </metadata>
    <files>
        <file src="traefik.exe" target="contentFiles/any/any/traefik.exe"/>
        <file src="traefik.exe" target="content/traefik.exe"/>
    </files>
</package>

这样,traefik.exe 将直接复制到您的输出文件夹。在你的包中,文件夹结构将是这样的。此文件夹结构是为内容文件预定义的。

content
    - traefik.exe
contentFiles
    - any
        - any
            - traefik.exe

您甚至可以使用这种方法针对一个或多个不同的框架。假设您有一个用于 .NET Framework 4.7.2 的特殊可执行文件,然后将第二个 any 调整为您的 target framework moniker,此处为 net472。如果框架与您的项目框架匹​​配,则可执行文件将仅复制到您的输出文件夹。

<files include="any/net472/traefik.exe" buildAction="None" copyToOutput="true" flatten="true"/>
<file src="traefik.exe" target="contentFiles/any/net472/traefik.exe"/>

当然,你可以像这样为不同的框架定义多个可执行文件。关于 NuSpec 文件的几点说明。可执行文件也被复制到包的 content 文件夹以实现向后兼容性,因为仅自 NuGet 4.0 使用 PackageReference 后才支持 contentFiles。 =26=]