【发布时间】: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.xml和newtonsoft.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:restore和nuget -t:restore之间的恢复格式问题,这是VS IDE中的恢复格式。 -
什么是“恢复格式”?我已经从 github.com/NuGet/Home/issues/4837 实现了“CopyPackages”。对我来说似乎有点难看,但至少有效。
-
经过深入研究和测试,这是VS 2017 IDE中存在的问题,在最新的VS2019中已修复。
-
我刚试过 - 它似乎没有做任何事情。使用 VS2019,解决方案按原样工作。使用 VS2017,您的修复似乎没有做任何事情 - 不起作用。
-
1>Target OnceBuild: 1> Target ValidateSolutionConfiguration: 1> Building solution configuration "Release|AnyCPU". 1> Target Restore: 1> 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> Done building target "Restore" in project "MyProject.sln". 1>
标签: visual-studio msbuild nuget