Mike
Mike

Reputation: 23

Project that Imports a project of common files. How to do in Visual Studio

I have a project that contains common files used by several projects and would like to use the project by using the MSBuild Import.

The following is a snippet from a *.csproj that contains what I'm after.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  ***<Import Project="My.commonFiles.csproj" />
  <PropertyGroup>***
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>

I cannot find any reference how this can be accomplished from within VS 2015. Logically, I would think I would right click the Project | click Add | click Existing Item | and select the project but that does not work.

I really don't wish to have to manually edit the *.csproj. Any help?

Upvotes: 2

Views: 3351

Answers (1)

weir
weir

Reputation: 4771

You do have to manually edit the .csproj. Visual Studio 2017 features the ability to edit project files without unloading, but for now that only applies to certain project types.

The other bummer is that Visual Studio 2015 will generally not even pick up changes to imported files until you close and reopen the solution. (This drove me to add a feature to my team's custom Visual Studio extension that detects such changes and prompts you to reload the solution with one click. Unfortunately, I don't have that functionality isolated and available to share on the Visual Studio Gallery.) In some cases I work around that by using the MSBuild task in lieu of an Import element, along these lines:

<MSBuild Projects="DESIRED_FILE.targets"
         Targets="DESIRED_TARGET"
         Properties="Configuration=$(Configuration);
                     Platform=$(Configuration);
                     Verbosity=$(Verbosity);
                     SomeOtherProperty=$(SomeOtherProperty)" />

However, since the MSBuild task spawns a separate process it is only useful for executing Targets-- not for populating common properties or items like you seem to be trying to do. Sorry that the answer is so disappointing!

By the way, to avoid confusion you would want to name any MSBuild files you create with one of the following extensions instead if .csproj:

  • .props if the file contains PropertyGroup and perhaps ItemGroup elements and needs to be placed near the top of the importing file
  • .targets (alternatively, .proj is sometimes used) if it contains Target elements and should be placed near the bottom of the consuming file

Upvotes: 2

Related Questions