Visual Studio 2013 Build and Publish

牧云@^-^@ 提交于 2019-11-29 11:56:29

Yes, this is absolutely possible. Simply add the following XML to your ServerManager.csproj file.

  <PropertyGroup>
    <DeployOnBuild>true</DeployOnBuild>
    <PublishProfile>Local</PublishProfile>
  </PropertyGroup>

UPDATED: 2017-02-13

After actually testing this solution I found Yusuf was correct and the deployment only worked when I explicitly set the DeployOnBuild and PublishProfile MSBuild properties on the MSBuild.exe call.

>"c:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild" /p:DeployOnBuild=true /p:PublishProfile=Local

Setting properties in the .csproj generally works without issue. It appears the DeployOnBuild property is special. Not sure if this is a bug or intentional but setting it in the project file or a referenced targets file is overwritten somewhere in the build. Only when you define it as a global MSBuild property by setting it as a flag on the MSBuild.exe call is it respected.

Once I discovered this I found an alternate solution that works. Pass the properties to a second MSBuild call in your project:

<Target Name="AfterBuild">
  <MSBuild Condition="'$(DeployOnBuild)'!='true'" Projects="$(MSBuildProjectFullPath)" Properties="DeployOnBuild=true;PublishProfile=Local;BuildingInsideVisualStudio=False"/>
</Target>

Now it will automatically deploy whether you build in VS or from the commandline.

Building on @chief7's answer, I was able to get this working in an MVC project in Visual Studio 2015 like so:

<Target Name="Deploy" AfterTargets="Build">
    <MSBuild
        Condition="'$(DeployOnBuild)'!='true'"
        Projects="$(ProjectPath)"
        Targets="WebPublish"
        Properties="PublishProfile=$(AutoDeployPublishProfileName)"/>
</Target>

The original answer was throwing MSB4006 for me:

There is a circular dependency in the target dependency graph involving target "AfterBuild"

This answer suggested changing the target name and adding the AfterTargets property as shown above, which worked.

As an aside for those who need to make deployment contingent on configuration (Debug, Release, etc.)—@chief7 has a great write-up on their website.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!