23

一度に複数のビルド構成をビルドするプロジェクトを作成できるように、プロジェクト ファイルを編集しようとしています。これは、バッチ アプローチと MSBuild タスク (以下を参照) を使用して行いました。

スクリプトを実行すると、次のエラーが表示されます。

エラー 103 プロジェクト "ThisMSBuildProjectFile.csproj" の OutputPath プロパティが設定されていません。このプロジェクトの構成とプラットフォームの有効な組み合わせを指定したことを確認してください。構成='デバッグ' プラットフォーム='AnyCPU'.

これは、MSBuild タスクから OutputPath を追加または省略した場合に得られます。VS2010 デバッガーを使用してスクリプトをステップ実行し、MSBuild タスクが呼び出された場合、デバッガーは再びファイルにステップインし、次に OutputPath にステップインするので、その値を取得する必要があります

これに対する助けがあれば大歓迎です-それは私を夢中にさせています。ありがとう、ポール。

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>

4

3 に答える 3

21

「MSBuild」タスクを使用すると、新しい子 MSBuild プロセスが開始されることに注意してください。これは、親の MSBuild プロセスで定義した項目とプロパティは、要素の属性を介して明示的に渡さない限り、子の MSBuild プロセスに自動的に渡されたり、子の MSBuild プロセスから表示されたりしないことを意味します ( のように)。PropertiesMSBuild<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. 同じディレクトリに 2 番目のファイルを作成します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
    

このテクニックを状況に適応させることができるはずです。

于 2011-04-01T14:32:50.327 に答える
5

プロジェクトファイルに問題があります。このXMLについて考えてみましょう。

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

$(Configuration)と$(Platform)が空であっても、バー文字と連結すると空の文字列と一致しないため、これらのプロパティを設定することはできません。その条件の最小値は'|'です ''ではありません。条件を「|」と比較して修正した場合でも、そのPropertyGroupのOutputPathで$(Configuration)を使用しようとしますが、$(Configuration)は使用された時点で値を持ちません。同様に、$(Platform)を'AnyCPU'に設定しようとすると、すでにその値が設定されている必要があります。おそらく、最初のPropertyGroupの条件を完全に省略することを意図しており、条件のない初期のPropertyGroupで$(Configuration)と$(Platform)のデフォルト値を指定する必要がある場合があります。

また、「ビルド」ターゲットをオーバーライドすると、MSBuildタスクに冗長な条件があることに注意してください。同じ条件で、どのタスクでも必要ありません。

于 2011-04-01T13:59:51.993 に答える
4

プロジェクトの csproj ファイル自体のこのような複雑な構成を実行したいかどうかはよくわかりません。両方の構成でソリューションをビルドする「Both」という特定のターゲットを持つ別の MSBuild「BuildBoth.proj」ファイルをセットアップしたいと思います。

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

次に、両方を対象とするコマンド (冗長性を最小限に設定) を実行します。

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
于 2011-04-01T15:02:50.657 に答える