你如何通过条件编译符号(DefineConstants)来的MSBuild [英] How do you pass conditional compilation symbols (DefineConstants) to msbuild

查看:1175
本文介绍了你如何通过条件编译符号(DefineConstants)来的MSBuild的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,无论是<一个href=\"http://stackoverflow.com/questions/479979/msbuild-defining-conditional-compilation-symbols\">this和这是 pretty清楚。简单地传递 / P:DefineConstants =符号

So, both this and this are pretty clear. Simply pass /p:DefineConstants="SYMBOL"

这不适合我在所有的工作,即使是在测试项目。我期待,传入/ P:DefineConstants =符号将覆盖的csproj定义的任何条件编译常数。不是这样的......然而

It doesn't work at all for me, even in a test project. I'm expecting that passing /p:DefineConstants="SYMBOL" will override any conditional compilation constants defined in the csproj. Not the case however...

全部code列出如下:

Full code listing below:

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的文件是:

.csproj file is:

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

帮助!

推荐答案

使用 DefineConstants 绝对不工作。这意味着你正在做的事情是错误的。我的猜测是,你先建项目没有定义任何东西,然后再建造它。随后的MSBuild会看到该项目已经建成并不会再建,但只要复制输出文件。你应该张贴的MSBuild的输出,我们可以肯定,但我参考只用了必要的开关编译你的项目,这里是结果(全输出的MSBuild省略):

Using DefineConstants definitely does work. Which means you are doing something wrong. My guess is you first built the project without defining anything, then built it again. MSBuild will then see the project was built already and won't build again, but just copy output files. You should post the output of msbuild so we can be sure, but for reference I compiled your project using the necessary switches only and here is the result (full msbuild output omitted):

> 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 

这篇关于你如何通过条件编译符号(DefineConstants)来的MSBuild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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