Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild publish dotnet core application

My setup is: I have a solution that had different dotnet4.6 applications (services) in it. Now we added a dotnet core project inside this solution. I can build and debug it, but this doesn't create an executable. In Visual Studio I can rightclick -> Publish... it. I created two profiles (x86 and x64) that should create nice binaries in /bin/Publish/x86 or /x64. In VS this works. The application is self-contained and works on different unprepared machines.

But now I Need to move that process to the buildserver. I messed around with dotnet publish but in the end i get stuck because other components of the solution are not clean dotnet core and so the build fails. So I need to stick with MSBuild.

The current attempt is: "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.

This says it finished building successfully, but I don't see any results. Also it doesn't make any difference, if I remove all properties and just call msbuild and *.csproj. It just builds the new project in bin/Debug, as dll, not exe.

I also messed around with p:PublishProfile="NewProject\Properties\PublishProfiles\x64Profile.pubxml" and /p:PublishUrl="NewProject\bin\Publish\x64" but it doesn't change anything.

I read a few articles on SO, telling that VS doesn't just call msbuild with parameters but does internal API calls. Still, I need a solution. I need the build server to create an executable. Is there a way to trigger msbuild to create thath?

like image 991
ecth Avatar asked Mar 11 '19 09:03

ecth


People also ask

Does MSBuild work with .NET Core?

NET Core applications are typically built using MSBuild or dotnet.exe . MSBuild and dotnet.exe don't require Visual Studio to be installed, but in order to use them, the following prerequisites are required on a build server: Visual Studio Build Tools installed.

How do I publish a .NET Core 3.1 application?

Published your ASP.NET Core Web API as FDD So open Visual Studio and Go to File -> New -> Project. Select ASP.NET Core Web Application and click on Next. Give the proper name to your project and click on Create.


1 Answers

Oh man, I searched for 2-3 days now. And - as always on StackOverflow - shortly after asking I found a working answer myself.

tl;dr:

Project.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp2.1</TargetFrameworks>
    <TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>  
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <RootNamespace>Company.Toolset.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" />
...

MSBuild command:

msbuild Project\Project.csproj -t:restore /t:Build;Publish /p:Configuration=Release /p:Platform=x86 /p:PublishProfile=x86Profile /p:OutputPath=bin/Publish/x86 (and the same for x64)

Explanation:

I think it was the dotnet build/publish command that wanted me to change TargetFrameworks to TargetFramework. But for MSBuild this is wrong. And dotnet wasn't working here, as the solution is mixing dotnet core and dotnet framework. So that had to be fixed.

The <RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers> was needed by the command. I added it to the *.csproj because I know that I build for windows only (at the moment) and that I need both versions.

I don't really know why I needed this line <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets" /> but without this publishing and using the PublishProfiles didn't work as expected.

Links that helped me to get here: (not sorted)

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

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

How to Publish Web with msbuild?

ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json

Configure MSBuild output path

like image 58
ecth Avatar answered Sep 18 '22 14:09

ecth