使用 MSBuild 构建多个配置 [英] Using MSBuild to Build Multiple Configurations

查看:28
本文介绍了使用 MSBuild 构建多个配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to edit my project file to enable me to have a project that builds multiple build configs at once. I've done this using a batching approach and using the MSBuild task (see below).

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

If I run the script, I get an this error:

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

Error 103 The OutputPath property is not set for project "ThisMSBuildProjectFile.csproj". Please check to make sure that you have specified a valid combination of Configuration and Platform for this project. Configuration='Debug' Platform='AnyCPU'.

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

I get this if I add or omit the OutputPath from the MSBuild task. If used the VS2010 debugger to step through the script and the MSBuild Task is called - the debugger steps into the file again and then steps into OutputPath, so afaik, it should pick that value up, no?

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

Any help for this would be greatly appreciated - it's driving me crazy. Thanks, Paul.

ThisMSBuildProjectFile.csproj(把多余的东西拿出来):

ThisMSBuildProjectFile.csproj (surplus stuff taken out):

<?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:FolderEtcOutput$(Configuration)</OutputPath>
    <OutDir>C:FolderEtcOutput$(Configuration)</OutDir>
    <BaseOutputPath>C:FolderEtcOutput$(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:FolderEtcThisMSBuildProjectFile.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:FolderEtcOutput" 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="PropertiesAssemblyInfo.cs" />
    <Compile Include="BlahBlah.cs" />
  </ItemGroup>

推荐答案

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

It is important to realize that when you use a "MSBuild" task, a new child MSBuild process will be started. The implication of this is that any items and properties you define in the parent MSBuild process will not be automatically passed to/visible from the child MSBuild process unless you explicitely pass them via Properties attribute on MSBuild element (as in <MSbuild Properties="..." />).

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

To answer your question, I wrote the following self-contained example that runs a child MSBuild project for all the specified configurations:

  1. 首先,为您的 MSBuild 实验创建一个目录(例如我使用 C: empmsbuildtest)

在这个目录下,创建第一个文件,main.proj:

In this directory, create the first file, 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)in\%(ConfigList.Identity)" Targets="Build" />
    </Target>
</Project>

  • 在同一目录中,创建第二个文件 child.proj(在您的情况下,这将是您正在尝试构建的实际 C# 项目,但因为我正在尝试为了说明我的观点,我正在使用一个简单的子项目,而不是运行 C# 编译器,只打印属性值:-))

  • In the same directory, create the second file, child.proj (in your case this would be the actual C# project you're trying to build, but because I'm trying to illustrate my point, I am using a simple child project that instead of running C# compiler just prints values of properties :-) )

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

  • 现在你可以运行这个例子了.如果您没有明确指定要构建的配置,则首先是默认值:

  • Now you can run the example. First the default, if you don't explicitly specify configurations to build:

    C:WINDOWSMicrosoft.NETFrameworkv4.0.30319msbuild main.proj
    > (cut the noise)
    > Build:
    >   Building configuration Debug with output path C:	emp_cdinDebug
    

    然后显式指定多个配置:

    And then explicitly specified multiple configurations:

    C:WINDOWSMicrosoft.NETFrameworkv4.0.30319msbuild main.proj /property:Config=Debug+Release+Staging+Production
    > (cut the noise)
    > Build:
    >   Building configuration Debug with output path C:	emp_cdinDebug
    > Build:
    >   Building configuration Release with output path C:	emp_cdinRelease
    > Build:
    >   Building configuration Staging with output path C:	emp_cdinStaging
    > Build:
    >   Building configuration Production with output path C:	emp_cdinProduction
    

  • 您应该能够根据自己的情况调整这种技术.

    You should be able to adapt this technique to your situation.

    这篇关于使用 MSBuild 构建多个配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆