首页 > 解决方案 > .Net Core MS 构建包创建生命周期

问题描述

MSBuild 相当强大和重要的工具,但有时配置它就像制造宇宙飞船或飞往火星一样困难。下一个问题的答案可以帮助它变得更有用和对开发人员友好: - MSBuild 如何创建包(zip)?- 它是使用一些临时文件夹还是直接来自构建输出?- 为什么输出文件夹内容与包内容不同 - 可以使用哪些事件来添加一些自定义逻辑?

这个问题的答案可以帮助我了解下一个长话短说:我正在使用 .Net Core 解决方案,该解决方案依赖于其他一些解决方案 DLL 文件(未直接引用,但需要在解决方案中文件夹)。在构建后事件 CL 上,添加了作为入口点“\TaskEP”的选项,这是下一个管道的起点:

<PropertyGroup>
<PipelineCopyAllFilesToOneFolderForMsdeployDependsOn>
  TaskEP;
  Task2;
  Task3;
  $(PipelineCopyAllFilesToOneFolderForMsdeployDependsOn);
</PipelineCopyAllFilesToOneFolderForMsdeployDependsOn>
<RunPostBuildEvent>Always</RunPostBuildEvent>

任务主要是为了将额外的 DLL 添加到包中,通常看起来像:

<Target Name="TaskEP" Condition="$(SomeConditions) == 'True'">
    <Message Importance="high" Text="TaskEP: Copying some files..." />
    <ItemGroup>
      <ItemsName Include="$(MSBuildProjectDirectory)\..\..somepath..\Extra.dll;....dll" />
      <FilesForPackagingFromProject Include="%(ItemsName.Identity)">
        <DestinationRelativePath>bin\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
      </FilesForPackagingFromProject>
    </ItemGroup>
  </Target>

基本上,对我来说,这条指令向 MSBuild 添加“Extra.dll”到输出包。
Pubxml 很常见:

 <PropertyGroup>
<WebPublishMethod>Package</WebPublishMethod>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<_PackagePathShortened Condition="'$(_PackagePathShortened)' == ''">SuperWebSite</_PackagePathShortened>
<ExcludeApp_Data>False</ExcludeApp_Data>
<PackageLocation>..\..\..\..\BIN\WEB\Release\Publish\Super.WebApplication.zip</PackageLocation>
<PackageAsSingleFile>true</PackageAsSingleFile>
<TargetFramework>netcoreapp2.1</TargetFramework> ....

还有一条指令将包中的 long long 路径替换为 Short 并定义了一条:

<Target Name="AddReplaceRuleForAppPath" BeforeTargets="BeforePublish">
<Message Text="Adding replace rules for application path '$(PublishIntermediateOutputPath)' replace with '$(_PackagePathShortened)'" Importance="high" />
<EscapeTextForRegularExpressions Text="$(PublishIntermediateOutputPath)">
  <Output PropertyName="_PackagePathRegex" TaskParameter="Result" />
</EscapeTextForRegularExpressions>
<!-- Add a replace rule for VSMSDeploy resp. MSdeploy to update the path -->
<ItemGroup>
  <MsDeployReplaceRules Include="replaceFullPath">
    <Match>$(_PackagePathRegex)</Match>
    <Replace>$(_PackagePathShortened)</Replace>
  </MsDeployReplaceRules>
</ItemGroup>

我还在 .pubxml 文件中复制了相同的“复制”任务,使用不同的事件“AfterBuild”、“BeforePublish”,但似乎所有这些都被忽略了。我可以在构建输出目录中看到“额外”DLL 文件以及发布时的所有信息消息,但在最终的“Super.WebApplication.zip”文件中看不到!

标签: msbuild.net-coremsdeploypubxml

解决方案


我发现在 .Net Core 中复制额外文件的解决方案取自本文档

<ItemGroup>
    <DotnetPublishFiles Include="$(MSBuildProjectDirectory)\..\..somepath..\Extra.dll;....dll" >
      <DestinationRelativePath>bin\x86\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
    </DotnetPublishFiles>
  </ItemGroup>

推荐阅读