2009-06-05 71 views
3

说我有两个项目文件“Parent.proj”和“Child.proj”。如果我宣布一个属性Parent.proj称为MyProp我可以用下面的代码通过这Child.proj:MSBuild从一个项目提取属性

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

这是好的,但我想知道是否有儿童中引用MyProp的方式.proj没有Child.proj被Parent.proj调用。

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

回答

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

希望这回答你的问题。

亲切的问候 诺埃尔

+0

+1辉煌!将属性放在单独的文件中以防止双向依赖性是一个好主意。非常感谢! – 2009-06-05 10:47:38

+0

有没有办法调用任务以及哪些可以设置这些全局属性。创建名为buildDate的属性,然后用

+0

您可以随时通过属性函数在构建目标中的任何属性中添加时间戳。例如,在Target的PropertyGroup部分设置 $(DateTime.Now)。请参阅http://msdn.microsoft.com/en-us/library/dd633440.aspx。 – d3r3kk 2014-10-31 21:52:46