Nuget:将exe包含为运行时依赖项

时间:2018-08-14 12:58:24

标签: c# visual-studio nuget

我有一个.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

注意:<files><contentFiles>似乎在复制功能。我想同时使用这两种软件,因为我知道这将在VS2017中证明它的未来发展

1 个答案:

答案 0 :(得分:0)

  

Nuget:将exe作为运行时依赖项

首先,我知道您想在将来使用某些技术,但是我们必须知道,这些面向未来的技术通常具有某些约束和条件。

例如,<contentFiles>用于带有 PackageReference NuGet 4.0 +, Visual Studio 2015 都不支持它们。有关详细信息,请参见Using the contentFiles element for content files

如果您对<contentFiles>感兴趣,可以阅读博客NuGet is now fully integrated into MSBuild

现在回到我们的问题,根据上述信息,当我们使用Visual Studio 2015时,我们不应该使用<contentFiles>。要解决这个问题,我们需要在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 packagessimilar issue以获得一些帮助。

希望这会有所帮助。

相关问题