簡體   English   中英

如何在生成的 .nuspec 的 MSBuild NuGet 包中注入自定義依賴項

[英]How to inject a custom dependency in an MSBuild NuGet pack generated .nuspec

我正在嘗試遷移到使用 MSBuild Pack支持使用 .csproj 生成項目 NuGet 包,其中在開發過程中使用本地 .dll 來構建項目,但需要替換/交換它們以引用生成的外部 NuGet 包.nu​​spec 使用 MSBuild 來“打包”項目時。

我能找到的最接近此用例的文檔示例是從還原圖替換一個庫,它建議您可以替換外部 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”目標時注入依賴項感興趣,因此它在所有其他目標中被忽略/惰性。

有趣的問題,當我四處尋找答案時,我發現了Stackoverflow 中的另一個討論

在其中一條評論中 - 有跡象表明 pack 命令僅適用於 4.6 及更高版本的框架項目 - 而不是 4.5。

根據微軟的這篇博客文章,.Net Standard 2.0 必須匹配 4.6.1

您現在可能已經找到了另一個解決方案,但我通過向 csproj 添加了一些自定義目標來“成功”做到了這一點。 解決方案確實讓我覺得也許我不應該這樣做。

步驟是

  • 在包生成目標之前注入一個步驟,禁用 nupkg 生成
  • 在包生成目標后注入修改生成的nuspec文件的step,再次調用包生成目標。

我在下面修改了我的修復程序,因此它注入了帖子中提到的依賴項。 我在這里禁用了驗證(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 項目使用,以防有人知道更簡單的方法來做到這一點)。

<?xml version="1.0"?>
<package >
<metadata>
<id>FIK.DAL</id>
<version>$version$</version>
<title>FIK.DAL .NET</title>
<authors>$author$</authors>
<owners>Imamul Karim</owners>
<license type="expression">MIT</license>
<projectUrl>https://github.com/imamulkarim/FIK.DAL</projectUrl>

<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A Simple SQL Query Generator Using ADO.NET Provider with SqlClient</description>
<releaseNotes>SQLite Library Bug</releaseNotes>
<copyright>$copyright$</copyright>
<tags>ADO.NET SQL QUERY Generator</tags>
</metadata>
<files>
<file src="bin\Debug\System.Data.SQLite.DLL" target="lib\net40"></file>
</files>
</package>

https://docs.microsoft.com/en-us/nuget/reference/nuspec

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM