使用MSBuild构建多个配置

时间:2011-04-01 09:06:25

标签: msbuild msbuild-task buildconfiguration

我正在尝试编辑我的项目文件,以便让我拥有一个可以同时构建多个构建配置的项目。我使用批处理方法并使用MSBuild任务(见下文)完成了这项工作。

如果我运行脚本,我会收到此错误:

  

错误103 OutputPath属性为   没有为项目设置   “ThisMSBuildProjectFile.csproj”。   请检查以确保您   指定了有效的组合   配置和平台   项目。配置=“调试”   平台= 'AnyCPU'。

如果我从MSBuild任务添加或省略OutputPath,我会得到这个。如果使用VS2010调试器逐步执行脚本并调用MSBuild任务 - 调试器再次进入文件然后进入OutputPath,那么afaik,应该选择该值,不是吗?

对此的任何帮助将不胜感激 - 它让我发疯。谢谢,保罗。

ThisMSBuildProjectFile.csproj(多余的东西取出):

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">

  <!-- Only Import normal targets if not building multiple projects -->
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" Condition="'$(Configuration)|$(Platform)' != 'AllBuild|AnyCPU' "/>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == '' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>C:\Folder\Etc\Output\$(Configuration)\</OutputPath>
    <OutDir>C:\Folder\Etc\Output\$(Configuration)\</OutDir>
    <BaseOutputPath>C:\Folder\Etc\Output\$(Configuration)\</BaseOutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>

  <!-- Common -->
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <Platform>AnyCPU</Platform>
    <!-- Repeated properties from above here (including, of course, OutputPath) -->  
   </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <!-- Repeated properties from above here (including, of course, OutputPath) --> 
  </PropertyGroup>

  <ItemGroup>
    <Projects Include="C:\Folder\Etc\ThisMSBuildProjectFile.csproj" />
  </ItemGroup>

   <!-- Call this project file again, but with a different configuration - if this was working, this would call multiple  build configs -->
  <Target Name="Build" Condition="'$(Configuration)|$(Platform)' == 'AllBuild|AnyCPU' ">
    <Message Text="hm!"/>
    <!-- Tried thiswith and without the OutputPath property - makes no difference. -->
   <MSBuild  Projects="@(Projects)" Properties="Configuration=Debug;OutputPath=C:\Folder\Etc\Output\" ToolsVersion="4.0" Condition="'$(Configuration)|$(Platform)' == 'AllBuild|AnyCPU' "/>
 </Target>

   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'AllBuild|AnyCPU' ">
    <!-- Repeated properties from above here (including, of course, OutputPath) --> 
  </PropertyGroup>

  <!-- Project files -->
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Properties\AssemblyInfo.cs" />
    <Compile Include="Blah\Blah.cs" />
  </ItemGroup>

3 个答案:

答案 0 :(得分:19)

重要的是要意识到当您使用“MSBuild”任务时,将启动一个新的子MSStild进程。这意味着您在父MSBuild流程中定义的任何项目和属性将自动传递给子MSBuild流程 /可见,除非您明确地通过它们传递它们Properties元素上的MSBuild属性(如<MSbuild Properties="..." />中所示)。

为了回答您的问题,我编写了以下自包含示例,该示例为所有指定的配置运行子MSBuild项目:

  1. 首先,为MSBuild实验创建一个目录(例如我使用C:\temp\msbuildtest

  2. 在此目录中,创建第一个文件main.proj

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.0">
        <ItemGroup>
            <ConfigList Condition=" '@(ConfigList)' == '' and $(Config) != '' " Include="$(Config.Split('+'))" /><!-- parse all requested configurations into a list -->
            <ConfigList Condition=" '@(ConfigList)' == '' " Include="Debug" /><!-- if no configurations were specified, default to Debug -->
        </ItemGroup>
        <!--
    
        Build the child project for each requested configuration. -->
        <Target Name="Build">
            <MSBuild Projects="$(MSBuildProjectDirectory)\child.proj" Properties="Configuration=%(ConfigList.Identity);OutputPath=$(MSBuildProjectDirectory)\bin\%(ConfigList.Identity)" Targets="Build" />
        </Target>
    </Project>
    
  3. 在同一目录中,创建第二个文件child.proj(在您的情况下,这将是您尝试构建的实际C#项目,但因为我试图说明我的观点,我正在使用一个简单的子项目,而不是运行C#编译器只打印属性的值:-))

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.0">
        <Target Name="Build">
            <Message Text="Building configuration $(Configuration) with output path $(OutputPath)" Importance="High" />
        </Target>
    </Project>
    
  4. 现在您可以运行该示例了。首先是默认值,如果您没有明确指定要构建的配置:

    C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\msbuild main.proj
    > (cut the noise)
    > Build:
    >   Building configuration Debug with output path C:\temp_c\d\bin\Debug
    

    然后明确指定多个配置:

    C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\msbuild main.proj /property:Config=Debug+Release+Staging+Production
    > (cut the noise)
    > Build:
    >   Building configuration Debug with output path C:\temp_c\d\bin\Debug
    > Build:
    >   Building configuration Release with output path C:\temp_c\d\bin\Release
    > Build:
    >   Building configuration Staging with output path C:\temp_c\d\bin\Staging
    > Build:
    >   Building configuration Production with output path C:\temp_c\d\bin\Production
    
  5. 您应该能够根据您的情况调整此技术。

答案 1 :(得分:4)

你的项目档案中有些不对劲。考虑一下这个XML:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == '' ">
  <DebugType>pdbonly</DebugType>
  <Optimize>true</Optimize>
  <OutputPath>C:\Folder\Etc\Output\$(Configuration)\</OutputPath> 
  ...
</PropertyGroup>

永远不能设置这些属性,因为即使$(Configuration)和$(Platform)为空,当与bar字符重叠时,它们也永远不会匹配空字符串;该条件的最小值是“|”并不是 ''。即使通过使条件与“|”进行比较来纠正,然后继续尝试在该PropertyGroup中的OutputPath中使用$(Configuration),但$(Configuration)在使用它时将永远不会有值。同样,在您尝试将$(平台)设置为“AnyCPU”的位置,它必须已具有该值。您可能想要在第一个PropertyGroup上完全省略条件,并且您可能需要在早期PropertyGroup中提供$(Configuration)和$(Platform)的默认值,同时也没有条件。将整个项目与新项目区分开来,看看是否还有其他奇怪现象。

另请注意,在覆盖“Build”目标时,MSBuild任务上有一个冗余条件;具有相同条件的是你在任何任务上都不需要它。

答案 2 :(得分:4)

我不太确定我是否想要对项目的csproj文件本身进行这样一个错综复杂的配置。我宁愿设置一个单独的MSBuild“BuildBoth.proj”文件,该文件有一个名为“Both”的特定目标,可以在两种配置中构建解决方案。

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Both">

    <!-- Calls twice for both configs -->
    <Target Name="Both">
        <MSBuild Projects="buildboth.sln" Targets="Rebuild" Properties="Configuration=Debug"
                         StopOnFirstFailure="true">
        </MSBuild>

        <MSBuild Projects="buildboth.sln" Targets="Rebuild" Properties="Configuration=Release"
                         StopOnFirstFailure="true">
        </MSBuild>
    </Target>

    <!-- single config targets

    <Target Name="Debug">
        <MSBuild Projects="buildboth.sln" Targets="Rebuild" Properties="Configuration=Debug"
                         StopOnFirstFailure="true">
        </MSBuild>
    </Target>

    <Target Name="Release">
        <MSBuild Projects="buildboth.sln" Targets="Rebuild" Properties="Configuration=Release"
                         StopOnFirstFailure="true">
        </MSBuild>
    </Target>
    -->

</Project>

然后我将命令(详细程度设置为Minimal)运行目标Both

C:\Projects\experiments\BuildBoth>msbuild /v:m /target:Both BuildBoth.proj
Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.225]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

  BothWpf -> C:\Projects\experiments\BuildBoth\BothWpf\bin\Debug\BothWpf.exe
  BothWpf -> C:\Projects\experiments\BuildBoth\BothWpf\bin\Release\BothWpf.exe