Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving compile items in msbuild into a separate file?

This is probably a FAQ, but we weren't able to find a solution even after a lot of searching.

We have a number of msbuild files that all operate on the same set of source files. (It's not particularly relevant but they compile to completely different platforms.) To make managing these a little simpler, we'd like to move the <Compile> source file names to a separate file and reference that from all the msbuild files.

We tried cutting the <ItemGroup> containing the <Compile> items and pasting it into a new file, and surrounding it with

<Project DefaultTargets="Build" ToolsVersion="3.5" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

and then referencing that file from the original with

<Import Project="Common.files.csproj" />

but that does not work - the solution opens (with a warning since we hacked the default config), but no items appear in the Solution Explorer.

What are we doing wrong?

like image 414
bright Avatar asked Sep 21 '11 14:09

bright


1 Answers

Tried with Visual Studio 2010:

1) Create your external .proj (or .target) file and add your files (I used a different item name but that shouldn't matter)

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <ExternalCompile Include="Program.cs" />
        <ExternalCompile Include="Properties\AssemblyInfo.cs" />
    </ItemGroup>
</Project>

2) Import your external .proj file at the top of your Visual Studio project file:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <Import Project="MyExternalSources.proj" />
   <PropertyGroup>
   ...

and modify the Compile ItemGroup like this:

...
<ItemGroup>
    <Compile Include="@(ExternalCompile)" />
</ItemGroup>
...

Warning: You'll have to add new items/files to your external .proj file - all items/files added from within Visual Studio will end up like this:

...
<ItemGroup>
    <Compile Include="@(ExternalCompile)" />
    <Compile Include="MyNewClass.cs" />
</ItemGroup>
...
like image 161
Filburt Avatar answered Sep 30 '22 21:09

Filburt