.net - 如何 “dotnet pack”一个已经编译的项目

标签 .net msbuild .net-core nuget dotnet-cli

我正在尝试为dotnet core 2.0项目设计一个生成脚本,该脚本执行以下操作

  • 清除输出目录
  • 使用-o bin构建解决方案\发布
  • 使用dotnet vstest
  • 运行单元测试
  • 用dotnet pack
  • 创建一个Nuget包

    因为我知道源代码已在步骤1-3中构建和测试,所以我不想为nuget包重建代码,所以我指定--no-build和--no-restore

    我遇到的困难是,在创建软件包时,因为我没有构建,并且输出目录设置为bin\Publish-pack命令正在bin\Debug目录中寻找项目。

    有没有一种方法可以设置dotnet pack命令来知道在哪里查找已编译的对象?

    这是我的构建脚本的示例
    dotnet clean ..\MySolution.sln -o bin/Publish/
    dotnet build ..\MySolution.sln /p:Configuration=Release -o bin/Publish/
    dotnet vstest ..\MySolution.UnitTests\bin\Publish\MySolution.UnitTests.dll
    dotnet pack --no-build --no-restore ..\MySolution.ConsoleApp\MySolution.csproj -o bin\Publish\Nuget\
    
    ....
    
    error : The file 'D:\MySolution.ConsoleApp\bin\Debug\netcoreapp2.0\MySolution.ConsoleApp.runtimeconfig.json' to be packed was not found on disk
    

    根据Microsoft Docs on dotnet pack

    "By default, dotnet pack builds the project first. If you wish to avoid this behavior, pass the --no-build option. This is often useful in Continuous Integration (CI) build scenarios where you know the code was previously built."



    所以我希望这是可能的,并且我遗漏了一些显而易见的东西。任何帮助,将不胜感激。

    最佳答案

    这样做的原因是dotnet pack --no-build选项将尝试使用以前构建的输出,但是找不到,因为您是在非标准输出路径上构建的,而pack逻辑需要在生成的输出中定位一些生成的 Assets 。
    -opack命令的build选项在内部有所不同,但是您可以将pack命令更改为:

    dotnet pack --no-build --no-restore ..\MySolution.ConsoleApp\MySolution.csproj -o bin\Publish\Nuget\ /p:OutputPath=bin\Publish\
    

    这将使用bin\Publish目录中内置的输出,并将其放入bin\Publish\Nuget\中的Nuget输出目录中

    关于.net - 如何 “dotnet pack”一个已经编译的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50232375/

    相关文章:

    msbuild - MSBuild 文件是否有标准文件扩展名?

    c# - .Net AsyncStateMachine 自定义实现行为

    c# - 如何将 System.Windows.Control WebBrowser.Document 转换为 mshtml.MSHTMLDocumentClass?

    c# - System.Net.Http.HttpClient 如何选择认证类型?

    C#/.NET(核心): Is the type system safe enough to prevent SQL injections?

    linux - 如何在 Linux 上的 .Net Core 中捕获双页错误(内存映射文件读取错误)?

    msbuild - 从 Teamcity 更新 appsettings.json 文件

    c# - 如何从键盘上读取字符?

    visual-studio - ##[警告]未找到 Visual Studio 版本 '14.0'。回退到版本 '15.0'

    c# - Microsoft.TeamTest.targets 中的 MSBuild NullReferenceException