问题描述
我有一个为控制台项目(.NET Core 2.1)创建的发布配置文件Staging.pubxml
,如下所示:
I have a publish profile Staging.pubxml
created for a console project (.NET Core 2.1), as follows:
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Staging</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>netcoreapp2.1</TargetFramework>
<PublishDir>bin\Release\netcoreapp2.1\publish\</PublishDir>
<SelfContained>false</SelfContained>
<_IsPortable>true</_IsPortable>
</PropertyGroup>
<Target Name="CustomActionsAfterPublish" AfterTargets="AfterPublish">
<Message Text="First occurrence" Importance="high" />
</Target>
</Project>
当我发布它时(通过VS2019);发布成功完成.但是我没有在输出窗口中看到文本:First occurrence
.
When I publish it (via VS2019); publish completes successfully. Yet I'am not seeing the text: First occurrence
in the output window.
在VS中,MSBuild输出的详细程度设置为Diagnostic
.
In VS, MSBuild output verbosity is set to Diagnostic
.
我做错了吗?
推荐答案
1.朋友,.pubxml文件不是定义自定义目标的好地方.
1.Hi friend, the .pubxml file is not a good place to define custom Target.
根据目标名称,您要在构建目标之后运行目标.对于这种情况:总是在项目文件中定义自定义目标.请尝试right-click your project
并选择edit xxx.csproj
,然后将目标添加到其中.然后重新加载项目并发布,然后您可以在构建输出中找到First occurrence
消息.
According to your target name, you want to run the target after build target. For this situation: always we define the custom target in project file. Please try right-click your project
and choose edit xxx.csproj
, and add the target into it. Then reload the project and publish, then you can find the First occurrence
message in the build output.
在测试完in.csproj文件后,无法将目标命名为AfterBuild,您可以将其更改为:
And after my test, in.csproj file, the target can't be named AfterBuild, you can change it to:
<Target Name="CustomTargetName" AfterTargets="build">
<Message Text="First occurrence" Importance="high" />
</Target>
2.此外,尽管我们不建议在.pubxml中添加它,但您的使用方式也可以在VS2019中使用.但这确实有效.由于无法在输出窗口中获取消息的原因,请检查在发布过程中是否真的调用了Staging.pubxml.
2.Also, the way you use can work in VS2019 though we don't suggest adding it in .pubxml. But it indeed works. For the reason why you can't get the message in output window, please check if the Staging.pubxml is really called during the publish process.
(将PublishDir的值更改为另一个文件夹,然后再次发布,检查项目是否已发布到新目录,然后您会发现是否使用了xxx.pubxml)
(Change the value of the PublishDir to another folder, and publish it again, check if the project is published to new directory, then you'll find if the xxx.pubxml is used)
更新:
根据您的.pubxml文件,您的项目是.net核心控制台应用,而不是 asp.net核心Web应用.这很重要!
According to your .pubxml file, your project is a .net core console app instead of a asp.net core web app. This makes the difference!
我在VS2017和VS2019中都对其进行了测试,发现目标在asp.net Web应用程序中运行良好,但在.net核心控制台中却无法运行.我相信对于那些非Web应用程序,可能不支持这种发布后目标.
I test it in both VS2017 and VS2019, and find the target works well in asp.net web app but not .net core console one. I believe for those non-web app, this kind of after publish target may not be supported.
请检查我发现的这两个正式文件:
And please check these two official documents I found:
Asp.net核心:在发布之前或之后运行目标
Asp.net core: Run a target before or after publishing
.net核心:部署VS的.net核心应用程序
.net核心文档中找不到任何与自定义目标相关的信息.因此,恐怕设计可能不支持它.
I can't find any custom-target-related info in the .net core document. So I'm afraid it might be not supported by design.
Update2:
实际上,您最初的需求是添加CopyFiles目标以进行发布过程.因此,我假设您真正想要的是copy sth to Publish Folder after Publish
处理或copy sth from Publish Folder to target folder
.
Actually your original need is to add CopyFiles target to publish process. So I assume what you really want is to copy sth to Publish Folder after Publish
process or copy sth from Publish Folder to target folder
.
.net核心控制台应用程序中不支持发布后目标.我们有相应的解决方法:
Though the after publish target is not supported in .net core console apps. We have corresponding workarounds:
- 要在发布后将某事复制到发布文件夹":
在这种情况下,您可以添加一个构建后目标,以便在发布过程之前将文件复制到Publish文件夹中.我认为这适用于这种情况:
For this situation, you can add a after build target to copy the files to Publish folder before publish process. I think it works for this situation:
<Target Name="CustomTarget" AfterTargets="build">
<ItemGroup>
<MySourceFiles Include="xxx\pathToTheFolderWhereYourFilesAre\*.*"/>
</ItemGroup>
<Copy
SourceFiles="@(MySourceFiles)"
DestinationFolder="$(ProjectDir)bin\Release\netcoreapp2.1\publish\"
/>
</Target>
- 要将某物从发布文件夹"复制到目标文件夹:
对于.net核心控制台应用程序,实际上所谓的发布过程是将文件从obj文件夹复制到发布文件夹.参见以下屏幕截图:
转到Tools=>Options=>Project and Solutions=>Build and Run
将构建输出的详细程度更改为详细",您可以在VS中的构建和发布过程中查看详细信息.
Go Tools=>Options=>Project and Solutions=>Build and Run
to change the build output verbosity to Detailed and you can see details in build and publish process in VS.
这就是为什么我建议您可以转到Project => Properties => build events => post-build事件以使用xcopy命令将文件复制到目标文件夹中的原因.(或者您可以使用上述脚本,请记住更改源路径和目标路径). 当我们要复制publish文件夹中的文件时,实际上我们正在尝试从obj文件夹中复制文件.因此,在构建过程完成之后就可以做一些事情!在发布过程之后不需要这样做.
That's why I suggest you can Go Project=>Properties=>build events=>post-build events to use a xcopy command to copy the files to your destination folder.(Or you can use a script like above, remember to change the source path and destination path). When we want to copy the files in publish folder, actually we're trying to copy the files from obj folder. So do something after build process is enough! Not need to do it after publish process.
这篇关于即使发布成功,发布配置文件中的构建目标也不会执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!