经过几个小时的战斗,我想我想出了非常简单且通用的解决方案。我发现的解决方案都不适合我(“发布”目标仅适用于 ClickOnce),所以我想出了这个:
发布文件(pubxml)只是类似项目的文件。因此添加一些值,例如:
<IsPublish>True</IsPublish>
然后在项目文件中:
<Target Name="MessageBeforePublish" AfterTargets="Build" Condition="'$(IsPublish)' == 'True'">
<Message Text="Before publishing..." Importance="high" />
</Target>
因此,需要明确的是,我的 pubxml 文件如下所示:
<?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>
<Configuration>Release</Configuration>
<Platform>x64</Platform>
<PublishDir>***my dir***</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
<PublishReadyToRun>True</PublishReadyToRun>
<PublishTrimmed>False</PublishTrimmed>
<IsPublish>True</IsPublish>
</PropertyGroup>
</Project>
注意我添加的最后一个属性(IsPublish)。 就是这样。
我这边用的是VS2017。我可以使用 MSbuild.exe 在命令提示符中获取文本。
因此,请确保正确编辑 .xxproj 文件,并在您这边正确运行 msbuild 命令行。我只是使用一个简单的 Winform 应用程序来测试它。
更新:
如果您只想在使用 VS IDE 发布期间调用它,我想您可以在这里找到解决方法: