为什么 MSBuild 无法在 Directory.Build.props 中找到我的常量?
Why does MSBuild fails to find my constant in Directory.Build.props?
我正在尝试使用 Directory.Build.props 文件为我的解决方案定义一个简单的预处理器常量。
出于测试目的,我创建了一个控制台应用程序 (C#) 并在生成的 .csproj[=29 旁边添加了一个 Directoy.Build.props 文件=] 文件:
<Project>
<PropertyGroup>
<DefineConstants>TEST1</DefineConstants>
</PropertyGroup>
</Project>
我还手动编辑了 .csproj 本身来测试 <DefineConstants>
是否有效:
<DefineConstants>DEBUG;TRACE;TEST2;</DefineConstants>
现在实际代码:
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
}
那么为什么 MSBuild 无法在 Directory.Build.props 中找到我的常量?
您应该注意到 Directory.Build.props
文件是在 csproj 文件的开头导入的。在你这边,因为导入的属性太早被覆盖,所以被CSProj.
中的defineConstants
覆盖了
所以Directory.Build.props
通常用于定义全局属性,而Directory.Build.targets
用于覆盖属性。
所以你应该使用覆盖属性。
解决方案
我有两个解决方案给你:
1) 将您的 Directory.Build.props
重命名为 Directory.Build.targets
并确保这些在文件中:
<Project>
<PropertyGroup>
<DefineConstants>$(DefineConstants)TEST1</DefineConstants>
</PropertyGroup>
</Project>
确保 csproj 文件是这样的:
<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) 将文件保留为 Directory.Build.props
并将其内容保留为:
<Project>
<PropertyGroup>
<DefineConstants>TEST1</DefineConstants>
</PropertyGroup>
</Project>
将您的 csproj 文件更改为:
<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>
我正在尝试使用 Directory.Build.props 文件为我的解决方案定义一个简单的预处理器常量。
出于测试目的,我创建了一个控制台应用程序 (C#) 并在生成的 .csproj[=29 旁边添加了一个 Directoy.Build.props 文件=] 文件:
<Project>
<PropertyGroup>
<DefineConstants>TEST1</DefineConstants>
</PropertyGroup>
</Project>
我还手动编辑了 .csproj 本身来测试 <DefineConstants>
是否有效:
<DefineConstants>DEBUG;TRACE;TEST2;</DefineConstants>
现在实际代码:
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
}
那么为什么 MSBuild 无法在 Directory.Build.props 中找到我的常量?
您应该注意到 Directory.Build.props
文件是在 csproj 文件的开头导入的。在你这边,因为导入的属性太早被覆盖,所以被CSProj.
defineConstants
覆盖了
所以Directory.Build.props
通常用于定义全局属性,而Directory.Build.targets
用于覆盖属性。
所以你应该使用覆盖属性。
解决方案
我有两个解决方案给你:
1) 将您的 Directory.Build.props
重命名为 Directory.Build.targets
并确保这些在文件中:
<Project>
<PropertyGroup>
<DefineConstants>$(DefineConstants)TEST1</DefineConstants>
</PropertyGroup>
</Project>
确保 csproj 文件是这样的:
<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) 将文件保留为 Directory.Build.props
并将其内容保留为:
<Project>
<PropertyGroup>
<DefineConstants>TEST1</DefineConstants>
</PropertyGroup>
</Project>
将您的 csproj 文件更改为:
<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>