MSBuild retrieves properties from one project to another

Let's say I have two project files "Parent.proj" and "Child.proj". If I declare a property in Parent.proj called MyProp, I can pass this to Child.proj with the following code:

<MSBuild Projects="Child.proj" Targets="dostuff" Properties="MyProp=MyValue" />

This is good, but I want to know if there is a link method MyPropin Child.proj without Child.proj called by Parent.proj.

I know that I can declare the same property in Child.proj and this will be complicated when Child.proj is called by Parent.proj, but I want to avoid repeating the value of the property.

+3
source share
1 answer

If you define your properties in an external project file, each project can import property parameters.

, orders.properties, .

<Project  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- always include the root properties -->
  <Import Project="$(root)\root.properties.proj"/>
  <PropertyGroup>

    <!-- Version numbers/names for this branch -->
    <orders_ver_major>99</orders_ver_major>
    <orders_ver_minor>0</orders_ver_minor>
    <orders_ver_release>0</orders_ver_release>
    <orders_ver>$(orders_ver_major).$(orders_ver_minor).$(orders_ver_release)</orders_ver>
    <orders_ver_db>$(orders_ver_major)_$(orders_ver_minor)_$(orders_ver_release)</orders_ver_db>

    <!-- setup folders specific to the orders project -->
    <orders_database>$(orders_root)\btq.orders.database</orders_database>

    <!-- 
      Setup order database default properties, can be overriden if passed in when called from
      the command line or from other build scripts.
    -->
        <orders_force_create    Condition="'$(orders_force_create)' == ''">false</orders_force_create>
    <orders_db_server           Condition="'$(orders_db_server)' == ''"   >.\sqlexpress</orders_db_server>
    <orders_db_username     Condition="'$(orders_db_username)' == ''" >yyyyyyyy</orders_db_username>
    <orders_db_password     Condition="'$(orders_db_password)' == ''" >xxxxxx</orders_db_password>
    <orders_db_name             Condition="'$(orders_db_name)' == ''"     >$(COMPUTERNAME)_btq_orders_v$(orders_ver_db)</orders_db_name>
  </PropertyGroup>
</Project>

orders.build.proj , .

.

<Project DefaultTargets="build"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- 
    Always setup the path to the root and also the orders root folder.
    We then include the orders properties, which includes the root properties
    For this project the orders folder is in the same folder as this build file
    so can just reference the ms build project directory property as the orders_root.
  -->
  <PropertyGroup>
    <root>$(MSBuildProjectDirectory)\..\..</root>
    <orders_root>$(MSBuildProjectDirectory)</orders_root>
  </PropertyGroup>

  <!--
      Once we have the roots configured we can now include all the standard properties,
      this also includes the root.properties also.
  -->
  <Import Project="$(orders_root)\orders.properties.proj"/>

, .

Noel

+8

Source: https://habr.com/ru/post/1709843/


All Articles