TransWikia.com

exclude certain projects from using Directory.Build.props

Stack Overflow Asked by user1367984 on December 1, 2021

i have a new visual studio solution that has around 350 projects. it takes visual studio a lot of time to compile the .sln file so i implemented Directory.Build.props to avoid copying of references that are not needed to copy to the local directory so the build can be made quicker. below is the code that im using inside the Directory.Build.props file under the root folder.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemDefinitionGroup>
  <Reference>
    <Private>False</Private>
  </Reference>
  <ProjectReference>
     <Private>False</Private>
  </ProjectReference>
</ItemDefinitionGroup>
</Project>

since i placed Directory.Build.props under root folder it is being applied for all projects.

Question::
how can i exclude few projects from applying Directory.Build.props so that the references can be copied to the local.

in short i want the Directory.Build.props to be applied to only 300 projects under the solution file remaining 50 projects need to be excluded from this

how/where can i write a condition in the above code that will exclude certain projects being affected by this code

2 Answers

For others dealing with the same problem, there is another trick that can be used to exclude certain project from using the Directory.Build.props file found at root level.

If you add a dummy Directory.Build.props file in the project you want to exclude, then the Directory.Build.props from the root will not be used. This is because MSBuild walks the directory structure upwards from the location of your project, until it locates the first Directory.Build.props. That will be used. This behavior is documented on the Customize your build page under Search scope at the Microsoft docs.

Sample of the dummy Directory.Build.props:

<Project>
    <!-- Only here so that the default Directory.Build.props will not be used. -->
</Project>

I found this to be a convenient way to solve this issue. Especially when dealing with only a few projects that need to be excluded.

Answered by Max on December 1, 2021

I had to work around this in a bit of a hacky way.

In my example, there was a custom analyzer project I wrote that I did not want included in another set of projects. I ended up writing something like this in my Directory.Build.props:

<Project>
  ...

  <Choose>
    <When Condition="$(MSBuildProjectName)!='Analyzer' AND ...">
      <ItemGroup>
        <ProjectReference Include="..AnalyzerAnalyzer.csproj">
          <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
          <OutputItemType>Analyzer</OutputItemType>
        </ProjectReference>
      </ItemGroup>
    </When>
    <Otherwise>
       ...
    </Otherwise>
  </Choose>

  ...
</Project>

Where I filled in ... with the projects I wanted it to skip.

I understand this may not be the exact answer you were looking for, but I did a ton of research and was also unable to find any way to do it the way you described. The stuff I have posted was the only way I was able to achieve the ability to exclude certain things from being applied to specific projects by filtering via name. I know that this is hacky and sucks, but it's the only thing that was able to work for me.

Also note that <Otherwise></Otherwise> may be turned into <Otherwise /> possibly, and may even be optional altogether. I left it there so that you could place stuff inside of it if needed.

Answered by Water on December 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP