dotnet cli与新vs2017 msbuild的关系 [英] Relationship between the dotnet cli and the new vs2017 msbuild

查看:15
本文介绍了dotnet cli与新vs2017 msbuild的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着从 project.json 迁移到 VS2017 引入的新 csproj 格式,我很难理解 dotnet cli 和新的 msbuild 以及何时使用一个而不是另一个.

1) 要从命令行构建一个新的 csproj netstandard 库,我应该调用 dotnet cli(例如 dotnet restoredotnet build)或使用 msbuild(例如 msbuild ExampleNetstandard.sln).

2) 另外,我的理解是 msbuild 有两个版本,一个基于完整框架构建,另一个针对 dotnet core.这个对吗?我应该总是使用 dotnet 版本

3) dotnet cli 是独立的还是需要安装 msbuild?例如,当您安装 dotnet SDK 时,这是否也会安装 msbuild?如果是这样,这与使用 vs2017 安装的版本不同吗?

解决方案

问题

<块引用>

  1. 要从命令行构建新的 csproj netstandard 库,我应该调用 dotnet cli(例如 dotnet restore dotnet build)还是使用 msbuild(例如 msbuild ExampleNetstandard.sln).

两者都做得很好,因为当前 dotnet 是在 msbuild 之上构建的.所以这是一个品味问题.您还可以使用 dotnet CLI 调用 msbuild 任务.(dotnet msbuild <msbuild_arguments>)

一开始,所有 .NET 核心的东西都只在 dotnet 中,而不是在 msbuild 中.这很麻烦,因为很多已经在 msbuild 上构建的东西不能很好地与 dotnet 开箱即用(例如 Xamarin).所以他们将这些东西移到 msbuild 并在 msbuild 之上构建 dotnet.

dotnet 有一些 msbuild 中没有的功能,例如 dotnet new.在我看来,dotnetmsbuild 更容易使用,所以我更喜欢 dotnet.

为了更清楚,我在帖子末尾添加了 msbuilddotnet 之间的比较.

<块引用>

  1. 另外,我的理解是 msbuild 有两个版本,一个基于完整框架构建,另一个针对 dotnet 核心.这个对吗?我应该一直使用 dotnet 版本吗

只有一个 msbuild.dotnet CLI 正在使用 msbuild:

<块引用>

由于 CLI 使用 MSBuild 作为其构建引擎,我们建议将工具的这些部分编写为自定义 MSBuild 目标和任务,因为它们可以参与整个构建过程

msbuild.dll 其实就是 msbuild.exe,在属性中可以看到:

一些代码

如果您查看 dotnet CLI 的代码,您会看到它正在生成 msbuild 命令.

例如 dotnet restore,由 RestoreCommand dotnet CLI 中的类.

精简版:

公共类 RestoreCommand : MSBuildForwardingApp{...公共静态RestoreCommand FromArgs(字符串[] args,字符串msbuildPath = null){var result = parser.ParseFrom("dotnet restore", args);...var msbuildArgs = 新列表<字符串>{/NoLogo","/t:恢复",/ConsoleLoggerParameters:Verbosity=Minimal"};...返回新的RestoreCommand(msbuildArgs,msbuildPath);}公共静态 int 运行(字符串 [] 参数){恢复命令 cmd;尝试{cmd = FromArgs(args);}捕获(CommandCreationException e){返回 e.ExitCode;}返回 cmd.Execute();}...}

您可以看到 dotnet restore 只是在调用 msbuild/NoLogo/t:Restore/ConsoleLoggerParameters:Verbosity=Minimal


如果您检查 dotnet v1.0.0 RC2时的RestoreCommand,不是使用msbuild而是调用nuget 直接.

return NuGet3.Restore(args, quiet);

dotnetmsbuild

之间的映射

我在 dotnetmsbuild 之间做了一个映射.不完整,但重要的命令都在那里.

Dotnet |构建 |评论------------------------|--------------------------------|---------------------添加 ||------------------------|--------------------------------|---------------------构建 |/t:构建 |------------------------|--------------------------------|---------------------构建 --no-incremental |/t:重建 |------------------------|--------------------------------|---------------------清洁 |/t:干净 |------------------------|--------------------------------|---------------------完成 ||------------------------|--------------------------------|---------------------帮助 ||帮助!------------------------|--------------------------------|---------------------列表 ||------------------------|--------------------------------|---------------------迁移 |- |------------------------|--------------------------------|---------------------构建 ||转发所有------------------------|--------------------------------|---------------------新 ||------------------------|--------------------------------|---------------------纽吉特 ||*------------------------|--------------------------------|---------------------包装 |/t:包装 |------------------------|--------------------------------|---------------------发布 |/t:发布 |------------------------|--------------------------------|---------------------删除 ||------------------------|--------------------------------|---------------------恢复 |/NoLogo/t:恢复 ||/ConsoleLoggerParameters:详细程度=最小 |------------------------|--------------------------------|---------------------运行 |/nologo/冗长:安静 ||/p:配置=/p:目标框架 |------------------------|--------------------------------|---------------------斜线 ||不在 msbuild 中------------------------|--------------------------------|---------------------商店 |/t:撰写商店 |------------------------|--------------------------------|---------------------测试 |/t:VSTest/v:安静/nologo |------------------------|--------------------------------|---------------------测试 ||转发到 vstest.console.dll

* dotnet nuget:向 csproj 添加/删除包,同样是有限的 nuget.exe 集,请参阅 比较

PS SO 中没有降价表 :(

With the move from project.json to the new csproj format introduced with VS2017, I'm struggling to understand the difference between the dotnet cli and the new msbuild and when to use one over the other.

1) To build a new csproj netstandard library from the command line, should I be calling the dotnet cli (for example dotnet restore dotnet build) or use msbuild (for example msbuild ExampleNetstandard.sln).

2) Also, my understanding is that there are two versions of msbuild, one built on the full framework and another targeting dotnet core. Is this correct? Should I always use the dotnet version

3) Is dotnet cli standalone or does it require msbuild to be installed?. For instance when you install the dotnet SDK does this install msbuild as well? If so is this different to the version that is installed with vs2017?

解决方案

Questions

  1. To build a new csproj netstandard library from the command line, should I be calling the dotnet cli (for example dotnet restore dotnet build) or use msbuild (for example msbuild ExampleNetstandard.sln).

Both do fine as currently dotnet is built on top of msbuild. So it's a matter of taste. You could also call msbuild tasks by using the dotnet CLI. (dotnet msbuild <msbuild_arguments>)

In the beginning, all the .NET core stuff was only in dotnet and not in msbuild. This was cumbersome as a lot of stuff that was already built on msbuild wasn't working well with dotnet out of the box (e.g. Xamarin). So they moved the stuff to msbuild and build dotnet on top of msbuild.

dotnet has some features that aren't in msbuild, like dotnet new. In my opinion, dotnet is easier to use than msbuild, so I prefer dotnet.

To make it more clear, I have added a comparison between msbuild and dotnet at the end of my post.

  1. Also, my understanding is that there are two versions of msbuild, one built on the full framework and another targeting dotnet core. Is this correct? Should I always use the dotnet version

There is only one msbuild. dotnet CLI is using msbuild:

Since CLI uses MSBuild as its build engine, we recommend that these parts of the tool be written as custom MSBuild targets and tasks, since they can then take part in the overall build process

https://docs.microsoft.com/en-us/dotnet/articles/core/tools/extensibility

The older version of msbuild was lacking the .NET Core support. Maybe that's the other version ;)

I agree it's confusing, as it was very different a few months ago.

  1. Is dotnet cli standalone or does it require msbuild to be installed?. For instance when you install the dotnet SDK does this install msbuild as well? If so is this different to the version that is installed with vs2017?

I wasn't sure about this, but it was easy to test. I have removed all msbuild.exe and it still worked. Found out it's using the msbuild.dll in the SDK folder. e.g. "C:Program Filesdotnetsdk1.0.3MSBuild.dll"

If you remove that one, there is a proof:

msbuild.dll is actually msbuild.exe, as you can see in the properties:

Some code

If you look into the code of the dotnet CLI, you can see it's generating msbuild commands.

For example dotnet restore, is created by the RestoreCommand class inside dotnet CLI.

A stripped version:

public class RestoreCommand : MSBuildForwardingApp
{
    ...
    public static RestoreCommand FromArgs(string[] args, string msbuildPath = null)
    {
        var result = parser.ParseFrom("dotnet restore", args);
        ...
        var msbuildArgs = new List<string>
        {
            "/NoLogo",
            "/t:Restore",
            "/ConsoleLoggerParameters:Verbosity=Minimal"
        };
        ...
        return new RestoreCommand(msbuildArgs, msbuildPath);
    }

    public static int Run(string[] args)
    {
        RestoreCommand cmd;
        try
        {
            cmd = FromArgs(args);
        }
        catch (CommandCreationException e)
        {
            return e.ExitCode;
        }
        
        return cmd.Execute();
    }
    ...
}

You can see dotnet restore is just calling msbuild /NoLogo /t:Restore /ConsoleLoggerParameters:Verbosity=Minimal


If you check RestoreCommand in the time of dotnet v1.0.0 RC2, it wasn't using msbuild but was calling nuget directly.

return NuGet3.Restore(args, quiet);

Mapping between dotnet and msbuild

I made a mapping between dotnet and msbuild. It's not complete, but the important commands are there.

Dotnet                 | Msbuild                                    | Remarks                         
-----------------------|--------------------------------------------|---------------------------------
Add                    |                                            |         
-----------------------|--------------------------------------------|---------------------------------                        
Build                  | /t:Build                                   |  
-----------------------|--------------------------------------------|---------------------------------                                
Build --no-incremental | /t:Rebuild                                 |    
-----------------------|--------------------------------------------|---------------------------------                              
Clean                  | /t:clean                                   |                                 
-----------------------|--------------------------------------------|--------------------------------- 
Complete               |                                            |                                 
-----------------------|--------------------------------------------|--------------------------------- 
Help                   |                                            | Help!                           
-----------------------|--------------------------------------------|--------------------------------- 
List                   |                                            |                                 
-----------------------|--------------------------------------------|--------------------------------- 
Migrate                | -                                          |                                 
-----------------------|--------------------------------------------|--------------------------------- 
Msbuild                |                                            | Forwarding all                  
-----------------------|--------------------------------------------|--------------------------------- 
New                    |                                            |                                 
-----------------------|--------------------------------------------|--------------------------------- 
Nuget                  |                                            |  *
-----------------------|--------------------------------------------|--------------------------------- 
Pack                   | /t:pack                                    |                                 
-----------------------|--------------------------------------------|--------------------------------- 
Publish                | /t:publish                                 |                                 
-----------------------|--------------------------------------------|--------------------------------- 
Remove                 |                                            |                                 
-----------------------|--------------------------------------------|--------------------------------- 
Restore                | /NoLogo /t:Restore                         |
                       | /ConsoleLoggerParameters:Verbosity=Minimal |
-----------------------|--------------------------------------------|--------------------------------- 
Run                    | /nologo /verbosity:quiet                   |
                       | /p:Configuration=   /p:TargetFramework     |                                 
-----------------------|--------------------------------------------|--------------------------------- 
Sln                    |                                            | Not in msbuild                  
-----------------------|--------------------------------------------|--------------------------------- 
Store                  | /t:ComposeStore                            |                                 
-----------------------|--------------------------------------------|--------------------------------- 
Test                   | /t:VSTest /v:quiet /nologo                 |                                 
-----------------------|--------------------------------------------|--------------------------------- 
Vstest                 |                                            | Forwarding to vstest.console.dll

* dotnet nuget: Adding/removing packages to csproj, also limited set of nuget.exe, see comparison

PS no markdown tables in SO :(

这篇关于dotnet cli与新vs2017 msbuild的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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