我正在尝试切换解决方案以使用SDK样式的csproj文件,并遇到问题。以下与旧版本的csproj文件配合使用。
我有一个具有多个配置的项目,其中的配置设置了FrameworkVersion:
Project1
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup Condition="$(Configuration.Contains('2020'))">
<TargetFrameworks>net472</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition="$(Configuration.Contains('2021'))">
<TargetFrameworks>net48</TargetFrameworks>
</PropertyGroup>
...
我想使用MSBuild任务从另一个项目中构建这两个配置。像这样:
Project2
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
...
<PropertyGroup>
<BuildProjectsProperties>Platform=AnyCPU;RestoreForce=true;</BuildProjectsProperties>
</PropertyGroup>
<ItemGroup>
<BuildProjectsList Include="$(SolutionDir)Project1\Project1.csproj">
<AdditionalProperties>Configuration=Release 2020;OutputPath=$(ProjectDir)BuiltProjects\2020\;$(BuildProjectsProperties)</AdditionalProperties>
</BuildProjectsList>
<BuildProjectsList Include="$(SolutionDir)Project1\Project1.csproj">
<AdditionalProperties>Configuration=Release 2021;OutputPath=$(ProjectDir)BuiltProjects\2021\;$(BuildProjectsProperties)</AdditionalProperties>
</BuildProjectsList>
</ItemGroup>
<Target Name="BuildProjects">
<RemoveDir ContinueOnError="true" Directories="$(ProjectDir)BuiltProjects" />
<MSBuild RemoveProperties="TargetFramework;TargetFrameworks" Projects="@(BuildProjectsList)" Targets="Restore;Build" BuildInParallel="false" StopOnFirstFailure="True" />
</Target>
...
第一个项目构建良好,但是在第二个MSbuild上找到obj \ project.assets.json,假定不需要还原,然后失败,因为project.assets文件指定了不同的框架。我可以在命令行中通过在MSbuild命令中添加“ -p:RestoreForce = true”来解决此问题,但似乎MSBuild并没有监听我在上面设置的RestoreForce属性。
修改 看起来这可能与以下问题有关:https://github.com/dotnet/msbuild/issues/2811。他们说不要使用“ Restore; Build”,我正在这样做。不过,我对如何实施变通办法没有信心。我尝试拆分MSBuild调用:
<MSBuild
Projects="$(SolutionDir)Project1\Project1.csproj"
Targets="Restore" Properties="Configuration=Release 2020;RestoreForce=true;PostBuildEvent=;"
RemoveProperties="TargetFramework;TargetFrameworks" BuildInParallel="false" StopOnFirstFailure="True" />
<MSBuild
Projects="$(SolutionDir)Project1\Project1.csproj"
Targets="Build" Properties="Configuration=Release 2020;PostBuildEvent=;OutputPath=$(ProjectDir)BuiltProjects\2020\"
RemoveProperties="TargetFramework;TargetFrameworks" BuildInParallel="false" StopOnFirstFailure="True" />
<MSBuild
Projects="$(SolutionDir)Project1\Project1.csproj"
Targets="Restore" Properties="Configuration=Release 2021;RestoreForce=true;PostBuildEvent=;$(BuildProjectsProperties)"
RemoveProperties="TargetFramework;TargetFrameworks" BuildInParallel="false" StopOnFirstFailure="True" />
<MSBuild
Projects="$(SolutionDir)Project1\Project1.csproj"
Targets="Build" Properties="Configuration=Release 2021;PostBuildEvent=;OutputPath=$(ProjectDir)BuiltProjects\2021\"
RemoveProperties="TargetFramework;TargetFrameworks" BuildInParallel="false" StopOnFirstFailure="True" />
可以肯定这是错误的,但是我不确定该怎么做。当开始构建第二组时,上面给出了相同的错误。我还尝试添加两个(<RemoveDir Directories="$(SolutionDir)Project1\obj" />
)之间的obj文件夹,这似乎没有任何区别。也许MSBuild会缓存资产文件?