如何在 MSBuild NuGet 包生成的 .nuspec 中注入自定义依赖项

2024-05-08

我正在尝试迁移到使用 MSBuildPack支持使用 .csproj 生成项目 NuGet 包,其中在开发过程中使用本地 .dll 来构建项目,但在使用 MSBuild“打包”项目时需要替换/交换它们以引用生成的 .nuspec 中的外部 NuGet 包项目。

我能找到的最接近该用例的记录示例是从恢复图中替换一个库 https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#replacing-one-library-from-a-restore-graph它建议您可以替换外部 NuGet 引用:

<PackageReference Include="Newtonsoft.Json" Version="9.0.1">
  <ExcludeAssets>All</ExcludeAssets>
</PackageReference>

它覆盖包以引用本地.dll反而:

<Reference Include="Newtonsoft.Json.dll" />

我正在尝试做类似的事情,该项目应该针对本地构建.dll's作为依赖 TeamCity/CI 构建的工件注入:

<Reference Include="..\..\lib\net45\ServiceStack.Interfaces.dll" />
<Reference Include="..\..\lib\net45\ServiceStack.Text.dll" />
<Reference Include="..\..\lib\net45\ServiceStack.Common.dll" />

但使用时ExcludeAssets=All根据文档:

<PackageReference Include="ServiceStack.Common" Version="5.0.0">
  <ExcludeAssets>All</ExcludeAssets>
</PackageReference>

PackageReference 不会在生成的依赖项列表中导出,例如:

<group targetFramework=".NETFramework4.5" />

我最接近获得首选行为的是使用ExcludeAssets="compile"所以我的本地项目不会针对它构建,除了此行为也会导出到 .nuspec 中:

<group targetFramework=".NETFramework4.5">
  <dependency id="ServiceStack.Common" version="5.4.0" exclude="Compile,Build,Analyzers" />
</group>

我只想避免在本地构建它,但将其作为正常依赖项导出。这种方法的另一个问题是我需要它引用尚未发布到 NuGet 的包(因为所有包都是锁定步骤发布的),例如:

<!-- v5.5.0 is the new version to publish -->
<PackageReference Include="ServiceStack.Common" Version="5.5.0" ExcludeAssets="compile"/>

构建失败,因为找不到未发布的包:

[NU1102] Unable to find package ServiceStack.Common with version (>= 5.5.0)

实际上,我需要某种方式在生成的 .nuspec 中“注入”我需要的确切依赖项和版本,以便将其包含在生成的 .nuspec 依赖项列表中,例如:

<group targetFramework="net45">
  <dependency id="ServiceStack.Common" version="5.5.0" />
</group>
<group targetFramework=".netstandard2.0">
  <dependency id="ServiceStack.Common" version="5.5.0" />
</group>

我们如何在 MSBuild 生成的 .nuspec 中注入自定义依赖项?

有什么方法可以手动声明<dependency/>如上所述,所以它仅在 MSBuild/NuGet 打包项目时使用?否则,有没有办法对生成的 .nuspec 应用一些 XML 转换,以便我可以在打包/压缩 .nuspec 之前操作 .nuspec 中的 XML?

基本上我只对运行“pack”目标时注入依赖项感兴趣,因此它在所有其他目标中被忽略/惰性。


您现在可能已经找到了另一种解决方案,但我通过向 csproj 添加一些自定义目标“成功”做到了这一点。这个解决方案确实让我觉得也许我不应该这样做。

步骤是

  • 在包生成目标之前注入一个步骤,禁用 nupkg 生成
  • 在包生成目标之后插入一个步骤,修改生成的 nuspec 文件,并再次调用包生成目标。

我修改了下面的修复程序,以便它注入帖子中提到的依赖项。我在这里禁用了验证(NoPackageAnalysis=true)。您可以将其放入 .target 文件中并从 csproj 导入。

<Project>

  <!-- Disable nupkg generation before running pack -->
  <Target Name="__DisablePacking" BeforeTargets="GenerateNuspec" Condition="$(NuspecFile) == ''">
    <PropertyGroup>
      <ContinuePackingAfterGeneratingNuspec>false</ContinuePackingAfterGeneratingNuspec>
    </PropertyGroup>
  </Target>

  <!-- Modify the generated nuspec file and rerun the pack target -->
  <Target Name="__EnablePackingAndInjectDependencies" AfterTargets="Pack" Condition="$(NuspecFile) == ''">
    <!-- Get the nuspec file name -->
    <PropertyGroup>
      <_NugetPackOutputAsProperty>@(NuGetPackOutput)</_NugetPackOutputAsProperty>
    </PropertyGroup>
    <ItemGroup>
      <_NugetPackOutputAsItem Remove="@(_NugetPackOutputAsItem)"/>
      <_NugetPackOutputAsItem Include="$(_NugetPackOutputAsProperty.Split(';'))" />
    </ItemGroup>
    <PropertyGroup>
      <__NuspecFileName>%(_NugetPackOutputAsItem.Identity)</__NuspecFileName>
    </PropertyGroup>

    <!-- Create an updated dependencies, with the net46 dependencies copied to a native group -->
    <PropertyGroup>
      <__NuSpecUpdatedDependencies>
        <group targetFramework="net45">
          <dependency id="ServiceStack.Common" version="5.5.0" />
        </group>
        <group targetFramework=".netstandard2.0">
          <dependency id="ServiceStack.Common" version="5.5.0" />
        </group>
      </__NuSpecUpdatedDependencies>
    </PropertyGroup>

    <!-- Poke them back in -->
    <XmlPoke XmlInputPath="$(__NuspecFileName)"
             Value="$(__NuSpecUpdatedDependencies)"
             Query="/n:package/n:metadata/n:dependencies"
             Namespaces="&lt;Namespace Prefix='n' Uri='http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd' /&gt;">
    </XmlPoke>

    <!-- call the pack operation again -->
    <PropertyGroup>
      <ContinuePackingAfterGeneratingNuspec>true</ContinuePackingAfterGeneratingNuspec>
    </PropertyGroup>

    <Msbuild
      Projects="$(MSBuildProjectFullPath)"
      Targets="Pack"
      Properties="NuspecFile=$(__NuspecFileName);NoPackageAnalysis=true">
    </Msbuild>
  </Target>
</Project>

(我希望注入一个 native0.0 框架依赖项,以便我的包可以被 c++/cli 项目使用,以防有人知道更简单的方法来做到这一点)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 MSBuild NuGet 包生成的 .nuspec 中注入自定义依赖项 的相关文章

随机推荐