在部署时将程序集信息添加到Zip文件名

时间:2018-07-18 21:31:01

标签: c# visual-studio csproj pubxml

Visual Studio中的Web部署发布功能允许您配置程序包的位置。使用此功能,您还可以指定文件名。如何将AssemblyInfo添加到此文件名?

作为构建的一部分,我已经能够将汇编版本添加到zip文件中。这是通过使用MSBuild.Extension.Pack的自定义项目文件完成的。以下是项目文件中的xml。是否可以在发布配置文件(.pubxml)中使用相同的方法?因此,仅在部署项目时才创建自​​定义zip。

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Run">
      <PropertyGroup>
        <Configuration>Release</Configuration>
        <ProjectName>MyProject</ProjectName>
        <TemporaryFolder>temp</TemporaryFolder>
      </PropertyGroup>

      <Target Name="Run">
        <CallTarget Targets="Build" />
        <CallTarget Targets="Pack" />
      </Target>

      <Target Name="Build">
        <Message Text="Build $(Configuration)" />
        <MSBuild Projects="$(ProjectName)/$(ProjectName).csproj" Properties="Configuration=$(Configuration)" ContinueOnError="False"/>
      </Target>

      <UsingTask AssemblyFile="packages/MSBuild.Extension.Pack.1.9.1/tools/net40/MSBuild.ExtensionPack.dll" TaskName="Assembly"/>
      <UsingTask AssemblyFile="packages/MSBuild.Extension.Pack.1.9.1/tools/net40/MSBuild.ExtensionPack.dll" TaskName="Zip"/>
      <Target Name="Pack">
        <PropertyGroup>
          <BinaryFolder>$(ProjectName)/bin</BinaryFolder>
          <MainExecutable>$(BinaryFolder)/$(ProjectName).dll</MainExecutable>
        </PropertyGroup>
        <Assembly TaskAction="GetInfo" NetAssembly="$(MainExecutable)">
          <Output TaskParameter="OutputItems" ItemName="Info"/>
        </Assembly>
        <Message Text="Identity: %(Info.Identity)" />
        <Message Text="FullName: %(Info.FullName)" />
        <Message Text="FileVersion: %(Info.FileVersion)" />
        <Message Text="AssemblyVersion: %(Info.AssemblyVersion)" />

        <PropertyGroup>
          <PackageFolder>$(TemporaryFolder)\$(ProjectName)_$(Configuration)_%(Info.AssemblyVersion)</PackageFolder>
          <ZipFileName>$(ProjectName)_$(Configuration)_%(Info.AssemblyVersion).zip</ZipFileName>
        </PropertyGroup>

        <MSBuild Targets="Package" Projects="$(ProjectName)/$(ProjectName).csproj" ContinueOnError="False"
          Properties="PublishProfile=$(Configuration);DesktopBuildPackageLocation=..\$(PackageFolder)\$(ZipFileName)" />

        <CreateItem Include="$(PackageFolder)\*.*" >
          <Output ItemName="ZipFiles" TaskParameter="Include"/>
        </CreateItem>

        <ConvertToAbsolutePath Paths="$(TemporaryFolder)">
          <Output TaskParameter="AbsolutePaths" PropertyName="TemporaryFolderAbsolute"/>
        </ConvertToAbsolutePath>  

        <Zip TaskAction="Create" CompressFiles="@(ZipFiles)" RemoveRoot="$(TemporaryFolderAbsolute)" ZipFileName="C:\$(ZipFileName)" />

        <RemoveDir Directories="$(TemporaryFolder)" ContinueOnError="True"/>
      </Target>

    </Project>`

0 个答案:

没有答案