在pubxml文件中更改publishUrl属性的MSBuild会在发布时抛出错误

时间:2017-04-13 17:12:56

标签: msbuild visual-studio-2017 pubxml

在VS2017中,我运行这个pubxml文件。

<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit http://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Date>$([System.DateTime]::Now.ToString(yyyyMMdd))</Date>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <LastUsedBuildConfiguration>Prod</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <Major>0</Major>
    <Minor>1</Minor>
    <Build>2</Build>
    <Publish>c:\</Publish>
    <publishUrl>$(Publish)</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
  </PropertyGroup>

  <Target Name="GetBuildUrl">
    <PropertyGroup>
      <In>$([System.IO.File]::ReadAllText('$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs'))</In>
      <Pattern>^\s*\[assembly: AssemblyVersion\(\D*(\d+)\.(\d+)\.(\d+)</Pattern>
      <Major>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[1].Value)</Major>
      <Minor>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[2].Value)</Minor>
      <Build>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[3].Value)</Build>
      <Publish>$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), _ReleaseRoot.txt))\FlightmapReleases\Releases\Flightmap $(Major).$(Minor)\Prod\$(Date) ($(Major).$(Minor).$(Build))\</Publish>
      <publishUrl Condition=" '$(Publish)' != '' ">$(Publish)</publishUrl>
      <publishUrl Condition=" '$(Publish)' == '' and '$(LastUsedBuildConfiguration)'!='' ">$(LastUsedBuildConfiguration)</publishUrl>
    </PropertyGroup>
  </Target>

  <Target Name="BeforeBuild" DependsOnTargets="GetBuildUrl">
    <Message Importance="High" Text="|" />
    <Message Importance="High" Text=" =================================================================================================" />
    <Message Importance="High" Text="    BUILD INFO                                                                                    " />
    <Message Importance="High" Text="    Version [$(Major).$(Minor).$(Build)] found in [$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs] " />
    <Message Importance="High" Text="    Build will be saved to [$(publishUrl)]                                                        " />
    <Message Importance="High" Text=" =================================================================================================" />
    <Message Importance="High" Text="|" />
  </Target>
</Project>

由于发布位置不在项目根目录中,因此它会搜索文件“_ReleaseRoot.txt”并将包含文件夹设置为root。消息传递目标为所有属性提供正确的值,因此我认为这是有效的。

当我运行发布消息时:

------ Build started: Project: Flightmap.Web, Configuration: Debug Any CPU ------   
|      
===================================================================
    BUILD INFO                                                                                    
    Version [5.4.0] found in [(correct project)\Properties\AssemblyInfo.cs]
    Build will be saved to [(correct location)\Prod\20170413 (5.4.0)\]
===================================================================

对我来说很好看。然后开始构建,说

Connecting to c:\...

并不奇怪
Publish failed. Target file:///c:/.

我怎样才能使这个工作?

1 个答案:

答案 0 :(得分:0)

我有类似的问题,它通过更改csproj文件来解决:

来自(可能与您略有不同,例如v4)

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v14.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />

更改为

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets" Condition="true" />

这是VS2017(15.82)和MSBuild 15(2019)的最新版本。