【问题标题】:Msbuild resolve project reference to custom project typeMsbuild 将项目引用解析为自定义项目类型
【发布时间】:2013-03-15 16:24:56
【问题描述】:

我为一种名为 clojure 的编程语言创建了一个自定义项目类型(cljproj 而不是 csproj)。项目编译时会输出多个 .dll 文件以及一些依赖的 .clj 文件。

这是通过覆盖 cljproj 文件中的默认 CoreCompile 目标来完成的。它基本上将所有需要编译的文件复制到 bin 目录,然后执行一个单独的应用程序来编译它们。

<Target Name="CoreCompile">
  <PropertyGroup>
    <ClojureNamespaces>@(Compile -> '%(RelativeDir)%(Filename)', ' ')</ClojureNamespaces>
  </PropertyGroup>
  <Copy SourceFiles="@(Compile)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" DestinationFiles="@(Compile -> '$(OutDir)%(RelativeDir)%(Filename)%(Extension)')" />
  <Exec WorkingDirectory="$(OutDir)" Command="&quot;$(ClojureRuntimesDirectory)\$(ClojureVersion)\Clojure.Compile&quot; $(ClojureNamespaces.Replace('\', '.'))" />
</Target>

我已将来自 c# 项目 (csproj) 的引用添加到我的 clojure 项目 (cljproj)。

<ProjectReference Include="..\Clojure ASP.Net MVC Controller Library1\Clojure ASP.Net MVC Controller Library1.cljproj">
  <Project>{8fe1995b-4b6d-4911-b563-a759467fdf53}</Project>
  <Name>Clojure ASP.Net MVC Controller Library1</Name>
</ProjectReference>

默认情况下,Visual Studio 不能正确解析项目引用,因为它假定只有一个输出,即 Clojure ASP.Net MVC Controller Library1.dll。 实际输出文件的示例是 MvcApplication1.Controllers.HomeController.dll 和 HomeController.clj

我想在不对 C# .csproj 文件进行任何更改的情况下完成这项工作,以便可以轻松地从任何 .csproj 文件中引用 .cljproj。

我尝试通过覆盖 GetTargetPath 目标来解决项目引用。

<Target Name="GetTargetPath" DependsOnTargets="$(GetTargetPathDependsOn)" Returns="@(TargetPath)">
  <ItemGroup>
    <TargetPath Include="$(TargetDir)\**\*.dll" />
    <!-- <TargetPath Include="$(TargetDir)\**\*.clj" /> -->
  </ItemGroup>
</Target>

如果我使用 *.dll 设置 TargetPath,它会起作用并将 .dll 文件复制到 c#.csproj 输出目录。它甚至将 .pdb 文件复制到该目录,尽管我没有将它们添加到 TargetPath。但是,如果我取消注释 *.clj TargetPath,CSC 会抱怨 .clj 文件已损坏(可能是因为它们是纯文本,而不是 .net 程序集)。

我很乐意使用复制命令而不是覆盖 TargetPath,但是我不确定要使用哪个变量将它们输出到目录,因为 $(outdir) 为我提供了自定义项目的 bin (. cljproj) 而不是试图解析项目引用 (.csproj) 的 c# 项目的 bin。我不确定除了 GetTargetPath 之外要覆盖哪些其他目标,因为在编译 c# (.csjproj) 项目时不会调用大多数 clojure 项目 (.cljproj) 目标,例如: .cljproj:AfterBuild 仅在直接编译 cljproj 时调用,不是在编译具有对 .cljproj 的项目引用的 .csproj 时。

【问题讨论】:

    标签: clojure msbuild


    【解决方案1】:

    我能够通过覆盖 GetCopyToOutputDirectoryItems 目标来使其工作。

    <Target Name="GetCopyToOutputDirectoryItems" Returns="@(CopyToOutputDirectoryItemsWithTargetPath)">
      <ItemGroup>
        <CopyToOutputDirectoryItems Include="$(TargetDir)\**\*.clj">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </CopyToOutputDirectoryItems>
      </ItemGroup>
      <AssignTargetPath Files="@(CopyToOutputDirectoryItems)" RootFolder="$(TargetDir)">
        <Output TaskParameter="AssignedFiles" ItemName="CopyToOutputDirectoryItemsWithTargetPath" />
      </AssignTargetPath>
    </Target>
    

    【讨论】:

      猜你喜欢
      • 2016-04-26
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      • 1970-01-01
      • 2013-06-19
      • 2015-05-07
      • 1970-01-01
      相关资源
      最近更新 更多