针对自定义道具/目标的MSBuild设计实践

Jef*_*eff 6 msbuild msbuild-target msbuild-15

我有一个NuGet包,正在使用自定义的props / targets文件,并且想知道有关如何在定义和覆盖方面对许多Properties,Items和Targets进行最佳实践的信息。

最好直接在我的MyNuGetPackage.targets文件中拥有所有属性,而根本不使用MyNuGetPackage.props,就像这样吗?

<PropertyGroup>
    <UseMyTarget Condition="'$(Prop1)' == ''">true</UseMyTarget>
</PropertyGroup>

<Target Name="MyTarget" Condition="'$(UseMyTarget)' == 'true'>
  ...
</Target>
Run Code Online (Sandbox Code Playgroud)

还是更好的做法是无条件地UseMyTarget.props文件中定义并将目标保留在.targets文件中?

如果我需要捕获Microsoft或其他人为其设置默认值的属性,则两种方法都将失效。例如:

<PropertyGroup>
    <MyCustomOutputPath>$(OutputPath)/somethingelse</UseMyTarget>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

如果我在中定义它.propsOutputPath可能尚未设置...

另一方面,许多作者似乎只是在使用.targets自己的东西……所以我最好还是对自己的依赖项做这样的事情,.targets而不用烦恼.props文件吗?

<Project InitialTargets="InitializeDependentProperties">
  <Target Name="InitializeDependentProperties">
    <PropertyGroup>
      <MyCustomOutputPath>$(OutputPath)/somethingelse</UseMyTarget>
    </PropertyGroup>
  </Target>
</Project>
Run Code Online (Sandbox Code Playgroud)

微软自己的MSBuild道具/目标实践似乎根本没有遵循任何标准/惯例。

我确实希望允许消耗性项目能够.props/.targets通过设置Properties/Items/Targets自己来改变我的行为。