【问题标题】:Getting files/content/assets from nuget packages从 nuget 包中获取文件/内容/资产
【发布时间】:2020-06-05 08:25:36
【问题描述】:

当 Visual Studio 项目使用 时,如何访问包中的文件? 比如“Newtonsoft.Json”V12.0.3除了Newtonsoft.Json.Dll之外还有几个文件——比如newtonsoft.json\12.0.3\lib\net20\Newtonsoft.Json.xmlnewtonsoft.json\12.0.3\LICENSE.md

我找到了GeneratePathProperty - 这很好,但是在 VS2017 中构建 SDK 项目时它不起作用。 (在构建旧式框架项目时,它确实有效)。

<IncludeAssets> 似乎没有做任何事情。

我的 csproj 文件的一部分:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" GeneratePathProperty="true">
        <IncludeAssets>all</IncludeAssets>
    </PackageReference>

</Project>

现在,如果我从命令行构建 (msbuild.exe 15.9.21+g9802d43bc3) msbuild /t:Restore;Rebuild /p:Configuration=Release /p:Platform="Any CPU" MyProject.sln

MyProject.csproj.nuget.g.props 包含

  <PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
    <PkgNewtonsoft_Json Condition=" '$(PkgNewtonsoft_Json)' == '' ">%userprofile%\.nuget\packages\newtonsoft.json\12.0.3</PkgNewtonsoft_Json>
  </PropertyGroup>

这很棒,因为我可以根据需要在我的项目文件中使用 $(PkgNewtonsoft_Json) 来获取这些文件。

但是,当我从 VS2017 (V15.9.17) 内部构建时,我的 *PROPS 文件没有定义这个符号:-(

-------- 更新 --------

好的,重新启动并再次尝试后它起作用了:-) 我必须将 Targets="restore;build" 更改为 Targets="restore;" 否则会出错。给定 Lib1 和 Lib2,其中 Lib2 依赖于 Lib1,我收到此错误

Target CoreCompile:
  Using shared compilation with compiler from directory: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Roslyn
  CSC : error CS0006: Metadata file 'C:\src\...\build\Lib2\bin\Debug\netstandard2.0\Lib2.dll' could not be found
Done building target "CoreCompile" in project "Lib1.csproj" -- FAILED.

然后,在构建整个解决方案之后 - 它失败了,当我再次构建 Lib1 时它失败了

error MSB4006: There is a circular dependency in the target dependency graph involving target "Build".```



【问题讨论】:

  • 我认为这是msbuild -t:restorenuget -t:restore之间的恢复格式问题,这是VS IDE中的恢复格式。
  • 什么是“恢复格式”?我已经从 github.com/NuGet/Home/issues/4837 实现了“CopyPackages”。对我来说似乎有点难看,但至少有效。
  • 经过深入研究和测试,这是VS 2017 IDE中存在的问题,在最新的VS2019中已修复。
  • 我刚试过 - 它似乎没有做任何事情。使用 VS2019,解决方案按原样工作。使用 VS2017,您的修复似乎没有做任何事情 - 不起作用。
  • 1&gt;Target OnceBuild: 1&gt; Target ValidateSolutionConfiguration: 1&gt; Building solution configuration "Release|AnyCPU". 1&gt; Target Restore: 1&gt; C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets(114,5): warning : Unable to find a project to restore! 1&gt; Done building target "Restore" in project "MyProject.sln". 1&gt;

标签: visual-studio msbuild nuget


【解决方案1】:

这很棒,因为我可以在我的项目中使用 $(PkgNewtonsoft_Json) 文件以获取这些文件。但是,当我从内部构建 VS2017 (V15.9.17),我的 *PROPS 文件没有定义这个符号

经过深入研究和测试,我发现这只是 VS2017 IDE 中的一个问题,而 VS2019 已经修复了这个问题。

xxx.csproj.nuget.g.propsrestore 进程生成。而在VS2017中,对于使用新SDK的项目,VS IDE的还原包机制与msbuild还原机制不同,存在很大的不兼容问题,导致出现各种情况。幸运的是,VS2019 修复了这个问题。

作为建议,您可以尝试以下步骤:

1)下载the latest VS2019并使用。

2) 添加一个自定义目标,其中包含使用 MSBuild 将项目构建到 xxxxx.csproj file 的任务。

<Target Name="OnceBuild" AfterTargets="Build">

    <MSBuild Projects="$(SolutionDir)$(SolutionFileName)" Targets="restore;build" Properties="Configuration=Release;Platform=Any CPU">        
    </MSBuild>   
 </Target>

它将生成由MSBuild restore 创建的xxx.csproj.nuget.g.props。虽然这包含错误 MSB4006,但它不会影响 VS IDE 中的构建过程,您不必担心。

更新 1

只需添加一个自定义目标即可在 MSBuild 中运行 msbuild -t:store

<Target Name="OnceBuild" AfterTargets="Build">    
  <MSBuild Projects="$(SolutionDir)$(SolutionFileName)" Targets="restore" Properties="Configuration=Release;Platform=Any CPU">        
   </MSBuild>   
</Target>

【讨论】:

    猜你喜欢
    • 2017-11-19
    • 1970-01-01
    • 2012-12-01
    • 2015-08-12
    • 2020-05-02
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多