كيفية القيام بما يلي: بنية ملفات مصدر تماما مع الخيارات المختلفة

When you بنية مشاريع, it هو often necessary إلى يحول برمجياً the same مكونات مع different بنية خيارات. For مثال, you can بنية a يصحح بنية مع الرمز معلومات أو a يطرح المنتج بنية مع لا الرمز معلومات but مع أمثلية ممكّن. أو you can بنية a مشروع إلى run تشغيل a specific النظام الأساسي, such كـ x86 أو x64. في الجميع these cases, most of the بنية خيارات stay the same; فقط a few خيارات are تم تغييره إلى عنصر تحكم the تكوين البناء. مع MSBuild, you استخدم خصائص و conditions إلى بنية the different بنية التكوينات.

Using خصائص إلى تعديل مشاريع

The Property عنصر defines a متغير that هو مشار إليها several مرة/مرات في a ملف مشروع, such كـ the الموقع of a temporary الدليل, أو إلى التعيين the قيم for خصائص that are used في several التكوينات, such كـ a يصحح بنية و a يطرح المنتج بنية. ل المزيد من المعلومات حول الخصائص، راجع خصائص ‏‫MSBuild.

You can استخدم خصائص إلى تغيير the تكوين of your بنية without having إلى تغيير the ملف مشروع. The Condition سمة of the Property عنصر و the PropertyGroup عنصر allows you إلى تغيير the القيمة of خصائص. ل المزيد من المعلومات حول MSBuildالشروط، راجع شروط MSBuild.

إلى التعيين a مجموعة of خصائص based تشغيل another خاصية

  • استخدم a Condition سمة في a PropertyGroup عنصر similar إلى the following:

    <PropertyGroup Condition="'$(Flavor)'=='DEBUG'">

    <DebugType>full</DebugType>

    <Optimize>no</Optimize>

    </PropertyGroup>

إلى define a خاصية based تشغيل another خاصية

  • استخدم a Condition سمة في a Property عنصر similar إلى the following:

    <DebugType Condition="'$(Flavor)'=='DEBUG'">full</DebugType>

تعيين خصائص على سطر الأوامر

Once your ملف مشروع هو written إلى قبول إعدادات متعددة, you need إلى have the ability إلى تغيير those التكوينات whenever you بنية your مشروع. MSBuildتوفر هذه القدرة عن طريق السماح بتعيين في خصائص سطر الأوامر يستخدم**/propertyأو/p**رمز التبديل.

لتعيين خاصية مشروع في سطر الأوامر

  • استخدم the /property تبديل مع the خاصية و قيمة الخاصية. فعلى سبيل المثال:

    msbuild file.proj /property:Flavor=Debug

    -أو-

    Msbuild file.proj /p:Flavor=Debug

لتحديد المزيد خاصية مشروع في سطر الأوامر

  • استخدم the /property أو /p تبديل multiple مرة/مرات مع the خاصية و خاصية قيم, أو استخدم واحد /property أو /p تبديل و separate multiple خصائص مع semicolons (;). فعلى سبيل المثال:

    msbuild file.proj /p:Flavor=Debug;Platform=x86

    -أو-

    msbuild file.proj /p:Flavor=Debug /p:Platform=x86

بيئة متغيرات are also treated كـ خصائص و are automatically incorporated بواسطة MSBuild. For المزيد معلومات حول using بيئة متغيرات, see كيفية القيام بما يلي: استخدام متغيرات بيئة في بنية.

ملاحظة

The قيمة الخاصية that هو specified تشغيل the الأمر خط takes precedence over أي القيمة that هو التعيين for the same خاصية في the ملف مشروع, و that القيمة في the ملف مشروع takes precedence over the القيمة في an بيئة متغير.

مثال

The following تعليمات برمجية مثال, the "Hello World" مشروع يحتوي على الثاني جديد خاصية groups that can be used إلى بنية a يصحح بنية و a يطرح المنتج بنية.

إلى بنية the يصحح الإصدار of this مشروع, نوع:

msbuild consolehwcs1.proj /p:flavor=debug

إلى بنية the retail الإصدار of this مشروع, نوع:

msbuild consolehwcs1.proj /p:flavor=retail

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

    <!-- Sets the default flavor of an environment variable called 
    Flavor is not set or specified on the command line -->
    <PropertyGroup>
        <Flavor Condition="'$(Flavor)'==''">DEBUG</Flavor>
    </PropertyGroup>

    <!-- Define the DEBUG settings -->
    <PropertyGroup Condition="'$(Flavor)'=='DEBUG'">
        <DebugType>full</DebugType>
        <Optimize>no</Optimize>
    </PropertyGroup>

    <!-- Define the RETAIL settings -->
    <PropertyGroup Condition="'$(Flavor)'=='RETAIL'">
        <DebugType>pdbonly</DebugType>
        <Optimize>yes</Optimize>
    </PropertyGroup>

    <!-- Set the application name as a property -->
    <PropertyGroup>
        <appname>HelloWorldCS</appname>
    </PropertyGroup>

    <!-- Specify the inputs by type and file name -->
    <ItemGroup>
        <CSFile Include = "consolehwcs1.cs"/>
    </ItemGroup>

    <Target Name = "Compile">
        <!-- Run the Visual C# compilation using input files
        of type CSFile -->
        <CSC  Sources = "@(CSFile)"
            DebugType="$(DebugType)"
            Optimize="$(Optimize)"
            OutputAssembly="$(appname).exe" >

            <!-- Set the OutputAssembly attribute of the CSC
            task to the name of the executable file that is 
            created -->
            <Output TaskParameter="OutputAssembly"
                ItemName = "EXEFile" />
        </CSC>
        <!-- Log the file name of the output file -->
        <Message Text="The output file is @(EXEFile)"/>
    </Target>
</Project>

راجع أيضًا:

موارد أخرى

MSBuild

MSBuild Concepts

مرجع MSBuild