Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

c# - Why does MSBuild fails to find my constant in Directory.Build.props?

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?

question from:https://stackoverflow.com/questions/65936714/why-does-msbuild-fails-to-find-my-constant-in-directory-build-props

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

enter image description here

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>binDebug</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>binDebug</OutputPath>
    <DefineConstants>DEBUG;TRACE;TEST2;$(DefineConstants)</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...