良好实践:如何重用的.csproj和的.sln文件来创建你的MSBuild脚本CI?你的、脚本、良好、文件

2023-09-04 00:16:05 作者:可樂

什么是使用的MSBuild作为构建亚军的无痛/维护的方式? (请原谅这个帖子的长度)

What is the painless/maintainable way of using MSBuild as your build runner ? (Forgive the length of this post)

我只是想我在TeamCity的手(我必须要说的是真棒WRT的学习曲线,开箱即用的功能)。我有一个SVN>的MSBuild>的NUnit> NCover组合的工作。

I was just trying my hand at TeamCity (which I must say is awesome w.r.t. learning curve and out of the box functionality). I got an SVN > MSBuild > NUnit > NCover combo working.

我很好奇,如何适度的大项目正在使用的MSBuild - 我刚刚指出的MSBuild我的主要SLN文件。 我花了一些时间与楠几年前,我发现的MSBuild是有点钝。该文档是太密集/详细的初学者。

I was curious as to how moderate to large projects are using MSBuild - I've just pointed MSBuild to my Main sln file. I've spent some time with NAnt some years ago and I found MSBuild to be a bit obtuse. The docs are too dense/detailed for a beginner.

的MSBuild似乎有一些特殊的魔力来处理的.sln文件;我想我的手在写一个自定义生成脚本的手,连接/包括以.csproj的文件(例如,我可以定制pre-后生成的任务)。然而,它扔了(引用重复的目标进口)。我假设大多数开发者不会想要去的MSBuild凸出文件乱搞 - 他们会作出改变的.csproj和的.sln文件。有一些工具/ MSBuild任务的逆向工程一个新的脚本从现有的.sln +我是不知道它的.csproj文件?

MSBuild seems to have some special magic to handle .sln files ; I tried my hand at writing a custom build script by hand, linking/including .csproj files in order (such that I could have custom pre-post build tasks). However it threw up (citing duplicate target imports). I'm assuming most devs wouldn't want to go messing around with msbuild proj files - they'd be making changes to the .csproj and .sln files. Is there some tool / MSBuild task that reverse-engineers a new script from an existing .sln + its .csproj files that I'm unaware of ?

如果我用的MSBuild刚做编译步骤,我还不如用南特与EXEC任务的MSBuild编译的解决方案吗?我这唠叨的感觉,我失去了一些东西明显。

If I'm using MSBuild just to do the compile step, I might as well use Nant with an exec task to MSBuild for compiling the solution ? I've this nagging feeling that I'm missing something obvious.

我的最终目标是在这里有一个MSBuild的构建脚本

My end-goal here is to have a MSBuild build script

它建立了解决方案 在充当构建脚本,而不是一个编译步骤。允许自定义pre /后任务。 (如调用NUnit的运行NUnit的项目(这似乎是没有通过TeamCity的web用户界面支持)) 撑出的更改到解决方案的开发方式。没有冗余;不应该要求开发者做出同样的变化在两个地方

推荐答案

我没有尝试TeamCity的,不过也都成立了建设环境,为新的BizTalk项目。

I didn't try TeamCity yet but did set up a Build environment for our new BizTalk project.

赛义德·易卜拉欣·哈希米中的my起步之前自己的问题,我创建了一整套的MSBuild .proj和.targets脚本。

Following the excellent advice of Sayed Ibrahim Hashimi on my own question before starting out, I created a set of MSBuild .proj and .targets scripts.

核心

要执行的实际建立一个中央.targets脚本步骤:

A central .targets script for the actual build steps you want to perform:

<Project DefaultTargets="Deploy" xmlns="...">
    <!-- omitted validation steps, see referenced post for details -->
    <PropertyGroup>
        <PullDependsOn>
            $(ValidateDependsOn);
            Validate;
        </PullDependsOn>
    </PropertyGroup>

    <PropertyGroup>
        <BuildDependsOn>
            $(PullDependsOn);
            PullFromVersionControl;
        </BuildDependsOn>
    </PropertyGroup>

    <PropertyGroup>
        <DeployDependsOn>
            $(BuildDependsOn);
            Build;
        </DeployDependsOn>
    </PropertyGroup>

    <Target Name="PullFromVersionControl" DependsOnTargets="$(PullDependsOn)">
        <Exec Command="..." />
    </Target>

    <Target Name="Build" DependsOnTargets="$(BuildDependsOn)">
        <MSBuild Projects="@(ProjectsToBuild)" />
    </Target>

    <Target Name="Deploy" DependsOnTargets="$(DeployDependsOn)">
        <Exec Command="..." />
    </Target>
</Project>

第二个核心部分是配置目标就像你会发现它们在你的.csproj文件

The second core part are the configuration targets like you find them in your .csproj files

<Project xmlns="...">
    <PropertyGroup Condition=" '$(Environment)' == 'DEV' ">
        <SomeConfigKey Condition=" '$(SomeConfigKey)' == '' ">Foo</SomeConfigKey>
    </PropertyGroup>

    <PropertyGroup Condition=" '$(Environment)' == 'TEST' ">
        <SomeConfigKey Condition=" '$(SomeConfigKey)' == '' ">Bar</SomeConfigKey>
    </PropertyGroup>
</Project>

项目

单.csproj的本身是由a.targets psented再$ P $你需要建设ItemGroups只是一个集合文件。

The single .csproj itself is represented by a.targets file with just a collection of ItemGroups you need for building.

<Project xmlns="...">
    <ItemGroup>
        <!-- this group contains the list of items to pull from version control -->
        <Sources Include="@(Sources)" />
        <Sources Include="MyProjectRootDir" />
        <Sources Include="MyDependentProjectRootDir" />
    </ItemGroup>

    <ItemGroup>
        <ProjectsToBuild Include="@(ProjectsToBuild)" />
        <ProjectsToBuild Include="MyProject.csproj" />
    </ItemGroup>
</Project>

将其组合在一起

在.proj你实际上要与MSBuild的执行将导入您的配置项目(来源$ C ​​$ C文件)以及核心(拉,构建和部署命令)

The .proj you are actually going to execute with MSBuild will import your Configuration, your Project (source code files) and the Core (Pull, Build and Deployment commands)

<Project DefaultTargets="Deploy" xmlns="...">
    <Import Project="Config.targets"/>

    <Import Project="Project.targets"/>

    <Import Project="Core.targets"/>
</Project>

使用这种方法,我能够重用包含源代码来构建创造VS解决方案给他们组我的一些50个项目,在许多不同的组合,而不是.targets。

Using this approach I was able to reuse the .targets containing the sources to build my some 50 projects in many different combinations instead of creating VS solutions to group them.

我希望你会发现这个有用的 - 如果你有兴趣,我可以添加更多的细节。

I hope you'll find this useful - I can add more details if you're interested.