代码之家  ›  专栏  ›  技术社区  ›  Ryan Southcliff

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

  •  0
  • Ryan Southcliff  · 技术社区  · 6 年前

    我有一个.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包时将my exe.exe文件放入VS项目,但在生成时不会复制该文件。我想要的是在构建时将该文件与我的其他应用程序文件一起安装,并将其保留在我的VS项目之外。

    我一直在看书 docs here 但不知道如何制作nuspec文件。

    更多细节:

    核4.5.1

    注:在 <files> <contentFiles> 似乎在复制功能。我想两者兼用,因为我知道这将是VS2017的未来证明

    1 回复  |  直到 6 年前
        1
  •  0
  •   Leo Liu    6 年前

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

    例如, <contentFiles> 用于 +与 ,它们都不受 视觉工作室2015 Using the contentFiles element for content files 一些细节。

    如果你对 <内容文件> ,您可以阅读博客 NuGet is now fully integrated into MSBuild

    现在回到我们的问题,根据上面的信息,我们不应该使用 <内容文件> 当我们使用Visual Studio 2015时。要解决这个问题,我们需要添加 .targets 生成项目时在nuget包中的文件:

    .目标 文件:

    <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

    另外,如果你想复制文件 到其他目的地,可以替换 .目标

    <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 为了一些帮助。

    希望这有帮助。