Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exclude certain projects from using Directory.Build.props

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

like image 738
user1367984 Avatar asked May 15 '19 18:05

user1367984


People also ask

How do you exclude a project from a build?

On the menu bar, choose Build > Configuration Manager. In the Project contexts table, locate the project you want to exclude from the build. In the Build column for the project, clear the check box. Choose the Close button, and then rebuild the solution.

What is directory build props?

Directory. Build. props is a user-defined file that provides customizations to projects under a directory.


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.

like image 62
Max Avatar answered Sep 21 '22 05:09

Max


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="..\Analyzer\Analyzer.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.

like image 34
Water Avatar answered Sep 19 '22 05:09

Water