dotnet publish profile ignores pubxml file

Viewed 3817

I have the following pubxml file which I created via Visual Studio 2019:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <PublishProvider>FileSystem</PublishProvider>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <ProjectGuid>143a6329-27d9-474e-9521-835be634c293</ProjectGuid>
    <SelfContained>true</SelfContained>
    <publishUrl>bin\Release\netcoreapp3.1\publish\</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
  </PropertyGroup>
</Project>

When I run dotnet.exe publish src\MyProject.Web.sln -p:PublishProfile=Properties\PublishProfiles\ProductionPublish.pubxml Release does not contain anything and publish happens on Debug folder (debug build). Whys does this happens and pubxml is ignored?

Update #1

Structure
src\MyProject\MyProject.csproj
src\MyProject.Utils\ect.Utils.csproj
src\MyProject.Web\MyProject.Web.csproj
src\MyProject.Web.sln

and the path of the pubxml

src\MyProject.Web\Properties\PublishProfiles\ProductionPublish.pubxml
3 Answers

You need the use following command:

dotnet build -c Release /p:DeployOnBuild=true /p:PublishProfile=FolderProfile
  • It's build, not publish.

  • Even if my FolderProfile's configuration is set to Release, I had to include -c Release because otherwise dependent projects were built with debug configuration.

  • Files not copied to target location if called without /p:DeployOnBuild=true.

  • Just the name FolderProfile -without extension- is enough for the
    profile file. Or you can give the path
    /p:PublishProfile=Properties\PublishProfiles\FolderProfile.pubxml

See Folder publish example from Microsoft Docs.

Found a solution on VS2022 and Core 6 to publish to an specific folder without indicating the output path on the CLI.

I created a profile called IISC

enter image description here

If you open that publish profile, you will see the PublishUrl property as follows

<PublishUrl>bin\Release\net6.0\publish\IISC</PublishUrl>

In My case I'm publishing to the solution folder bin\releas....\IISC

The trick is to add another propery called PublishDir

<PublishDir>bin\Release\net6.0\publish\IISC</PublishDir>

Now you can publish with this:

dotnet publish -c Release /p:PublishProfile=IISC

Find Bellow My complete Profile with and addition of an environment variable specific for this profile and and item group to exclude all the appsettings except for appsettings.json and appsettings.IISC.json

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
  <PropertyGroup>
    <ASPNETCORE_ENVIRONMENT>iisc</ASPNETCORE_ENVIRONMENT>
    <DeleteExistingFiles>true</DeleteExistingFiles>
    <ExcludeApp_Data>false</ExcludeApp_Data>
    <LaunchSiteAfterPublish>true</LaunchSiteAfterPublish>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <PublishProvider>FileSystem</PublishProvider>
    <PublishUrl>bin\Release\net6.0\publish\IISC</PublishUrl>
    <PublishDir>bin\Release\net6.0\publish\IISC</PublishDir>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <SiteUrlToLaunchAfterPublish />
    <TargetFramework>net6.0</TargetFramework>
    <ProjectGuid>3e4c25a6-2051-4ccc-a518-645d46d120dd</ProjectGuid>
    <SelfContained>false</SelfContained>
  </PropertyGroup>

    <ItemGroup>
        <Content Update="appsettings.*.json">
            <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </Content>
        <Content Update="appsettings.$(ASPNETCORE_ENVIRONMENT).json">
            <CopyToPublishDirectory>Always</CopyToPublishDirectory>
        </Content>
    </ItemGroup>
    <ItemGroup>
        <Folder Include="Properties\PublishProfiles\" />
    </ItemGroup>

</Project>

I'm gonna answer for those who, like me, do want to use the publish command, because it's been designed to do so.

According to Options section on publish command, you can use the options configuration and output to solve your problem:

dotnet publish -c Release -o "<where you want>" -p:PublishProfile=<your profile name>

Note that where you want can be an absolute path and your profile name is only the name of profile, without "pubxml" and without the relative path (IF AND ONLY IF the profile is in "<project_folder>/Properties/PublishProfiles").

Related