我最终得出的答案对于 Visual Studio 2010 和 Visual Studio 2012 非常有效;把它放在你的网络应用程序的末尾之前.csproj
file:
<Project...
[...]
<!-- The following makes sure we can't accidentally publish a non-Release configuration from within Visual Studio -->
<Target Name="PreventNonReleasePublish2010" BeforeTargets="PipelinePreDeployCopyAllFilesToOneFolder" Condition="'$(BuildingInsideVisualStudio)'=='true' AND '$(VisualStudioVersion)'=='10.0'">
<Error Condition="'$(Configuration)'!='Release'" Text="When publishing from Visual Studio 2010, you must publish the Release configuration!" />
</Target>
<Target Name="PreventNonReleasePublish2012" BeforeTargets="MSDeployPublish" Condition="'$(BuildingInsideVisualStudio)'=='true' AND '$(VisualStudioVersion)'=='11.0'">
<Error Condition="'$(Configuration)'!='Release'" Text="When publishing from Visual Studio 2012, you must publish the Release configuration!" />
</Target>
</Project>
继续阅读以了解我在这个答案背后的想法,但基本上它围绕着这样一个事实:Visual Studio 2010 定义了PipelinePreDeployCopyAllFilesToOneFolder
要挂钩的目标,Visual Studio 2012 定义了更“标准”的MSDeployPublish
要挂接的目标。
上面的代码仅允许在处于Release
从 Visual Studio 中进行配置,但可以轻松修改它以防止all从 Visual Studio 中部署发布。
AFAIR,Visual Studio 2010 上下文菜单中的“发布”会调用 webdeploy\msdeploy 工具。我玩了一下,但我一点也不喜欢。如果您仍然想使用此功能并将目标插入某处 - 您需要知道确切的目标及其依赖属性。
Check
c:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets
您将发现两个任务 - MSDeploy 和 VSMSDeploy。后一种听起来适合我。该文件中根本没有使用第一个。但是 VSMSDeploy 用于三个不同的目标,
PackageUsingManifest、TestDeployPackageToLocal 和 MSDeployPublish。后一个听起来不错;)
<Target Name="MSDeployPublish" DependsOnTargets="$(MSDeployPublishDependsOn)">
所以你只需要重写一个属性。将其放在您的目标之前,“YourTargetName”将在 MSDeployPublish 之前被调用。
<PropertyGroup>
<MSDeployPublishDependsOn Condition="'$(MSDeployPublishDependsOn)'!=''">
$(MSDeployPublishDependsOn);
YourTargetName;
</MSDeployPublishDependsOn>
</PropertyGroup>
如果您已经切换到 MSBuild 4.0,则有一种更简单的方法来挂钩您的目标。您只需要指定BeforeTarget
属性。在我们的例子中,它将是这样的:
<Target Name="MyTarget" BeforeTargets="MSDeployPublish">
<Error Condition="'foo'=='foo'" Text="test publish error" />
</Target>
我希望这有帮助。询问您是否还有其他问题。
PS:我没有检查所有这些,因为我没有任何 MSDeploy 就绪环境;)
注意:我记得我不鼓励将 MSDeploy 用于我们自己的产品,因为为一个产品正确配置它是非常违反直觉的。持续集成 http://en.wikipedia.org/wiki/Continuous_integration(CI)系统。也许我不太擅长,你的解决方案会正常工作。但要小心地继续使用 MSDeploy。