MSBuild 和创建 ZIP 文件

发布于 2024-12-20 03:16:19 字数 2587 浏览 2 评论 0原文

这是我的构建脚本:

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
  <PropertyGroup>
    <!-- Path where the solution file is located (.sln) -->
    <ProjectPath>W:\Demo</ProjectPath>
    <!-- Location of compiled files -->
    <DebugPath>W:\Demo\bin\Debug</DebugPath>
    <ReleasePath>W:\Demo\bin\Release</ReleasePath>
    <!-- Name of the solution to be compiled without the .sln extension -->         <ProjectSolutionName>DemoTool</ProjectSolutionName>

    <!-- Path where the nightly zip file will be copyd -->
    <NightlyBuildPath>W:\Nightly_Builds\Demo</NightlyBuildPath>
    <!-- Name of the nighly zip file (YYYYMMDD_NightlyZipName.zip, date added automatically) -->
    <NightlyZipName>Demo</NightlyZipName>
  </PropertyGroup>

  <ItemGroup>
    <!-- All files and folders from ./bin/Debug or ./bin/Release what will be added to the nightly zip -->
    <DebugApplicationFiles Include="$(DebugPath)\**\*.*" Exclude="$(DebugPath)\*vshost.exe*" />
    <ReleaseApplicationFiles Include="$(ReleasePath)\**\*.*" Exclude="$(ReleasePath)\*vshost.exe*" />
  </ItemGroup>

  <Target Name="DebugBuild">
    <Message Text="Building $(ProjectSolutionName) Debug Build" />
    <MSBuild Projects="$(ProjectPath)\$(ProjectSolutionName).sln" Targets="Clean" Properties="Configuration=Debug"/>
    <MSBuild Projects="$(ProjectPath)\$(ProjectSolutionName).sln" Targets="Build" Properties="Configuration=Debug"/>
    <Message Text="$(ProjectSolutionName) Debug Build Complete!" />
    <CallTarget Targets="CreateNightlyZip" />
  </Target>

  <Target Name="CreateNightlyZip">
    <PropertyGroup>
      <StringDate>$([System.DateTime]::Now.ToString('yyyyMMdd'))</StringDate>
    </PropertyGroup>
    <MakeDir Directories="$(NightlyBuildPath)"/>
    <Zip Files="@(DebugApplicationFiles)"
          WorkingDirectory="$(DebugPath)"
          ZipFileName="$(NightlyBuildPath)\$(StringDate)_$(NightlyZipName).zip"
          ZipLevel="9" />
  </Target>
</Project>

我的脚本运行良好,只有一个奇怪的问题。当我第一次构建项目时,没有 \bin\Debug 文件夹及其在构建过程中创建的文件夹,但 ZIP 文件仍然为空。当 \bin\Debug 文件夹现在包含构建文件时,第二次运行构建脚本,然后该文件将添加到 ZIP 中。

第一次运行脚本时 ZIP 文件为空可能是什么问题?

Here is my build script:

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
  <PropertyGroup>
    <!-- Path where the solution file is located (.sln) -->
    <ProjectPath>W:\Demo</ProjectPath>
    <!-- Location of compiled files -->
    <DebugPath>W:\Demo\bin\Debug</DebugPath>
    <ReleasePath>W:\Demo\bin\Release</ReleasePath>
    <!-- Name of the solution to be compiled without the .sln extension -->         <ProjectSolutionName>DemoTool</ProjectSolutionName>

    <!-- Path where the nightly zip file will be copyd -->
    <NightlyBuildPath>W:\Nightly_Builds\Demo</NightlyBuildPath>
    <!-- Name of the nighly zip file (YYYYMMDD_NightlyZipName.zip, date added automatically) -->
    <NightlyZipName>Demo</NightlyZipName>
  </PropertyGroup>

  <ItemGroup>
    <!-- All files and folders from ./bin/Debug or ./bin/Release what will be added to the nightly zip -->
    <DebugApplicationFiles Include="$(DebugPath)\**\*.*" Exclude="$(DebugPath)\*vshost.exe*" />
    <ReleaseApplicationFiles Include="$(ReleasePath)\**\*.*" Exclude="$(ReleasePath)\*vshost.exe*" />
  </ItemGroup>

  <Target Name="DebugBuild">
    <Message Text="Building $(ProjectSolutionName) Debug Build" />
    <MSBuild Projects="$(ProjectPath)\$(ProjectSolutionName).sln" Targets="Clean" Properties="Configuration=Debug"/>
    <MSBuild Projects="$(ProjectPath)\$(ProjectSolutionName).sln" Targets="Build" Properties="Configuration=Debug"/>
    <Message Text="$(ProjectSolutionName) Debug Build Complete!" />
    <CallTarget Targets="CreateNightlyZip" />
  </Target>

  <Target Name="CreateNightlyZip">
    <PropertyGroup>
      <StringDate>$([System.DateTime]::Now.ToString('yyyyMMdd'))</StringDate>
    </PropertyGroup>
    <MakeDir Directories="$(NightlyBuildPath)"/>
    <Zip Files="@(DebugApplicationFiles)"
          WorkingDirectory="$(DebugPath)"
          ZipFileName="$(NightlyBuildPath)\$(StringDate)_$(NightlyZipName).zip"
          ZipLevel="9" />
  </Target>
</Project>

My script works perfectly, only there is one strange problem. When i build a project first time and there is no \bin\Debug folder and its created during the build, but the ZIP file still comes empty. Running the build script second time when the \bin\Debug folder is now in place with builded files then the file are added to the ZIP.

What could be the problem that running script first time the ZIP file is empty?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水晶透心 2024-12-27 03:16:19

问题出在 DebugApplicationFiles 项集合中。它是在调用构建之前创建的。将 DebugApplicationFiles 移动到 CreateNightlyZip 目标中。这样更新你的脚本:

<Target Name="CreateNightlyZip">
    <PropertyGroup>
      <StringDate>$([System.DateTime]::Now.ToString('yyyyMMdd'))</StringDate>
    </PropertyGroup>
    <ItemGroup>
        <DebugApplicationFiles Include="$(DebugPath)\**\*.*" Exclude="$(DebugPath)\*vshost.exe*" />
    </ItemGroup>
    <MakeDir Directories="$(NightlyBuildPath)"/>
    <Zip Files="@(DebugApplicationFiles)"
      WorkingDirectory="$(DebugPath)"
      ZipFileName="$(NightlyBuildPath)\$(StringDate)_$(NightlyZipName).zip"
      ZipLevel="9" />
</Target>

The problem is in the DebugApplicationFiles item collection. It is created before the build is invoked. Move the DebugApplicationFiles into CreateNightlyZip target. Update your script this way:

<Target Name="CreateNightlyZip">
    <PropertyGroup>
      <StringDate>$([System.DateTime]::Now.ToString('yyyyMMdd'))</StringDate>
    </PropertyGroup>
    <ItemGroup>
        <DebugApplicationFiles Include="$(DebugPath)\**\*.*" Exclude="$(DebugPath)\*vshost.exe*" />
    </ItemGroup>
    <MakeDir Directories="$(NightlyBuildPath)"/>
    <Zip Files="@(DebugApplicationFiles)"
      WorkingDirectory="$(DebugPath)"
      ZipFileName="$(NightlyBuildPath)\$(StringDate)_$(NightlyZipName).zip"
      ZipLevel="9" />
</Target>
抱着落日 2024-12-27 03:16:19

如果powershell 5.0或更高版本可用,您可以直接使用powershell命令。

<Target Name="Zip" BeforeTargets="AfterBuild">
  <ItemGroup>
    <ZipFiles Include="$(OutDir)release\file1.exe" />
    <ZipFiles Include="$(OutDir)release\file2.exe" />
  </ItemGroup>
  <Exec Command="PowerShell -command Compress-Archive @(ZipFiles, ',') $(OutDir)release\zippedfiles.zip" />
</Target>

If powershell 5.0 or greater is available, you could use powershell command directly.

<Target Name="Zip" BeforeTargets="AfterBuild">
  <ItemGroup>
    <ZipFiles Include="$(OutDir)release\file1.exe" />
    <ZipFiles Include="$(OutDir)release\file2.exe" />
  </ItemGroup>
  <Exec Command="PowerShell -command Compress-Archive @(ZipFiles, ',') $(OutDir)release\zippedfiles.zip" />
</Target>
枕花眠 2024-12-27 03:16:19

如果您希望压缩整个文件夹以进行“xcopy 部署”,自 MSBuild 15.8 起有一种简单的方法 - ZipDirectory 构建任务。

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <Target Name="ZipOutputPath" AfterTargets="Build">
        <ZipDirectory
            SourceDirectory="$(OutputPath)"
            DestinationFile="$(OutputPath)\..\$(AssemblyName).zip"
            Overwrite=="true" />
    </Target>

</Project>

[1] https://learn.microsoft .com/en-us/visualstudio/msbuild/zipdirectory-task?view=vs-2019

Should you wish to zip a whole folder for 'xcopy deploy', since MSBuild 15.8 there is a simple way - the ZipDirectory build task.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <Target Name="ZipOutputPath" AfterTargets="Build">
        <ZipDirectory
            SourceDirectory="$(OutputPath)"
            DestinationFile="$(OutputPath)\..\$(AssemblyName).zip"
            Overwrite=="true" />
    </Target>

</Project>

[1] https://learn.microsoft.com/en-us/visualstudio/msbuild/zipdirectory-task?view=vs-2019

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文