2009-10-27 65 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

相关问题