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

MSBuild将属性从一个项目提取到另一个项目,msbuild,Msbuild,假设我有两个项目文件“Parent.proj”和“Child.proj”。如果我在Parent.proj中声明一个名为MyProp的属性,我可以使用以下代码将其传递给Child.proj: <MSBuild Projects="Child.proj" Targets="dostuff" Properties="MyProp=MyValue" /> 这很好,但我想知道是否有一种方法可以在Child.proj中引用MyProp,而不必由Parent.proj调用Child.proj

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

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

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


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

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

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

99
0
0
$(订单版本号)。$(订单版本号)。$(订单版本号)。$(订单版本号发布)
$(主要订单)$(次要订单)$(发布订单)
$(订单\u根)\btq.orders.database
假的
sqlexpress
YYYYYY
xxxxxx
$(计算机名称)\u btq\u orders\u v$(orders\u ver\u db)
在我的主构建项目中,我将订单属性导入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"/>

$(MSBuildProjectDirectory)\。。
$(MSBuildProjectDirectory)
希望这能回答你的问题

问候
诺埃尔

+1才华横溢!这是一个好主意,将属性放在一个单独的文件中,以防止双向依赖。非常感谢!是否有一种调用任务的方法可以设置这些全局属性。。e、 g.创建名为buildDate的属性,然后使用task设置buildDate?您始终可以通过属性函数在构建目标内的任何属性中放置时间戳。例如,在目标的PropertyGroup部分中设置$(DateTime.Now)。看见