【发布时间】:2017-08-09 06:19:07
【问题描述】:
我正在尝试创建将所有已发布文件复制到各种文件夹的发布配置文件。不幸的是,我读到无法通过publishUrl 直接执行此操作,建议发布到一个文件夹,从中复制所有文件。我设法编写了复制目标功能,但是目标运行的顺序是错误的。
我正在尝试通过 Build > Publish Web 直接从 VisualStudio 2015 运行发布。
这是我的发布资料:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebPublishMethod>FileSystem</WebPublishMethod>
<LastUsedBuildConfiguration>Debug</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<LaunchSiteAfterPublish>False</LaunchSiteAfterPublish>
<ExcludeApp_Data>True</ExcludeApp_Data>
<publishUrl>obj\Package\PackageTemp</publishUrl>
<DeleteExistingFiles>True</DeleteExistingFiles>
<ExcludeFilesFromDeployment>Web.config;package.json;packages.config;Typescript\**;DynamicCss\**;gulpfile.js;</ExcludeFilesFromDeployment>
</PropertyGroup>
<Import Project="AdditionalResources.targets" />
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>AdditionalResources</CopyAllFilesToSingleFolderForPackageDependsOn>
</PropertyGroup>
<ItemGroup>
<DestLocations Include="D:\PUBLISH_TEST\" />
<DestLocations Include="D:\PUBLISH_TEST2\" />
</ItemGroup>
<Target Name="CopyToDeployFolders" AfterTargets="CopyAllFilesToSingleFolderForPackage" Outputs="%(DestLocations.Identity)">
<ItemGroup>
<PublishedFiles Include="obj\Package\PackageTemp\**\*" />
</ItemGroup>
<Message Importance="high" Text="FilesToCopy: @(PublishedFiles)" />
<PropertyGroup>
<_DeployPathIdentity>%(DestLocations.Identity)</_DeployPathIdentity>
</PropertyGroup>
<Copy SourceFiles="@(PublishedFiles)" DestinationFiles="@(PublishedFiles->'$(_DeployPathIdentity)%(RecursiveDir)%(Filename)%(Extension)')" />
</Target>
</Project>
它的运行顺序如下:
Build
AdditionalResources
CopyToDeployFolders
Publish folder
这是有问题的,因为我的临时文件夹尚未创建/发布。我试图让我的 CopyToDeployFolders 目标覆盖 AfterPublish 并尝试使用各种不同的 AfterTargets,但它们都不起作用 - 有时我的目标未执行或仍在发布文件夹之前运行。
我试图通过查看来找出运行的目标 c:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.Publishing.targets 如this answer Why does MSBuild ignore my BeforePublish target?所示,但没有找到正确的,尝试将AfterTarget更改为不同的,但结果仍然相同。
编辑: 带有诊断级别的输出:
2> Target "GatherAllFilesToPublish" in file "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.Publishing.targets" from project "C:\MyProject\MySite\MySite.csproj" (entry point):
2> Done building target "GatherAllFilesToPublish" in project "MySite.csproj".
2>Done building project "MySite.csproj".
2>Deleting existing files...
2>Publishing folder /...
... files being published ...
2>Web App was published successfully file:///C:/MyProject/MySite/MySite/obj/Package/PackageTemp
2>
编辑2: 添加 AdditionalResources.targets 的内容
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="AdditionalResources">
<ItemGroup>
<AddFiles Include="$(ProjectDir)\App_Themes\**\Basic\Dynamic.css">
<CustomPath>App_themes\</CustomPath>
</AddFiles>
<AddFiles Include="C:\y\SharedDLL\Clients\Web\bin\**\*.dll">
<CustomPath>bin\</CustomPath>
</AddFiles>
<AddFiles Include="C:\y\SharedDLL\Clients\Web\resources\**\ctrl_res\**\*.*" />
<AddFiles Include="C:\y\SharedDLL\Clients\Web\resources\js\**\*.*">
<CustomPath>js\</CustomPath>
</AddFiles>
<AddFiles Include="C:\y\SharedDLL\Clients\Web\resources\**\ClientBin\**\*.xap" />
<AddFiles Include="C:\y\SharedDLL\Clients\Web\resources\FileUpload\**\*.js">
<CustomPath>js\</CustomPath>
</AddFiles>
<!-- Minified files should be copied last to override non-minified ones -->
<AddFiles Include="$(ProjectDir)\MINIFIED_FILES\**\*.*" />
</ItemGroup>
<ItemGroup>
<FilesForPackagingFromProject Include="%(AddFiles.Identity)">
<DestinationRelativePath>%(AddFiles.CustomPath)%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
</Project>
【问题讨论】:
-
在 msbuild 详细程度设置为诊断的情况下运行,它将显示所有目标,然后只需选择合适的目标与 AfterTargets 一起使用。
-
@stijn 试过了,但是文件开始发布时没有目标。在没有输出的情况下更新了我的问题。
-
@CrudaLilium,我用你的发布配置文件创建了一个示例,经过测试我发现目标 CopyToDeployFolders 在发布后执行(我评论了目标 AdditionalResources,因为我没有 AdditionalResources.targets 文件)。那么您是否介意与我们分享 AdditionalResources.targets 文件或 onedrive 的测试样本,请在此处分享链接,以便我们重现此问题。
-
@Leo-MSFT 用 AdditionalResources.targets 的内容更新了我的问题,因为它不是太大,它应该添加不属于项目的其他文件,但对站点来说是必需的。我没有包含它,因为我认为它与问题无关,我会尝试将其删除并重新发布,看看它是否影响订单。
-
注释掉 AdditionalResources 目标的导入和使用,以及将 AfterTarget 更改为“CopyToDeployFolders”,但顺序仍然相同。似乎我所有的目标都在 GatherAllFilesToPublish 目标中运行,CopyToDeployFolders 也是其中的一部分,但文件是在它之后发布的。
标签: visual-studio-2015 msbuild publish