2018-04-11 12:30

用 Visual Studio 自動壓縮發佈的程式

針對只能用隨身碟安裝程式,又不想用 Web Deploy 這麼複雜工具,簡單的用壓縮檔來是處理,利用 Visual Studio 的檔案發佈再進行壓縮封裝,在原本的 pubxml 檔的後面加上 ZipPublishOutput Target 來接續發佈處理,這樣在 Visual Studio 在執行發佈時就可以產生壓縮封裝。

<?xml version="1.0" encoding="utf-8"?>
<!--
"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" MySolution.sln /t:Rebuild /p:VisualStudioVersion=14.0;Configuration=Release;DeployOnBuild=true;PublishProfile=Release.pubxml
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>True</ExcludeApp_Data>
    <publishUrl>obj\Publish</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
    <AutoParameterizationWebConfigConnectionStrings>False</AutoParameterizationWebConfigConnectionStrings>
  </PropertyGroup>

  <Target Name="ZipPublishOutput" AfterTargets="GatherAllFilesToPublish;WebFileSystemPublish">
    <Message Text="== 封裝 7-Zip 安裝包 ===============================" Importance="high" />
    <PropertyGroup>
      <Today>$([System.DateTime]::Today.ToString("yyyyMMdd"))</Today>
      <zip>"C:\Program Files\7-Zip\7z.exe"</zip>
      <ReleasePath>$(ProjectDir)obj\Release\Package\PackageTmp</ReleasePath>
      <PackagePath>D:\MySolution_$(Today).exe</PackagePath>
    </PropertyGroup>

    <Exec Command='DEL $(PackagePath)' />
    <Exec Command='$(zip) a -t7z -mx9 -sfx7z.sfx -bd $(PackagePath) * | findstr /v "^$"' WorkingDirectory="$(ReleasePath)" />
  </Target>
</Project>

0 回應: