MSBuild发布dotnet核心应用程序

ect*_*cth 5 msbuild .net-core

我的设置是:我有一个解决方案,其中包含不同的dotnet4.6应用程序(服务)。现在,我们在此解决方案中添加了一个dotnet核心项目。我可以构建和调试它,但这不会创建可执行文件。在Visual Studio中,我可以右键单击->发布...。我创建了两个配置文件(x86和x64),它们应该在/ bin / Publish / x86或/ x64中创建漂亮的二进制文件。在VS中有效。该应用程序是独立的,可以在未准备好的其他计算机上运行。

但是现在我需要将该过程移至构建服务器。我搞砸了,dotnet publish但最后我被卡住了,因为该解决方案的其他组件不是干净的dotnet核心,因此构建失败。因此,我需要坚持使用MSBuild。

当前尝试是: "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe" NewProject\NewProject.csproj /p:DeployOnBuild=true /p:UsePublishProfile=true /p:PublishProfile=x64Profile

这表明它已成功完成构建,但是我看不到任何结果。如果我删除所有属性并仅调用msbuild和* .csproj,也没有任何区别。它只是在bin / Debug中以dll(而不是exe)构建新项目。

我也弄乱了p:PublishProfile="NewProject\Properties\PublishProfiles\x64Profile.pubxml"/p:PublishUrl="NewProject\bin\Publish\x64"但它没有任何改变。

我读了几篇关于SO的文章,告诉我VS不仅使用参数调用msbuild,而且还进行内部API调用。不过,我需要一个解决方案。我需要构建服务器来创建可执行文件。有没有一种方法可以触发msbuild创建该对象?

ect*_*cth 9

哦,天哪,我现在搜索了2-3天。并且-与往常一样,在StackOverflow上-询问后不久,我自己找到了一个有效的答案。

tl; dr:

Project.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp2.1</TargetFrameworks>
    <TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>  
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <RootNamespace>Akka.RetUsLight.Exporter</RootNamespace>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    <RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers>
  </PropertyGroup>

  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets" />
...
Run Code Online (Sandbox Code Playgroud)

MSBuild命令:

msbuild Project\Project.csproj -t:restore /t:Build;Publish /p:Configuration=Release /p:Platform=x86 /p:PublishProfile=x86Profile /p:OutputPath=bin/Publish/x86 (与x64相同)

说明:

我认为是dotnet build/publish要我更改TargetFrameworks为的命令TargetFramework。但是对于MSBuild,这是错误的。而且dotnet在这里没有用,因为解决方案混合了dotnet核心和dotnet框架。因此必须解决。

<RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers>命令需要。我将其添加到* .csproj,因为我知道(目前)仅针对Windows构建并且需要两个版本。

我真的不知道为什么需要此行,<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets" />但是没有发布并且使用PublishProfiles不能按预期工作。

帮助我到达此处的链接:(未排序)

https://github.com/Microsoft/msbuild/issues/1901

https://github.com/aspnet/vsweb-publish/issues/22

如何使用msbuild发布Web?

仅适用于Windows x64的ASP.NET Core应用程序(.NET Framework)在project.assets.json中出现错误

配置msbuild输出路径


Jon*_*n R 5

我也有一个噩梦dotnet publish,即Visual Studio IDE 的构建和命令之间的不一致,这只能通过使用 msbuild.exe 来解决。此外,使用/p:PublishProfiles=theXMLthatVSgenerates.xml从未奏效,所以我不得不将每个选项都分解到 msbuild 命令行中。

以下是对我有用的内容:

"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\msbuild.exe" C:\Users\xxxx\Source\Repos\netcore-agent1\CoreAgent1\CoreAgent1.csproj /t:Restore;Rebuild;Publish /p:PublishSingleFile=True /p:SelfContained=True /p:PublishProtocol=FileSystem /p:Configuration=Release /p:Platform=x64 /p:TargetFrameworks=netcoreapp3.1 /p:PublishDir=bin\Release\netcoreapp3.1\publish\win-x64 /p:RuntimeIdentifier=win-x64 /p:PublishReadyToRun=False /p:PublishTrimmed=False
Run Code Online (Sandbox Code Playgroud)

  • 解决了我的问题。需要指出的是,当使用“/p:SelfContained=True”时,还必须设置 RuntimeIdentifier。示例:“/p:RuntimeIdentifier=win-x64” // 是的,您已经将其包含在选项列表中。 (5认同)
  • 几天来一直在寻找这个解决方案 - 似乎“dotnetpublish”的作用与从 Visual Studio 发布不同。这解决了我的问题 - 非常感谢! (3认同)