MSBuild从一个项目中提取属性到另一个项目

时间:2009-06-05 09:58:53

标签: msbuild

假设我有两个项目文件“Parent.proj”和“Child.proj”。如果我在Parent.proj中声明一个名为MyProp的属性,我可以使用以下代码将其传递给Child.proj:

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

这很好,但是我想知道在Child.proj中是否有一种方法可以引用MyProp,而Child.proj不会调用Child.proj。

我知道我可以在Child.proj中声明相同的属性,当Parent.proj调用Child.proj时会覆盖它,但我想避免重复属性值。

1 个答案:

答案 0 :(得分:8)

如果在外部项目文件中定义属性,则每个项目都可以导入属性设置。

这是一个非常简单的属性文件,名为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"/>

希望这能回答你的问题。

亲切的问候 诺尔

相关问题