Msbuild和VStudio生成不同的project.assets.json文件
这个问题是关于当 Visual Studio 或 Msbuild 生成文件时 NET 5 project.assets.json 文件内容的差异。我的实验显示了以下发现:
VStudio 在每个“干净”操作上重新生成 project.assets.json 文件。
VStudio 仅在 json 中创建目标
net5.0-windows7.0
文件。VStudio 只能在内部构建和调试项目 json 文件中的
net5.0-windows7.0
。VStudio 未在
net5.0-windows7.0/win-x64
中创建目标 json 文件。Msbuild 需要一个目标
net5.0-windows7.0/win-x64
构建成功。如果出现以下情况,MsBuild 将不会重新生成 project.assets.json 文件 存在。
Msbuild 在 VS 生成的 project.assets.json 文件上失败。
我可以使用 msbuild 成功构建的唯一方法是删除 obj/project.assets.json 文件并让 msbuild 重新生成它(可能通过恢复操作)。当 msbuild 重新生成 json 文件时,它包含 net5.0-windows7.0
和 net5.0-windows7.0/win-x64
的两个目标。
我在 csproj 文件中尝试了 Platform=x64/AnyCPU、PlatformTarget=x64/AnyCPU 和 RuntimeIdentifier=win-x64 的各种组合和排列,但没有成功。我可以在命令行上获得成功的 msbuild 的唯一方法是删除构建中涉及的所有项目的所有现有的 project.asset.json 文件。
这是我的 msbuild 命令行:
msbuild /t:clean;restore;publish /p:Platform=AnyCPU /p:PlatformTarget=AnyCPU /p:Configuration=Debug /p:TargetFramework=net5.0-windows7.0 /p:RuntimeIdentifier=win-x64 /p:SelfContained=false /p:PublishProtcol=FileSystem /p:DeleteExistingFiles=true MyFile.csproj
这是我的 csproj 文件(我在排列中添加了 RuntimeIdentifer=win-x64 和 Platforms/Target=AnyCPU;x64,但它们没有帮助。)
<PropertyGroup>
<OutputType>Exe</OutputType>
<Platforms>AnyCPU</Platforms>
<PlatformTarget>AnyCPU</PlatformTarget>
<TargetFramework>net5.0-windows7.0</TargetFramework>
</PropertyGroup>
这是由VStudio:
{
"version": 3,
"targets": {
"net5.0-windows7.0": {}
},
. . . no runtimes section in the VStudio-generated file
这是由 MsBuild 生成的 Project.assets.json:
{
"version": 3,
"targets": {
"net5.0-windows7.0": {},
"net5.0-windows7.0/win-x64": {}
},
. . .
"runtimes": {
"win-x64": {
"#import": []
}
}
问:这是 VS 和 msbuild 的正常行为吗?
问:为什么 msbuild /t:clean;restore;publish
不使用 msbuild 命令行上的参数自动重新生成文件?如果 VS 可以通过 clean
操作自动重新生成文件,为什么 msbuild 不会做同样的事情呢?
问:我是否做错了什么,或者有没有办法避免在每次 msbuild 操作之前删除 project.asset.json 文件?
This question is about the differences in the content of the NET 5 project.assets.json file when Visual Studio or Msbuild generate the file. My experiments have shown the following findings:
VStudio regenerates the project.assets.json file on each ‘clean’ operation.
VStudio creates only a target
net5.0-windows7.0
in the json
file.VStudio can build and debug the project within only
net5.0-windows7.0
in the json file.VStudio does not create a target
net5.0-windows7.0/win-x64
in
the json file.Msbuild requires a target
net5.0-windows7.0/win-x64
for a
successful build.MsBuild will not regenerate the project.assets.json file if it
exists.Msbuild fails on a VS-generated project.assets.json file.
The only way I can get a successful build with msbuild is to delete the obj/project.assets.json file and let msbuild regenerate it (probably with a restore operation). When msbuild regenerates the json file, it includes BOTH targets for net5.0-windows7.0
and net5.0-windows7.0/win-x64
.
I have experimented with various combinations and permutations of Platform=x64/AnyCPU, PlatformTarget=x64/AnyCPU, and RuntimeIdentifier=win-x64 in the csproj file, but without success. The only way I can get a successful msbuild on the command line is to delete all existing project.asset.json files for all projects involved in the build.
Here is my msbuild command line:
msbuild /t:clean;restore;publish /p:Platform=AnyCPU /p:PlatformTarget=AnyCPU /p:Configuration=Debug /p:TargetFramework=net5.0-windows7.0 /p:RuntimeIdentifier=win-x64 /p:SelfContained=false /p:PublishProtcol=FileSystem /p:DeleteExistingFiles=true MyFile.csproj
Here is my csproj file (I added RuntimeIdentifer=win-x64 and Platforms/Target=AnyCPU;x64 in the permutations, but they did not help.)
<PropertyGroup>
<OutputType>Exe</OutputType>
<Platforms>AnyCPU</Platforms>
<PlatformTarget>AnyCPU</PlatformTarget>
<TargetFramework>net5.0-windows7.0</TargetFramework>
</PropertyGroup>
Here is the Project.assets.json file generated by VStudio:
{
"version": 3,
"targets": {
"net5.0-windows7.0": {}
},
. . . no runtimes section in the VStudio-generated file
Here is the Project.assets.json generated by MsBuild:
{
"version": 3,
"targets": {
"net5.0-windows7.0": {},
"net5.0-windows7.0/win-x64": {}
},
. . .
"runtimes": {
"win-x64": {
"#import": []
}
}
Q. Is this normal behavior for VS and msbuild?
Q. Why doesn’t msbuild /t:clean;restore;publish
automatically regenerate the file using the arguments on the msbuild command line? If VS can automatically regenerate the file on a clean
operation, why won’t msbuild do the same thing?
Q. Am I doing something wrong, or is there a way to avoid deleting the project.asset.json files before every msbuild operation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论