【问题标题】:MSBuild: Add additional files to compile without altering the project fileMSBuild:在不更改项目文件的情况下添加其他文件进行编译
【发布时间】:2011-03-03 01:58:55
【问题描述】:

环顾四周后,我找不到这个问题的简单答案。

我正在尝试创建一个 MSBuild 文件,以允许我在 Visual Studio 2010 express 中轻松使用 SpecFlow 和 NUnit。

下面的文件不完整,这只是一个概念证明,需要更通用。

<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>

SpecFlow 任务在 .csproj 文件中查找并创建一个 SpecFlowFeature1.feature.cs。 我需要在构建 .csproj 时包含此文件,以便 NUnit 可以使用它。

我知道我可以修改(直接或复制).csproj 文件以包含生成的文件,但我希望避免这种情况。

我的问题是:有没有办法使用 MSBuild 任务来构建项目文件并告诉它包含一个附加文件以包含在构建中?

谢谢。

【问题讨论】:

    标签: msbuild specflow


    【解决方案1】:

    我发现不编辑项目文件就无法做到这一点。

    所以我制作了一个 MSBuild 文件:

    • 复制项目文件
    • 通过 SpecFlow 运行副本
    • 将新的 .cs 文件添加到复制的项目中
    • 编译项目
    • 调试通过 NUnit 运行每个已编译的 DLL
    • 清理 - 删除复制的项目

    我在这里写过关于如何使用它的博客:

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

    (这是第1版,我想改进脚本)

    【讨论】:

    • 我阅读了您的博客文章并遵循了所有步骤,但它对我不起作用。作为示例,我使用了 Tekpub MVC 入门站点,但它不起作用。我得到的错误是:“C:\WebTest\VS2010ExpressSpecFlow.xml”(默认目标)(1)->(AddSpecFlowCSFilesToProject 目标)-> C:\WebTest\VS2010ExpressSpecFlow.xml(86,5):错误:对象引用未设置为对象的实例。
    • 感谢您的尝试,让人们点击它是我所需要的。我一有时间就会看看这个……可能这个周末。
    • 虽然我会说构建脚本希望找到 .feature 文件作为内容(属性->构建操作->内容)
    • 好文章!我们将确定从 SpecFlow 站点链接它。
    【解决方案2】:

    如果不对 .csproj 文件进行任何修改,我想不出任何方法来实现。

    我建议的方法如下所示。

    在你的 .csproj 中你 Import 一个容器目标文件

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

    就在 CSharp.targets 上方。

    Specflow.targets 看起来像这样

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

    所以从 VS 构建项目时不会造成伤害。

    然后您可以使用 SpecFlow Exec 的输出并将其添加到 SpecFlow.targets 文件中

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

    ...

    并在构建 .csproj 后清理 SpecFlow.targets。

    【讨论】:

      猜你喜欢
      • 2012-12-13
      • 2022-01-21
      • 2018-06-24
      • 2022-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多