web-dev-qa-db-ja.com

条件付きコンパイルシンボル(DefineConstants)をmsbuildに渡す方法

したがって、 thisthis はどちらもかなり明確です。単に/p:DefineConstants="SYMBOL"

テストプロジェクトでも、私にはまったく機能しません。/p:DefineConstants = "SYMBOL"を渡すと、csprojで定義されている条件付きコンパイル定数が上書きされると思います。しかし、そうではありません...

以下の完全なコードリスト:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DefineConstants
{
    class Program
    {
        static void Main(string[] args)
        {
#if DEV
            Console.WriteLine("DEV");
#Elif UAT 
            Console.WriteLine("UAT");
#else
            Console.WriteLine("No environment provided");
#endif
        }
    }
}

.csprojファイルは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.Microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
    <ProductVersion>8.0.30703</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{57A2E870-0547-475C-B0EB-66CF9A2FE417}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>DefineConstants</RootNamespace>
    <AssemblyName>DefineConstants</AssemblyName>
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
    <PlatformTarget>x86</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>TRACE;DEBUG</DefineConstants>
    <ErrorReport>Prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
    <PlatformTarget>x86</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>Prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

を使用して構築:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild DefineConstants.sln /p:DefineConstants="DEV;DEBUG" /p:Configuration="Debug" /p:Platform="x86"

プログラムを実行すると次のようになります。

No environment provided

ヘルプ!

21
Steven P

DefineConstantsを使用しても確実に機能します。それはあなたが何か間違ったことをしていることを意味します。最初は何も定義せずにプロジェクトをビルドし、次に再度ビルドしたと思います。 MSBuildは、プロジェクトが既にビルドされていることを確認し、再度ビルドするのではなく、出力ファイルをコピーするだけです。 msbuildの出力を投稿して、確実に確認できるようにする必要がありますが、参考のために、必要なスイッチのみを使用してプロジェクトをコンパイルしました。結果は次のとおりです(完全なmsbuild出力は省略されています)。

> msbuild ConsoleApplication1.sln /p:DefineConstants="DEV" /t:Rebuild
....
Building solution configuration "Debug|x86".
Project ... is building ConsoleApplication1.csproj" (Rebuild target(s)).
...
> ConsoleApplication1\bin\Debug\DefineConstants.exe
DEV
> msbuild ConsoleApplication1.sln /p:DefineConstants="UAT" /t:Rebuild
...
> ConsoleApplication1\bin\Debug\DefineConstants.exe
UAT
> msbuild ConsoleApplication1.sln /t:Rebuild
...
> ConsoleApplication1\bin\Debug\DefineConstants.exe
No environment provided 
29
stijn

条件付きコンパイルシンボルを渡していないようです。そのため、次のように出力されます。環境が提供されていません。プロジェクトのプロパティに移動し、ビルドタブ(アプリケーションの下の左側)をクリックします。条件付きコンパイルシンボルを要求するボックスが1つあります。必要な出力に従って定数を書き込むだけです。例として。あなたの例では、「DEV」を印刷するには、条件付きコンパイルボックスでDEVと書き込み、プロジェクトを再ビルドします。きっとうまくいくと思います。

注:コードよりもシンボルを渡したい場合は、次のように出力されます。環境が選択されていません。

0
Sanket Raval