dotnet 发布配置文件忽略 pubxml 文件

问题描述 投票:0回答:4

我有以下通过 Visual Studio 2019 创建的 pubxml 文件:

<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>

当我运行

dotnet.exe publish src\MyProject.Web.sln -p:PublishProfile=Properties\PublishProfiles\ProductionPublish.pubxml
发布时不包含任何内容并且发布发生在调试文件夹(调试构建)上。为什么会发生这种情况并且 pubxml 被忽略?

更新#1

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

以及 pubxml 的路径

src\MyProject.Web\Properties\PublishProfiles\ProductionPublish.pubxml
c# asp.net .net .net-core msbuild
4个回答
29
投票

您需要使用以下命令:

dotnet build -c Release /p:DeployOnBuild=true /p:PublishProfile=FolderProfile
  • build
    ,不是
    publish

  • 即使我的FolderProfile配置设置为Release,我也必须 包括

    -c Release
    因为否则构建了依赖项目 具有调试配置。

  • 如果在没有

    /p:DeployOnBuild=true
    的情况下调用,文件不会复制到目标位置。

  • 仅名称

    FolderProfile
    - 不带扩展名 - 就足够了
    配置文件。或者你可以给出路径
    /p:PublishProfile=Properties\PublishProfiles\FolderProfile.pubxml

请参阅 Microsoft Docs 中的文件夹发布示例


8
投票

在 VS2022 和 Core 6 上找到了一个解决方案,可以发布到特定文件夹,而无需在 CLI 上指示输出路径。

我创建了一个名为 IISC 的配置文件

如果打开该发布配置文件,您将看到 PublishUrl 属性,如下所示

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

在我的例子中,我正在发布到解决方案文件夹 bin 埃利斯...\IISC

技巧是添加另一个名为 PublishDir 的属性

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

现在您可以使用以下内容进行发布:

dotnet publish -c Release /p:PublishProfile=IISC

找到下面的我的完整配置文件,并添加特定于此配置文件和项目组的环境变量,以排除除 appsettings.json 和 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>

2
投票

我要回答那些像我一样确实想使用 publish 命令的人,因为它就是被设计来这样做的。

根据publish命令的Options部分,您可以使用选项configurationoutput来解决您的问题:

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

请注意,您想要的位置可以是绝对路径,您的配置文件名称只是配置文件的名称,没有“pubxml”,也没有相对路径(当且仅当配置文件位于“/Properties/PublishProfiles”中时”)。


0
投票

这对我有用......

 - task: DotNetCoreCLI@2         
        inputs:
          command: 'publish'
          projects: '$(project)'
          publishWebProjects: false
          arguments: '-c $(buildConfiguration) -o $(Build.ArtifactStagingDirectory)/$(buildConfiguration) /p:PublishProfile=MyProfile /p:password=$(MYPASSWD)'
© www.soinside.com 2019 - 2024. All rights reserved.