C
C#2y ago
Byron

✅ Restore a PackageReference and use contained MSBBuild scripts inside of .targets file

Hey folks, I have a CICD script that executes a custom target Foo.targets using dotnet msbuild Foot.targets /t:DoSomething The script pretty much looks like this
<Project>
<ItemGroup>
<PackageRefernce Include="CustomBuildTargets" Version="1.0.0"/>
</ItemGroup>

<Target Name="DoSomthing">
<Message Importance="high" Text="The value is $(CustomBuildTargets_Variable)"/>
</Target>
</Project>
<Project>
<ItemGroup>
<PackageRefernce Include="CustomBuildTargets" Version="1.0.0"/>
</ItemGroup>

<Target Name="DoSomthing">
<Message Importance="high" Text="The value is $(CustomBuildTargets_Variable)"/>
</Target>
</Project>
The target is trying to use other targets and variables that are defined within the CustomBuildTargets nuget package. However since the this is a .target file and not a csproj I can't use restore. Is there a way to go about doing this? Things I have attempted> 1. -t:Restore=true 2. GeneratePathProperty and trying to get the path to the build folder in the nuget pakcage
3 Replies
reflectronic
reflectronic2y ago
i would not use a plain targets file for this the PackageReference does nothing here you don't import or run the NuGet targets so nothing uses it. you might as well have typed <Blablabla Include="CustomBuildTargets" /> what i think you probably want is to use the Microsoft.Build.NoTargets SDK https://github.com/microsoft/MSBuildSdks/tree/main/src/NoTargets. it imports all of the SDK logic but doesn't actually do anything inside the compile target so, you would define a Foo.msbuildproj that looks something like:
<Project Sdk="Microsoft.Build.NoTargets/3.7.0">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageRefernce Include="CustomBuildTargets" Version="1.0.0"/>
</ItemGroup>

<Target Name="DoSomething" BeforeTargets="CoreCompile">
<Message Importance="high" Text="The value is $(CustomBuildTargets_Variable)"/>
</Target>

</Project>
<Project Sdk="Microsoft.Build.NoTargets/3.7.0">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageRefernce Include="CustomBuildTargets" Version="1.0.0"/>
</ItemGroup>

<Target Name="DoSomething" BeforeTargets="CoreCompile">
<Message Importance="high" Text="The value is $(CustomBuildTargets_Variable)"/>
</Target>

</Project>
and then dotnet build it
Byron
ByronOP2y ago
oh that is really intresting! I have never played around with the NoTargets. I will take a gander at that. Thank you
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server