.net – error MSB4057: The target “Build” does not exist in the project


The targets file does not have the Build defined.

When you use command like dotnet build dirs.proj, dotnet.exe will try to find the target named build in dirs.proj file. You should define the build target it directly in dirs.proj file or in the xx.targets file. Or you’ll get the error MSB4057…

However, if I define something that’s like it does not do anything.

I assume you’re trying to build the ConsoleApp1 project by command dotnet build dirs.proj, if so, the content in the dirs.proj file should be:

<Project>
  <Import Project="Directory.Build.props" />
  <Import Project="Directory.Build.targets" />
  <ItemGroup>
    <ProjectFile Include="ConsoleApp1/ConsoleApp1.csproj" />
  </ItemGroup>
  <Target Name="build"> 
    <Message Text="Custom build starts..." Importance="High"/>
    <MSBuild Projects="@(ProjectFile)"/>
 </Target>
</Project>

You can use msbuild task in the defined build target, and it will compile and build the consoleApp1 project for you.

In addition:

1.According to your script, your targets file, props file and the dirs.proj file should locate in solution dir

2.If you have many projects in the solution directory, you can try using wildcard to include them all:

<ItemGroup>
    <ProjectFile Include="**/*.csproj" />
</ItemGroup>

3.For msbuild task, you can specify properties when using this task. You can specify the output path, configuration(debug or release), assembly name…

4.See this document, actually the Directory.Build.props and Directory.Build.targets are something will work even we don’t import them explicitly.

Hope it helps 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *