为什么MSBuild忽略我的BeforePublish目标? [英] Why does MSBuild ignore my BeforePublish target?

查看:103
本文介绍了为什么MSBuild忽略我的BeforePublish目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我肯定在这里缺少明显的东西,但是我在ASP.NET MVC Web项目的.csproj文件的末尾有这个东西:

I must be missing something obvious here, but I have this at the end of my ASP.NET MVC web project's .csproj file:

    [...]
    <Target Name="BeforePublish">
        <Error Condition="'foo'=='foo'" Text="test publish error" />
    </Target>
</Project>

据我所知,这应该总是导致发布失败并出现错误.但是,如果我加载了该项目,请右键单击它,然后单击发布",该项目将顺利发布.我想念什么?

As far as I can tell, that should always cause the publish to fail with an error. Yet, if I load the project, right-click on it, and click "Publish", the thing publishes without a hitch. What am I missing?

推荐答案

我最终得出的答案对于Visual Studio 2010和Visual Studio 2012效果很好.将其放在Web应用程序的.csproj文件末尾之前:

The answer I finally came up with which works well for Visual Studio 2010 and Visual Studio 2012; put this just before the end of your web application's .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目标.钩上.

Read on to see my thinking behind this answer, but basically it revolves around the fact that Visual Studio 2010 defines the PipelinePreDeployCopyAllFilesToOneFolder Target to hook on to, and Visual Studio 2012 defines the more "standard" MSDeployPublish Target to hook on to.

上面的代码仅允许在Visual Studio中以Release配置进行部署发布,但是可以很容易地对其进行修改以防止在Visual Studio中进行 all 部署发布.

The above code only allows deploy publishing when in a Release configuration from within Visual Studio, but it could easily be modified to prevent all deploy publishing from within Visual Studio.

AFAIR,Visual Studio 2010上下文菜单中的发布"将调用webdeploy \ msdeploy工具.我玩了一点,但是我一点都不喜欢.如果您仍想使用此功能并将目标插入到某处-您需要知道确切的目标及其依赖项属性.

AFAIR, "Publish" from Visual Studio 2010 context menu invokes webdeploy\msdeploy tool. I played with it a bit, but I didn't liked at all. If you still want to use this functionality and insert your target somewhere - you need to know the exact target and its dependency property.

检查 c:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets

您将找到两个任务-MSDeploy和VSMSDeploy.后者听起来很适合我.此文件中根本没有使用第一个.但是VSMSDeploy用于三个不同的目标, PackageUsingManifest,TestDeployPackageToLocal和MSDeployPublish.再次听起来不错;)

You will find two tasks - MSDeploy and VSMSDeploy. The latter one sounds right for me. The first one is not used in this file at all. But VSMSDeploy used in three different targets, PackageUsingManifest, TestDeployPackageToLocal and MSDeployPublish. Again the latter one sounds good ;)

<Target Name="MSDeployPublish" DependsOnTargets="$(MSDeployPublishDependsOn)">

因此,您只需要覆盖一个属性.将其放在目标之前,"YourTargetName"将在MSDeployPublish之前被调用.

So you just need to override one property. Put this before your target and "YourTargetName" will be called right before MSDeployPublish.

<PropertyGroup>
    <MSDeployPublishDependsOn Condition="'$(MSDeployPublishDependsOn)'!=''">
        $(MSDeployPublishDependsOn);
        YourTargetName;
    </MSDeployPublishDependsOn>
  </PropertyGroup>

如果您已经切换到MSBuild 4.0,则有一种更简单的方法来挂接目标.您只需要指定BeforeTarget属性.在我们的情况下,将是这样的:

If you already switched to MSBuild 4.0, there is an easier way to hook your target. You just need to specify the BeforeTarget attribute. In our case it will be like this:

  <Target Name="MyTarget" BeforeTargets="MSDeployPublish">
        <Error Condition="'foo'=='foo'" Text="test publish error" />
  </Target>

我希望这会有所帮助.询问是否还有其他问题.

I hope this helps. Ask if you have more questions.

PS:我没有检查所有内容,因为我没有任何支持MSDeploy的环境;)

PS: I didn't checked all that, because I don't have any MSDeploy-ready environments ;)

NB:我记得我不建议将MSDeploy用于我们自己的产品,因为对于连续集成(CI)系统.也许我不是很擅长,您的解决方案将正常运行.但是请仔细进行MSDeploy.

NB: I remember that I was discouraged from using MSDeploy for our own products because it was pretty counter-intuitive to properly configuring it for a continuous integration (CI) system. Maybe I wasn't very good at that, and your solution will work properly. But proceed with MSDeploy carefully.

这篇关于为什么MSBuild忽略我的BeforePublish目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆