Unome
Unome

Reputation: 6900

MSBuild run XSD Task before files get compiled

I'm pretty new to MSBuild and am trying to get an XSD Task to run so that the C# files I need to run my app will be refreshed each time we run a build.

I've got the build task working like the following.

<UsingTask TaskName="XSD" AssemblyName="Microsoft.Build.CppTasks.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<Target Name="GenerateCSFromXSD" BeforeTargets="Build">
  <ItemGroup>
    <XSDInput Include="XML\File1.xsd"/>
    <XSDInput Include="XML\File2.xsd"/>
  </ItemGroup>

  <XSD GenerateFromSchema="classes" Language="CS" Sources="@(XSDInput)" />
</Target>

This works fine. Each time I run a build it will regenerate a new C# file for the File1 and File 2 inputs. My issue is that if I delete the File1 and File2.cs files from my repo, and run build, the build will fail because for some reason the files my XSD task is making are not generated at the time where it looks for my source files to compile.

I thought that BeforeTargets="Build" would cause the files to be generated before it did anything else. Any ideas? I've also tried BeforeTargets="Compile"

Upvotes: 1

Views: 832

Answers (1)

Lex Li
Lex Li

Reputation: 63264

You can refer to https://mhut.ch/journal/2015/06/30/build_time_code_generation_msbuild for more details on how to hook your code generation logic.

Or the simplest is to do so in a BeforeBuild target, which MSBuild executes before any other targets.

Upvotes: 1

Related Questions