【问题标题】:Nuget: Including an exe as a Run-time dependencyNuget:包括一个 exe 作为运行时依赖项
【发布时间】:2018-08-14 12:58:23
【问题描述】:

我有一个 .exe 应用程序,我需要在构建时将它与我的 C# 应用程序一起分发。我正在尝试使用 Nuget 对其进行打包,以便在构建时将其包含在构建根目录中,但无法获得我想要的行为。

这是我在.nuspec 文件中的内容:

<?xml version="1.0"?>
<package>
  <metadata>
    <id>my.id</id>
    <version>1.0.0</version>
    <authors>me</authors>
    <owners>me</owners>
    <licenseUrl>myurl</licenseUrl>
    <projectUrl>myurl</projectUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>A copy of an .exe so we can easily distribute it 
       with our applications without needing to include it in our VCS repo</description>
    <releaseNotes>Initial test version</releaseNotes>
    <copyright>Copyright 2018</copyright>
    <dependencies>
    </dependencies>
    <packageTypes>
    </packageTypes>
    <contentFiles>
        <files include="any\any\myexe.exe" buildAction="None" copyToOutput="true" />
    </contentFiles>
  </metadata>
  <files>
    <file src="content\myexe.exe" target="content" />
  </files>
</package>

这会在我安装 Nuget 包时将 myexe.exe 文件放入我的 VS 项目,但在我构建时它不会复制该文件。我想要的是在构建时将该文件与我的其他应用程序文件一起安装,并将其排除在我的 VS 项目之外。

我一直在阅读docs here,但不确定如何制作 nuspec 文件。

更多详情:

Nuget 4.5.1

Visual Studio 2015

注意:&lt;files&gt; 和 &lt;contentFiles&gt; 可能看起来是重复的功能。我想同时使用两者,因为我知道这将在 VS2017 中适应未来

【问题讨论】:

    标签: c# visual-studio nuget


    【解决方案1】:

    Nuget:包含一个 exe 作为运行时依赖项

    首先,我知道你想为未来使用一些技术,但我们要知道,这些面向未来的技术往往有一定的限制和条件。

    例如,&lt;contentFiles&gt; 用于带有 PackageReference 的 NuGet 4.0+,Visual Studio 2015 都不支持它们。有关详细信息,请参阅Using the contentFiles element for content files。

    如果您对&lt;contentFiles&gt;感兴趣,可以阅读博客NuGet is now fully integrated into MSBuild。

    现在回到我们的问题,根据上面的信息,我们在使用Visual Studio 2015时不应该使用&lt;contentFiles&gt;。为了解决这个问题,我们需要在构建时在nuget包中添加一个.targets文件项目:

    .targets文件的内容:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <ItemGroup>
        <None Include="$(ProjectDir)myexe.exe">
          <Link>myexe.exe</Link>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CustomToolNamespace></CustomToolNamespace>
        </None>
      </ItemGroup>
    </Project>
    

    .nuspec 文件如下:

      <files>
        <file src="build\YouNuGetPackageName.targets" target="build\YouNuGetPackageName.targets" />
        <file src="content\myexe.exe" target="content\myexe.exe" />
      </files>
    

    注意: .targets 文件的名称应与您的 nuget 包名称相同。

    通过这种方式,当您构建项目时,MSBuild/VS 会将文件 myexe.exe 复制到输出文件夹。

    另外,如果要将文件myexe.exe复制到其他目的地,可以将.targets文件的内容替换为复制任务,例如:

    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
      <Target Name="CopyMyexe" BeforeTargets="Build">
      <Message Text="Copy CopyMyexe to the folder."></Message>
      <Copy
      SourceFiles="$(ProjectDir)myexe.exe"
      DestinationFolder="xxx\xxx\xx\myexe.exe"
    />
      </Target>
    </Project>
    

    请参阅 Creating native packages 和 similar issue 以获得一些帮助。

    希望这会有所帮助。

    【讨论】:

    • 很好的答案。 +1。这行得通,我现在正在阅读以更好地理解它。但是有一个问题:如果可能的话,我想将 myexe.exe 排除在我的 VS 项目之外。理想的过程是,当我安装此 Nuget 包时,myexe.exe 不会添加到项目中。只有当我构建文件时才会添加到构建文件夹中。我的想法是,有了这个,我不需要将 .exe 文件保存在我们的版本控制中,但可以相信,如果同事因为 Nuget 引用而拉下我的仓库,它将自动包含在内。也许content 是错误的文件夹类型?
    • @RyanSouthcliff,是的,您应该使用 Tools 类型的文件夹。这就是你想要的。此外,由于上述答案解决了您的问题,您可以接受它作为答案,这将有利于其他有相同问题的社区成员阅读此主题并易于找到答案。
    猜你喜欢
    • 2017-09-24
    • 1970-01-01
    • 2019-03-19
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多