Craig Norton
Craig Norton

Reputation: 1047

MSBuild: Add additional files to compile without altering the project file

After looking around I can't find a simple answer to this problem.

I am trying to create an MSBuild file to allow me to easily use SpecFlow and NUnit within Visual Studio 2010 express.

The file below is not complete this is just a proof of concept and it needs to be made more generic.

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

    <PropertyGroup>
        <BuildDependsOn>
            BuildSolution;          
            SpecFlow;
            BuildProject;           
            NUnit;
        </BuildDependsOn>
    </PropertyGroup>

    <PropertyGroup>
        <Solution>C:\Users\Craig\Documents\My Dropbox\Cells\Cells.sln</Solution>
        <CSProject>C:\Users\Craig\Documents\My Dropbox\Cells\Configuration\Configuration.csproj</CSProject>
        <DLL>C:\Users\Craig\Documents\My Dropbox\Cells\Configuration\bin\Debug\Configuration.dll</DLL>
        <CSFile>C:\Users\Craig\Documents\My Dropbox\Cells\Configuration\SpecFlowFeature1.feature.cs</CSFile>
    </PropertyGroup>

    <Target Name="Build" DependsOnTargets="$(BuildDependsOn)">
        <Message Text="Build Started" Importance="high" />
        <Message Text="Build Ended" Importance="high" />
    </Target>

    <Target Name="BuildSolution">
        <Message Text="BuildSolution Started" Importance="high" />
            <MSBuild Projects="$(Solution)" Properties="Configuration=Debug" />
        <Message Text="BuildSolution Ended" Importance="high" />
    </Target>

    <Target Name="SpecFlow">
        <Message Text="SpecFlow Started" Importance="high" />
            <Exec Command='SpecFlow generateall "$(CSProject)"' />
        <Message Text="SpecFlow Ended" Importance="high" />
    </Target>

    <Target Name="BuildProject">
        <Message Text="BuildProject Started" Importance="high" />
            <MSBuild Projects="$(CSProject)" Properties="Configuration=Debug" />
        <Message Text="BuildProject Ended" Importance="high" />
    </Target>

    <Target Name="NUnit">
        <Message Text="NUnit Started" Importance="high" />
            <Exec Command='NUnit /run "$(DLL)"' />
        <Message Text="NUnit Ended" Importance="high" />
    </Target>
</Project>

The SpecFlow Task looks in the .csproj file and creates a SpecFlowFeature1.feature.cs. I need to include this file when building the .csproj so that NUnit can use it.

I know I could modify (either directly or on a copy) the .csproj file to include the generated file but I'd prefer to avoid this.

My question is: Is there a way to use the MSBuild Task to build the project file and tell it to include an additional file to include in the build?

Thank you.

Upvotes: 2

Views: 4460

Answers (2)

Craig Norton
Craig Norton

Reputation: 1047

I found no way of doing it without editing the project file.

So I made an MSBuild file to:

  • Copy the project files
  • Run the copies through SpecFlow
  • Add the new .cs files to the copied projects
  • Compile the projects
  • Debug Run each of the compiled DLLs through NUnit
  • Clean up - Delete the copied projects

I've blogged about how to use it here:

http://learntdd.wordpress.com/2010/06/10/using-specflow-and-nunit-on-visual-studio-2010-express/

(It's version 1, I'd like to improve the script)

Upvotes: 3

Filburt
Filburt

Reputation: 18082

I couldn't think of any way to achieve without any modification to the .csproj file.

The approach I'd suggest would look like this.

In your .csproj you Import a container target file

...
<Import Project="SpecFlow.target" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
...

just above the CSharp.targets.

Specflow.targets would look like this

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <Compile Include="@(Compile)" />
    </ItemGroup>
</Project>

so it doesn't harm while building the project from VS.

You could then use the Output of your SpecFlow Exec and add it to the SpecFlow.targets file

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <Compile Include="@(Compile)" />
        <Compile Include="SpecFlowFeature1.feature.cs" />
    </ItemGroup>
</Project>

...

and clean SpecFlow.targets after building your .csproj.

Upvotes: 1

Related Questions