构建解决方案时在命令行上为单个项目传递 属性

Pass property for single project on commandline when building a solution

当我使用 msbuild 构建包含多个项目的解决方案时,我能否在命令行中以 属性 仅用于其中一个项目的方式传递 属性?

也就是说,我可以说-p:Foo=42应该用于Project1,而不是Project2吗?

MSBuild 无法通过解决方案文件 (msbuild xxx\xxx.sln) 轻松地为项目之一指定 属性 和 only targets can be specified。或者您必须键入多个 msbuild 命令行以指定相关的 csproj 文件以启用已更改的 属性,如 msbuild Project1.csproj -t:build -p:xxx=xxxmsbuild Project2.csproj -t:build。然而,它太复杂和不方便。

所以我建议你可以使用msbuild脚本来得到你想要的。

1) 创建一个名为 build.proj 的文件:

<Project>        
    
  <ItemGroup>
              
     <!--add all the projects from the solution and remove the any project you want to modify the foo property-->
     <MostProjectFile Include="**\*.csproj;**\*.vcxproj" Exclude="**\Project1.csproj" />
              
     <!--add any projects you want to modify the foo value-->
     <SpecialProjectFile Include="**\Project1.csproj" />
              
  </ItemGroup>
            
            
  <Target Name="Build">
            
  <!--build the most projects and remove the project which you want to change foo property-->
  <MSBuild Projects="@(MostProjectFile)" Targets="Build" Properties="Configuration=Debug"/>
            
  <!--build any projects that wants to modify the foo property separately-->
  <MSBuild Projects="@(SpecialProjectFile)" Targets="Build" Properties="Configuration=Debug;Foo=42"/>
            
  </Target>
            
</Project>

2)如果你想再次更改foo 属性,你可以根据需要修改build.proj

运行 msbuild xxx\build.proj -t:Build 构建它。