Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild extract properties from one project to another

Tags:

msbuild

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 fine, but I want to know if there is a way of referencing MyProp within Child.proj without Child.proj being called by Parent.proj.

I know I can declare the same property in Child.proj and this will get overriden when Child.proj is called by Parent.proj but I want to avoid repeating a property value.

like image 845
Matthew Dresser Avatar asked Jan 23 '23 13:01

Matthew Dresser


1 Answers

If you define your properties in an external project file then each of the projects can import the property settings.

Here's a very simple properties files called orders.properties which I am currently working on.

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

In my main build project I import the order properties in the orders.build.proj file and any subprojects that require it.

Here is the initial section of the main build file.

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

Hope this answers your question.

Kind Regards Noel

like image 69
Bigtoe Avatar answered Feb 02 '23 07:02

Bigtoe