【发布时间】:2018-12-13 13:25:17
【问题描述】:
我正在尝试迁移到使用 MSBuild Pack 支持使用 .csproj 生成项目 NuGet 包,在开发过程中本地 .dll 用于构建项目,但它们需要被替换/使用 MSBuild “打包”项目时,交换为在生成的 .nuspec 中引用外部 NuGet 包。
我能找到的与此用例最接近的记录示例是 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 构建,这些本地 .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 中操作 XML?
基本上,我只对在“pack”目标运行时注入依赖项感兴趣,因此它在所有其他目标中都被忽略/惰性。
【问题讨论】: