How to create MSBuild targets file to deploy files

2019-07-18 01:24发布

问题:

Given I have a post build process that generates files in \bin\out how can I use a MSBuild targets file to state that all files (recursively) in that directory should be published during web deploy?

回答1:

Let's split this in two parts:

First, build the web project:

<Target Name="BuildWeb">
    <MSBuild Projects="WebExtranet\WebExtranet.csproj"
             Properties="Configuration=$(Environment);DeployOnBuild=True;CreatePackageOnPublish=True;BaseIntermediateOutputPath=..\$(DistDir)\Web\" />
</Target>

Then use this to publish to the local IIS (you might need to adjust the msdeploy command to suit your needs)

<Target Name="DeployWeb">
    <ItemGroup>
        <PackageSourceDir Include='$(DistDir)\Web\$(Environment)\Package\WebExtranet.zip' />
    </ItemGroup>

    <Message Text="Package path: %(PackageSourceDir.FullPath)" />

    <Exec Command="&quot;c:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe&quot; -verb:sync -allowUntrusted -source:package=&quot;%(PackageSourceDir.FullPath)&quot; -setParam:'IIS Web Application Name'='My Web App' -dest:auto"/>
</Target>

Common pitfalls:

  • The msbuild version is important. I'm using V3.
  • You have to have the web application already created on IIS. Only need to do it once.

Note that the $(Environment) variable is the build configuration. I have several, but run the first target and check the folder structure created. Then adjust the script.