为什么 MSBuild 无法在 Directory.Build.props 中找到我的常量? [英] Why does MSBuild fails to find my constant in Directory.Build.props?

查看:60
本文介绍了为什么 MSBuild 无法在 Directory.Build.props 中找到我的常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用

检查这个链接.

所以你应该使用覆盖属性.

解决方案

我有两种解决方案:

1) 将您的 Directory.Build.props 重命名为 Directory.Build.targets 并确保这些在文件中:

<项目><属性组><DefineConstants>$(DefineConstants)TEST1</DefineConstants></PropertyGroup></项目>

确保 csproj 文件像这样:

I am trying to use a Directory.Build.props file to define a simple preprocessor constant for to my solution.

For testing purposes I created a console application (C#) and added a Directoy.Build.props file right next to the generated .csproj file:

<Project>
   <PropertyGroup>
       <DefineConstants>TEST1</DefineConstants>
   </PropertyGroup>
</Project>

I also manually edited the .csproj itself to test the <DefineConstants> actually works:

<DefineConstants>DEBUG;TRACE;TEST2;</DefineConstants>

Now the actual code:

static void Main(string[] args)
{
#if TEST1
    // NOT WORKING!
    Console.WriteLine("<DefineConstants>TEST1</DefineConstants> in Directory.Build.props"); // NOT WORKING!
#else
    Console.WriteLine("failed TEST1 in Directory.Build.props");
#endif

#if TEST2
     // WORKS!
     Console.WriteLine("<DefineConstants>TEST2</DefineConstants> in csproj"); // WORKS!
#else
     Console.WriteLine("failed TEST2 in csproj");
#endif

    }

So why does MSBuild fails to find my constant in Directory.Build.props?

解决方案

You should note that Directory.Build.props file is imported at the begin of the csproj file. In your side, because the imported property is too early to be overridden, it is overridden by defineConstants in CSProj.

So Directory.Build.props is usually used to define global properties while Directory.Build.targets is to overwrite the properties.

Check this link.

So you should use to overwrite the property.

Solutions

I have two solutions for you:

1) rename your Directory.Build.props to Directory.Build.targets and ensure these on the file:

<Project>
   <PropertyGroup>
       <DefineConstants>$(DefineConstants)TEST1</DefineConstants>
   </PropertyGroup>
</Project>

Make sure that csproj file like this:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE;TEST2;</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>

2) remain the file as Directory.Build.props and keep its content as:

<Project>
   <PropertyGroup>
       <DefineConstants>TEST1</DefineConstants>
   </PropertyGroup>
</Project>

change your csproj file to:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE;TEST2;$(DefineConstants)</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>

这篇关于为什么 MSBuild 无法在 Directory.Build.props 中找到我的常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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