2009-10-27 63 views
0

我正在嘗試編寫一個MSBuild任務以使用FluentNhibernate映射來構建數據庫。在自定義MSBuild任務中傳遞程序集引用

任務的代碼目前看起來像這樣...

public class CreateDatabase : Task 
{ 
    [Required] 
    public string ConfigFile 
    { get; set; } 

    [Required] 
    public string MappingAssemblyName 
    { get; set; } 

    public override bool Execute() 
    { 
     var mappingAssembly = Assembly.Load(MappingAssemblyName); 

     var config = new Configuration(); 
     config.Configure(ConfigFile); 

     var fluentConfig = Fluently.Configure(config) 
      .Mappings(m => m.FluentMappings.AddFromAssembly(mappingAssembly)); 

     var sessionSource = new SessionSource(fluentConfig); 

     sessionSource.BuildSchema(); 

     return true; 
    } 
} 

和MSBuild的使用看起來像這樣...

<ItemGroup> 
    <Content Include="hibernate.cgf.xml" /> 
    </ItemGroup> 
    <UsingTask AssemblyFile="..\lib\MyUtilities.MSBuild.dll" TaskName="CreateDatabase" /> 
    <Target Name="Build" > 
    <CreateDatabase ConfigFile="@(Content)" MappingAssemblyName="MyMappingAssemlyName" /> 
    </Target> 

但現在我被困

毫不奇怪,Assembly.Load失敗是因爲包含我的Fluent映射('MyMappingAssemly')的程序集不存在。

假設Fluent映射是在我的解決方案中的另一個項目中定義的,告訴我有關映射程序集的MSBuild任務的最佳方法是什麼?我想我可能會使用'MappingAssemblyName'屬性走錯路。

回答

1

我假設你想獲得項目輸出的路徑,該項目定義了你的'流暢映射'。您可以使用「GetTargetPath」的目標是這樣的:

<MSBuild Projects="..\path\to\projectfile" Targets="GetTargetPath"> 
    <Output TaskParameter="TargetOutputs" ItemName="ProjectPath"/> 
</MSBuild> 

您可能需要設置配置/平臺爲目標平臺是這樣的:

Properties="Configuration=$(Configuration); Platform=$(Platform)" 

但是,這將正常工作只有在配置/平臺的引用項目匹配當前項目。如果解決方案文件設置了不同的值,則必須使用AssignProjectConfiguration任務。

因爲,我不確定這是否正是你想要的,如果需要,我會在此停下來並在稍後添加更多信息。

+0

謝謝Ankit,這得到了很多幫助。儘管看起來我不得不使用「PropertyName」屬性,而不是「ItemName」。 我現在有一個單獨的問題。我的自定義任務依賴於其他第三方程序集。如何確保在MSBuild執行我的任務時存在這些程序集?添加了對項目的引用,調用該任務似乎沒有做到這一點。 – sandy 2009-10-28 10:47:02

+0

添加此問題http://stackoverflow.com/questions/1636521/custom-msbuild-with-other-dependencies – sandy 2009-10-28 11:06:42

相關問題