通过MSBuild构建不同平台的Visual Studio项目

4

我有3个配置的项目:

  • 项目A: Debug|AnyCPU, Release|AnyCPU
  • 项目B: Debug|AnyCPU, Release|AnyCPU
  • 项目C: Debug|x86, Debug|x64, Release|x86, Release|x64

项目C依赖于B,B依赖于A。(A <- B <- C)

我使用*.bat文件通过命令行构建它们:

msbuild A.csproj /target:Build /property:Configuration=Debug;Platform=AnyCPU /verbosity:minimal
msbuild A.csproj /target:Build /property:Configuration=Release;Platform=AnyCPU /verbosity:minimal<br/>
msbuild B.csproj /target:Build /property:Configuration=Debug;Platform=AnyCPU /verbosity:minimal
msbuild B.csproj /target:Build /property:Configuration=Release;Platform=AnyCPU /verbosity:minimal
msbuild C.csproj /target:Build /property:Configuration=Debug;Platform=x86 /verbosity:minimal
msbuild C.csproj /target:Build /property:Configuration=Release;Platform=x86 /verbosity:minimal
msbuild C.csproj /target:Build /property:Configuration=Debug;Platform=x64 /verbosity:minimal
msbuild C.csproj /target:Build /property:Configuration=Release;Platform=x64 /verbosity:minimal

收到错误信息:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets(609,5): error : 项目 'A.csproj' 没有设置 OutputPath 属性。请确保为该项目指定了一个有效的 Configuration 和 Platform 组合。Configuration='Debug' Platform='x86'。您可能会看到此消息,因为您正在尝试构建一个没有解决方案文件的项目,并且指定了不存在于该项目中的非默认 Configuration 或 Platform。[A.csproj]
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets(609,5): error : 项目 'B.csproj' 没有设置 OutputPath 属性。请确保为该项目指定了一个有效的 Configuration 和 Platform 组合。Configuration='Debug' Platform='x86'。您可能会看到此消息,因为您正在尝试构建一个没有解决方案文件的项目,并且指定了不存在于该项目中的非默认 Configuration 或 Platform。[B.csproj]

1
使用.dll引用而不是ProjectReference是不好的,我希望每次构建时也能构建依赖项。为主项目定义“Any CPU”也不好,因为它不是“Any CPU”,而是x86或x64。对我来说,这个问题没有得到很好的解决。我宁愿停止使用MSBuild来构建我的项目,直到这个问题得到正确的解决。我认为我也可以使用命令行中的devenv.exe进行构建,但似乎这已经被弃用了,并且存在错误,有时候devenv会无缘无故地挂起,直到被杀掉。 - watbywbarif
3个回答

6
我找到了解决我的问题的方法。 在*.csproj文件中使用Choose元素来检测是通过Visual Studio还是通过MSBuild构建,并且针对MSBuild,使用Reference(而不是ProjectReference)即可。
<Choose>
  <When Condition="'$(BuildingInsideVisualStudio)' == 'true'">
    <ItemGroup>
      <ProjectReference Include="A.csproj">
        <Project>{AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA}</Project>
        <Name>A</Name>
        <Private>True</Private>
      </ProjectReference>
      <ProjectReference Include="B.csproj">
        <Project>{BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB}</Project>
        <Name>B</Name>
        <Private>True</Private>
      </ProjectReference>
    </ItemGroup>
  </When>
  <Otherwise>
    <ItemGroup>
      <Reference Include="A, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx, processorArchitecture=MSIL">
        <HintPath>A.dll</HintPath>
        <Private>True</Private>
      </Reference>
      <Reference Include="B, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx, processorArchitecture=MSIL">
        <HintPath>B.dll</HintPath>
        <Private>True</Private>
      </Reference>
    </ItemGroup>
  </Otherwise>
</Choose>

3
如果您在解决方案文件中定义自定义构建配置,就可以使这个工作起来。
在 Visual Studio 中打开解决方案,然后打开该解决方案的“配置管理器”。在“活动解决方案平台”下拉列表中,将会有一个“”选项,允许您为解决方案创建 x86 和 x64 平台。创建后,选择相应的平台并确保在项目 A 和 B 的列表中都有“Any CPU”,而 C 项目则选择相应的 x86 或 x64,同时确保“生成”复选框已选中。
现在切换到“发布”的“活动解决方案配置”,以相同的方式定义这些额外的平台。
完成上述操作后,您将能够只指定 MSbuild 命令行中的解决方案文件来构建所有 3 个项目。
msbuild ABC.sln /target:Build /property:Configuration=Debug;Platform=x86 /verbosity:minimal
msbuild ABC.sln /target:Build /property:Configuration=Release;Platform=x86 /verbosity:minimal
msbuild ABC.sln /target:Build /property:Configuration=Debug;Platform=x64 /verbosity:minimal
msbuild ABC.sln /target:Build /property:Configuration=Release;Platform=x64 /verbosity:minimal

0

您可以尝试使用命令行从devenv.exe构建项目。我认为这不会有您遇到的问题,但我记得关于此构建选项被弃用并且有时会无缘无故地挂起的事情。我将其写成一个选项,只是因为其他两个选项对我来说也不好。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接