如何在解决方案级别使用Microsoft.Net.Compilers? [英] How can I use Microsoft.Net.Compilers at solution level?

查看:441
本文介绍了如何在解决方案级别使用Microsoft.Net.Compilers?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开始使用 Microsoft.Net.Compilers 来简化我们的工作构建服务器.但是,通过将包添加到所有项目中,我只能使其在每个项目级别下运行.
这是有问题的,因为必须将包添加到每个新创建的项目中.这可能会导致代码在开发人员的机器(具有最新的编译器)上编译,但在构建服务器上失败的情况.我们有很多项目(超过100个),所以这是相对常见的.

I want to start using Microsoft.Net.Compilers to simplify work with our build server. However, I could only get it to work at a per-project level, by adding the package to all projects.
This is problematic because the package would have to be added to each new created project. This could lead to a case where the code compiles on the developer's machine (which has the latest compiler), but would fail on the build server. We have many projects (over 100), so this is relatively common.

是否可以在解决方案级别使用Microsoft.Net.Compilers?

Is there a way of using Microsoft.Net.Compilers at solution level?

如果没有受支持的方法,是否有我不需要在构建服务器上安装的命令行工具?或者,这不是这些工具的预期用途吗?

If there is no supported way, is there a command line tool I would not have to install on the build server? Alternatively, is this not an intended usage of these tools?

推荐答案

如果在VS 2017中(更新1,内部版本号> = 15.1.*),则可以使用集成的MSBuild机制PackageReference代替以前的packages.config以前仅适用于.net core和.net标准项目类型.请参阅

If in VS 2017 (update 1, build numbers >= 15.1.*) you can use the MSBuild integrated PackageReference mechanism instead of packages.config which was previously only available for .net core and .net standard project types. See the PackageReference documentation as well as the NuGet blog post announcing the support, especially the section "What about other project types that are not .NET Core?".

这个想法是从安装软件包并将其添加到packages.config进行还原,切换到仅在csproj文件中指定MSBuild项.可以在VS中为新项目进行设置: (动画来自上面链接的NuGet博客文章)

The idea is to switch from installing a package and adding it to packages.config for restore to just specifying an MSBuild items in the csproj file. This can be set up for new projects in VS: (animation is from the NuGet blog post linked above)

MSBuild 15的一项新功能是它自动支持在目录层次结构中包含特殊名称的文件.分别是Directory.Build.propsDirectory.Build.targets,它们将包含在项目文件内容的(属性)之前(目标)之后(.targets版本的一个错误,该错误针对即将发布修复程序的多目标项目).

A new feature of MSBuild 15 is that it supports automatically including files in the directory hierarchy that have special names. Those are Directory.Build.props and Directory.Build.targets which will get included before (props) and after (targets) your project file's content (there is a bug with the .targets version for multi targeting projects for which a fix is about to be released).

如果在解决方案级别创建具有以下内容的Directory.Build.props文件,则其下面目录层次结构中的所有项目将继承其内容,并且您可以将NuGet依赖项强制应用于每个项目:

If you create a Directory.Build.props file with the following content at the solution level, all projects in the directory hierarchy below it will inherit it's content and you can force a NuGet dependency onto each project:

<Project>
  <ItemGroup>
    <PackageReference Include="Microsoft.Net.Compilers" Version="2.1.0"/>
  </ItemGroup>
</Project>

这篇关于如何在解决方案级别使用Microsoft.Net.Compilers?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆