Call script only once before all builds in a multi-targeting project

98 Views Asked by At

I'm having a multiple targeting project using a script that generates classes out of a XSD. That should only be done once - for all build targets.

I found that SO-question but this solution does not work.

My filtered csproj contains these entries:

<PropertyGroup>
  <TargetFrameworks>net472;net60-windows</TargetFrameworks>
...
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
  <Exec Command="call $(ProjectDir)..\..\..\Scripts\Python\XsdClassConverter2-Invoke.bat  ..." />
</Target>

I tried to change BeforeTargets and Name to <Target Name="OuterPreBuild" BeforeTargets="DispatchToInnerBuilds"> as suggested in linked post. But this does not work.

I'm sure that I'm missing something.

I tried to find all possible entries for BeforeTargets and found that link. But all of the possibilities does not sound like the setting that I'm looking for.

How do I've to call/setup my script to run only once?

Build environment is VS2022.

EDIT

I've a working solution:

<Target Name="GenerateXsdsCore">
  <Exec Command="call &quot;$(ProjectDir)..\..\..\Scripts\Python\XsdClassConverter2-Invoke.bat&quot; &quot;$(ProjectDir)Csv\Persistence\V1\V1_0.CsvCommon.xsd&quot; -n &quot;Toolkit.IO.Framework.Csv.Persistence.V1&quot; --field-specified-as-compare-default" />
  <Exec Command="call &quot;$(ProjectDir)..\..\..\Scripts\Python\XsdClassConverter2-Invoke.bat&quot; &quot;$(ProjectDir)Licensing\Osi\Persistence\V1\V1_0.OpenSourceLicenseInformation.xsd&quot; -n &quot;Toolkit.IO.Framework.Licensing.Osi.Persistence.V1&quot; --field-specified-as-compare-default" />
</Target>
<Target Name="GenerateXsds" BeforeTargets="DispatchToInnerBuilds;BeforeBuild">
  <MSBuild Projects="$(MSBuildProjectFile)" Targets="GenerateXsdsCore" Properties="TargetFramework=once" />
</Target>

The interesting point is TargetFramework=once. But this solution does not look like it should be done.

1

There are 1 best solutions below

1
Jonathan Dodds On

You haven't explained how "this solution does not work".

If you create a target like the following, do you see the message appear in the build output? Does it appear once when there are multiple targets?

  <Target Name="TestTargetBeforeMultiFrameworkTargeting" BeforeTargets="DispatchToInnerBuilds">
    <Message Text="*** in TestTargetBeforeMultiFrameworkTargeting" Importance="high" />
  </Target>

This is testing that the technique in the answer to the "How do I call my script before all the builds for only once in a multi-targeting project" question, works as advertised.