MSBuild Design Practice for Custom Props/Targets
Asked Answered
I

0

7

I have a NuGet package I'm working on with custom props/targets files and am wondering about best practice for how to many Properties, Items, and Targets in terms of definition and overriding.

Is it better to have all properties directly in my MyNuGetPackage.targets file and don't use the MyNuGetPackage.props at all, like so?

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

<Target Name="MyTarget" Condition="'$(UseMyTarget)' == 'true'>
  ...
</Target>

or is it better practice to define UseMyTarget in the .props file, unconditionally and keep the target in the .targets file?

Both approaches break down if I need to catch a property that Microsoft or someone else sets a default value for. For example:

<PropertyGroup>
    <MyCustomOutputPath>$(OutputPath)/somethingelse</UseMyTarget>
</PropertyGroup>

If I define this in .props, OutputPath may not have been set yet...

On the other hand MANY authors seem to exclusively use .targets themselves...so am I better off just doing something like this for dependent things in my .targets and not bother with .props files?

<Project InitialTargets="InitializeDependentProperties">
  <Target Name="InitializeDependentProperties">
    <PropertyGroup>
      <MyCustomOutputPath>$(OutputPath)/somethingelse</UseMyTarget>
    </PropertyGroup>
  </Target>
</Project>

Microsoft's own practices for their own MSBuild props/targets don't seem to follow any kind of standard/convention at all.

I DO want to allow consuming projects to be able to alter behavior of my .props/.targets by setting Properties/Items/Targets themselves.

Intertidal answered 12/8, 2018 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.