I found a workaround for this. Maybe it is not the most elegant thing to do, but it works.
I had to override the "Build" Target. The original target is defined in Microsoft.Common.Targets file. I inspected the file tho see how the "Build" target is defined and it is like this
<Target
Name="Build"
Condition=" '$(_InvalidConfigurationWarning)' != 'true' "
DependsOnTargets="$(BuildDependsOn)"
Returns="$(TargetPath)" />
So, in my project file I had to create my own "Build" target which would be identical to that one above, with the only difference that it would have additional conditional logic
In my csproj file I created this:
<Target
Name="Build"
Condition=" '$(_InvalidConfigurationWarning)' != 'true' And (.... more conditions here....) "
DependsOnTargets="$(BuildDependsOn)"
Returns="$(TargetPath)" />
This seems to work, especially when the project is built as part of a batch build of multiple projects within a solution.