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

查看:18
本文介绍了为什么 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 配置中部署发布,但可以轻松修改它以阻止 all 从 Visual Studio 内部署发布.

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 上下文菜单中的发布"调用 webdeploymsdeploy 工具.我玩了一点,但我一点也不喜欢.如果您仍想使用此功能并将目标插入某处 - 您需要知道确切的目标及其依赖属性.

AFAIR, "Publish" from Visual Studio 2010 context menu invokes webdeploymsdeploy 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)MSBuildMicrosoftVisualStudiov10.0WebMicrosoft.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 ;)

注意:我记得我不鼓励将 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天全站免登陆