使用msbuild构建并动态设置项目引用

Dar*_*ell 8 msbuild reference

我有几个参考SQL Server程序集的项目.对于SQL Server 2005和SQL Server 2008,我目前正在维护2个项目文件,这些文件指向相同的源文件,唯一的区别是对SQL Server程序集的引用.

有什么方法我只能维护一个项目并在我的构建脚本中动态指定引用?

Lau*_*mpé 17

正在寻找解决同一问题的解决方案,我得出了在ItemGroup上有条件的建议解决方案.但这有副作用,因为在Visual Studio引用中我可以看到两个引用,这也影响了ReSharper.

我最后使用了Select When,而且我没有任何问题,ReSharper和Visual Studio显示两个References.

<Choose>
  <When Condition=" '$(Configuration)' == 'client1DeployClickOnce' ">
    <ItemGroup>
        <ProjectReferenceInclude="..\client1\app.Controls\app.Controls.csproj">
        <Project>{A7714633-66D7-4099-A255-5A911DB7BED8}</Project>
        <Name>app.Controls %28Sources\client1\app.Controls%29</Name>
      </ProjectReference>
    </ItemGroup>
  </When>
  <Otherwise>
    <ItemGroup>
      <ProjectReference Include="..\app.Controls\app.Controls.csproj">
        <Project>{2E6D4065-E042-44B9-A569-FA1C36F1BDCE}</Project>
        <Name>app.Controls %28Sources\app.Controls%29</Name>
      </ProjectReference>
    </ItemGroup>
  </Otherwise>
</Choose>
Run Code Online (Sandbox Code Playgroud)

您可以在我的博客文章中阅读更多相关信息:在MSBuild项目文件中使用Condition的ProjectReference


Say*_*imi 5

每个MSBuild元素(几乎每个都可以)可以有一个与之关联的Condition.我建议你编辑项目文件(这是一个MSBuild文件本身)并将所有SQL服务器引用放在一个ItemGroup中,例如:

  <ItemGroup Condition="'$(SqlServerTargetEdition)'=='2005'">
    <!-- SQL Server 2005 References here -->
    <Reference Include="..."/>
  </ItemGroup>
Run Code Online (Sandbox Code Playgroud)

另一个Sql server 2008的ItemGroup:

  <ItemGroup Condition="'$(SqlServerTargetEdition)'=='2008'">
    <!-- SQL Server 2008 References here -->
    <Reference Include="..."/>
  </ItemGroup>
Run Code Online (Sandbox Code Playgroud)

在声明这些项之前,您应该为属性SqlServerTargetEdition提供默认值.然后在命令行中,您可以在调用msbuild.exe时使用/ p开关覆盖该值.

Sayed Ibrahim Hashimi

我的书:Microsoft Build Engine内部:使用MSBuild和Team Foundation Build