.NET Core + MSBuild:如何将一个 csproj 与多个 project.json 文件相关联?

.NET Core + MSBuild: How to associate a csproj with multiple project.json files?

编辑 3: 看起来我正在尝试做的事情不受支持;实际上不可能跨配置 'share' project.json 文件。我用

代替了这个问题

edit 2: 是的,我的怀疑是正确的。 ProjectJsonProjectLockJson 实际上并不是特殊的 MSBuild 属性,它们的值被用于另一个导入的 MSBuild 项目以完成实际工作。当我有更新时,我会 post 更多关于这个(希望以答案的形式)。

编辑: 我已经发布了整个项目 to GitHub 这样你们就可以重现了。只是 git clone 存储库,在 VS 中打开它,开始构建,你应该会遇到错误。

原版Post

我正在尝试创建一个基于 .NET Core 的可移植 class 库。我在将多个 project.json 文件与我的 csproj 相关联时遇到问题。这是我的目录结构:

LibFoo
|
---- LibFoo.csproj
|
---- project.json
|
---- Platforms
     |
     ---- Net45
     |    |
     |    ---- project.json
     |
     ---- Profile32
          |
          ---- project.json

我有两个 MSBuild 平台(不包括 AnyCPU),Net45Profile32。基本上,当它设置为 .NET Framework 4.5 时,我希望它编译包含根目录中的 project.json, [=18= 中的 project.json ].当它设置为 Profile32 时,我希望它使用 Platforms/Profile32 中的 project.json 来做到这一点。

我的问题是,如何让它在 MSBuild 中运行?这是我的 csproj 文件的相关片段:

<ItemGroup Condition=" '$(Platform)' == 'Net45' ">
  <!-- Target WPF apps -->
  <TargetPlatform Include=".NETFramework, Version=4.5" />
</ItemGroup>
<ItemGroup Condition=" '$(Platform)' == 'Profile32' ">
  <!-- Target Windows 8.1 Universal apps -->
  <TargetPlatform Include="Windows, Version=8.1" />
  <TargetPlatform Include="WindowsPhoneApp, Version=8.1" />
</ItemGroup>
<ItemGroup>
  <None Include="Platforms\Net45\project.json" />
  <None Include="Platforms\Profile32\project.json" />
</ItemGroup>
<PropertyGroup>
  <ProjectJsonRoot>Platforms$(Platform)</ProjectJsonRoot>
  <ProjectJson>$(ProjectJsonRoot)\project.json</ProjectJson>
  <ProjectLockJson>$(ProjectJsonRoot)\project.lock.json</ProjectLockJson>
</PropertyGroup>
<ItemGroup>
  <!-- Include a common project.json for shared configs -->
  <None Include="project.json" />
</ItemGroup>

出于某种原因,ProjectJsonProjectLockJson 部分似乎无法正常工作。我怀疑他们是问题所在,因为我在 MSBuild, but CoreFX 上找不到他们的任何官方文档,似乎在他们的 csproj 文件中非常成功地使用了它。我做错了什么吗?

问题是您应该从 .csprojs 转移到 .xprojs。然后每个 .xproj 可以有单个 project.json 可以针对多个框架。

很棒的文章:http://blog.marcgravell.com/2015/11/the-road-to-dnx-part-1.html

这是我的示例:https://github.com/adamsitnik/BenchmarkDotNet

对于偶然发现此问题寻求帮助的人,请遵循 答案。对于绝大多数用例,解决方案只是从 csproj 切换到 xproj,因此您的 project.json 文件可以针对多个框架。

唉,对于我的情况(使用我库中的 Windows 运行时 API),不幸的是这是不可能的,因为 project.json 没有访问 WinRT 的内置方法API,即使你正在为他们的目标框架编译。例如,如果您这样做:

"frameworks": {
    ".NETPortable,Version=v4.6,Profile32": {
        "frameworkAssemblies": {
            "Windows": { "type": "build" }
        }
    }
}

它不会编译,尽管编译器会很乐意为您找到像 System.RuntimeSystem.Linq 这样的程序集。

我最后在 CoreFX 回购 here. It seems that the way the CoreFX team does this (for example you will find multiple project.jsons in a project like this) is by creating a custom MSBuild task that allows them to override the project.json, which they store the code for in their buildtools 回购上提出了一个关于这个的问题。

我真的不想创建我自己的 MSBuild 任务,也不想使用 .NET 构建工具来构建我自己的存储库,所以我最终只是以老式的方式创建了多个 csproj 文件。