首页 文章

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

提问于
浏览
4

我有3个配置项目:

  • 项目A:调试| AnyCPU,发布| AnyCPU

  • 项目B:调试| AnyCPU,发布| AnyCPU

  • Project 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
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):错误:未为项目“A.csproj”设置OutputPath属性 . 请检查以确保您为此项目指定了Configuration和Platform的有效组合 . Configuration ='Debug'Blatform ='x86' . 您可能会看到此消息,因为您正在尝试构建没有解决方案文件的项目,并且已指定了此项目不存在的非默认配置或平台 . [A.csproj] C:\ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \ Microsoft.Common.targets(609,5):错误:未为项目“B.csproj”设置OutputPath属性 . 请检查以确保您为此项目指定了Configuration和Platform的有效组合 . Configuration ='Debug'Blatform ='x86' . 您可能会看到此消息,因为您正在尝试构建没有解决方案文件的项目,并且已指定了此项目不存在的非默认配置或平台 . [B.csproj]

3 回答

  • 3

    我找到了解决问题的方法 . 使用* .csproj文件中的Choose元素通过Visual Studio或MSBuild检测构建,并使用Reference(而不是ProjectReference)进行MSBuild .

    <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>
    
  • 0

    如果在解决方案文件中定义自定义生成配置,则可以使此工作正常 .

    在Visual Studio中打开解决方案,然后打开解决方案的Configuration Manager . 在Active solution平台下拉列表中,将有 <New...> 选项,允许您为解决方案创建x86和x64平台 . 创建它们之后,在同一下拉列表中选择每个项目,并确保在列表中您的项目A和B具有 Any CPU ,并且项目C已选择相应的x86或x64 . 还要确保选中Build复选框 .

    现在将Active解决方案配置切换为Release,并以相同方式定义这些额外平台 .

    完成后,您将能够构建所有3个项目,仅指定MSbuild命令行中的解决方案文件:

    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
    
  • 5

    您可以尝试从命令行使用devenv.exe构建项目 . 我认为这不会有你遇到的问题,但我记得有一些关于这个构建选项被弃用的东西,它有时候会毫无理由地被绞死 . 我把它写成一个选项,只是因为其他两个对我也不好 .

相关问题