msbuild - Is there a .NET Core CLI pre before build task?

ID : 274473

viewed : 49

Tags : .netmsbuildtaskvisual-studio-2017dotnet-cli.net





Top 3 Answer for msbuild - Is there a .NET Core CLI pre before build task?

vote vote

94

The "default items" as the .NET SDK calls it are part of the static evaluation of the project file - before any target is run. So you'll need a target that is run before the @(Compile) items are needed.

The trick is to include files added to the filesystem after the custom tool is run. This can be done by re-scanning all files and excluding those already part of the project inside a target that is run before the build:

  <Target Name="GenerateSomeFiles" BeforeTargets="BeforeBuild">     <Exec Command="dotnet my-tool" />     <ItemGroup>       <Compile Include="**/*$(DefaultLanguageSourceExtension)"                Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder);$(BaseIntermediateOutputPath)**;$(BaseOutputPath)**;@(Compile)" />     </ItemGroup>   </Target> 
vote vote

86

If your custom tool creates just one file, you can create it directly in the intermediate output path and include it in your compile:

  <Target Name="GenerateSomeFiles" BeforeTargets="CoreCompile" DependsOnTargets="PrepareForBuild">     <Exec Command="dotnet my-tool $(MSBuildProjectDirectory)\$(IntermediateOutputPath)\my-output.cs" />   </Target>    <ItemGroup>     <Compile Include="$(MSBuildProjectDirectory)\$(IntermediateOutputPath)\my-output.cs" />   </ItemGroup> 
vote vote

73

Top 3 video Explaining msbuild - Is there a .NET Core CLI pre before build task?







Related QUESTION?