简体   繁体   English

基于目标平台的NuGet包的props中的DefineConstant

[英]DefineConstant in NuGet package's props based on the target platform

I'm building a multi-target NuGet package and I want it to provide a compile constant for the consumer:我正在构建一个多目标 NuGet package 并且我希望它为消费者提供一个编译常量:

<TargetFrameworks>Xamarin.Mac20;netstandard2.0;uap10.0.17763</TargetFrameworks>

I want a certain feature to be only available for two of this targets.我希望某个功能仅适用于其中两个目标。 To do so I'm defining a custom constant:为此,我定义了一个自定义常量:

<PropertyGroup Condition="$(TargetFramework.StartsWith('uap10.0.17763')) Or $(TargetFramework.StartsWith('Xamarin.Mac'))">
    <DefineConstants>$(DefineConstants);FEATURE_AVAILABLE</DefineConstants>
</PropertyGroup>

Now I want not only to use this constant in my current assembly I want to create a package from, but also in the consumer project.现在我不仅想在我当前的程序集中使用这个常量,我想从中创建一个 package,而且还想在消费者项目中使用。 To achieve that I created a Common.props file and included it into the package:为此,我创建了一个Common.props文件并将其包含在 package 中:

<Import Project="Common.props" />
<ItemGroup>
  <None Include="Common.props" Pack="true" PackagePath="build" />
</ItemGroup>

After building I can confirm my Common.props was added to the package and I could successfully consume it in the main project.构建后,我可以确认我的Common.props已添加到 package 并且我可以在主项目中成功使用它。

The main project targets the same 3 platforms as the package project.主项目针对与 package 项目相同的 3 个平台。 Now I just want to use FEATURE_AVAILABLE imported from the package so I can utilise only the available code from the package.现在我只想使用从 package 导入的FEATURE_AVAILABLE ,所以我只能使用 package 中的可用代码。

The problem is it doesn't respect the platform conditions that wrap the DefineConstants instruction in my main project like if it was executed before MSBuild knows the target framework it's going to build for.问题是它不尊重在我的主项目中包装DefineConstants指令的平台条件,就像它在 MSBuild 知道它将为其构建的目标框架之前执行的一样。 If I remove the condition, the constant is available in my main project code.如果我删除条件,则常量在我的主项目代码中可用。 With the condition in place it's not.在条件到位的情况下,它不是。

It turned out Nuget doesn't respect Xamarin.Mac20 as a target name though dotnet itself does.事实证明 Nuget 不尊重Xamarin.Mac20作为目标名称,尽管 dotnet 本身确实如此。 When the packages are being restored, Nuget generates its own PROJECT_NAME.csproj.nuget.g.props file that includes the following line (cleaned up and sanitised):恢复包时,Nuget 会生成自己的PROJECT_NAME.csproj.nuget.g.props文件,其中包括以下行(已清理和清理):

<ImportGroup Condition=" '$(TargetFramework)' == 'xamarinmac20'">
  <Import Project="$(NuGetPackageRoot)MY_NUGET_NAME/VERSION/build/FILE.props" />
</ImportGroup>

Changing the target framework from Xamarin.Mac20 to xamarinmac20 solves the issue.将目标框架从Xamarin.Mac20更改为xamarinmac20解决了该问题。 I don't really want to do it for all of my dependent projects so I'll either be including the full import line manually or will find a way to configure Nuget to my needs.我真的不想为我所有的依赖项目都这样做,所以我要么手动包含完整的导入行,要么找到一种方法来配置 Nuget 以满足我的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM