构建没有依赖项的 MSBuild 目标

发布于 2024-10-19 15:19:02 字数 60 浏览 1 评论 0原文

有什么方法可以告诉 MSBuild 4.0 构建目标,但忽略任何依赖项?我只想建立这个目标本身,没有别的。

Is there any way I can tell MSBuild 4.0 to build a target, but ignore any dependencies? I just want to build that target itself, nothing else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

献世佛 2024-10-26 15:19:02

我想重申 @EMP 的解决方案(由于我的名声微薄,我不能投票给他)。

避免 MSBuild 重建项目文件中列出的所有依赖项的默认行为的正确方法是将 BuildProjectReferences 属性设置为 false。

在他的回答中,他从 MSBuild 脚本中调用 MSBuild;这是命令行的示例:

MSBuild myproj.csproj /p:Configuration=Debug /p:BuildProjectReferences=false /t:Build

I would like to reiterate @EMP's solution (and I can't vote him up due to my puny reputation).

The correct way to avoid MSBuild's default behavior of rebuilding all the dependencies listed in the project file is to set the BuildProjectReferences property to false.

In his answer he invokes MSBuild from within an MSBuild script; here's an example from the command line:

MSBuild myproj.csproj /p:Configuration=Debug /p:BuildProjectReferences=false /t:Build
瞄了个咪的 2024-10-26 15:19:02

事实证明,内置的 Build 目标检查名为 BuildProjectReferences 的属性来判断是否构建引用。我确实需要在项目本身上运行 Build(否则它不起作用),而不是在其依赖项上运行。我最终打电话:

<MSBuild Projects="MyCloudProject.ccproj" Targets="CorePublish" Properties="Configuration=$(Configuration); BuildProjectReferences=false" />

It turns out the built-in Build target checks a property named BuildProjectReferences to tell whether to build references or not. I do need to run Build on the project itself (otherwise it doesn't work), just not on its dependencies. I ended up calling:

<MSBuild Projects="MyCloudProject.ccproj" Targets="CorePublish" Properties="Configuration=$(Configuration); BuildProjectReferences=false" />
悍妇囚夫 2024-10-26 15:19:02

可以覆盖内置目标,添加您自己的条件,并让您的目标复制原始目标,但这可能会相当复杂。如果您可以追踪单个依赖的内置目标,那么这就是可维护的。有时,这些“核心”目标仅由 DependsOnTargets 列表组成,有时甚至在属性中定义这些目标,因此覆盖它并在缺少它们的情况下添加条件是微不足道的。有时你需要进行大量的剪切和粘贴才能得到正确的结果。

基本上,MSBuild 将仅维护任何给定名称定义的最后一个目标,因此找到您的项目导入包含要覆盖的目标的 .target 文件的位置,然后将您自己的包含覆盖的 .target 文件导入放在其后面。

例如,从 Microsoft.Common.targets 添加条件到“CoreBuild”目标就这么简单,同时保持相同的行为(下面带有 $(SkipCoreBuild) 属性的目标条件)

<Target
    Name="CoreBuild"
    Condition="'$(SkipCoreBuild)' != 'true'"
    DependsOnTargets="$(CoreBuildDependsOn)">

    <OnError ExecuteTargets="_TimeStampAfterCompile;PostBuildEvent"
        Condition="'$(RunPostBuildEvent)'=='Always' or
            '$(RunPostBuildEvent)'=='OnOutputUpdated'"
        />
    <OnError ExecuteTargets="_CleanRecordFileWrites" />
</Target>

:目标文件并在标准之后将其导入到 C# 项目中...

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

...将覆盖默认实现,该实现不具备选择性禁用它所需的条件。

无论如何,如果您发现一个目标不允许您修改条件,我会在 Connect 上提交一个错误,描述您正在尝试执行的操作,MSBuild 人员可以对此类事情非常敏感(随着时间的推移) )。

It may be possible to override the built-in target, add your own condition, and have your target duplicate the original, but this can get rather involved. If you can track down a single dependent built-in target this can be maintainable. Sometimes these "core" targets consist of nothing more than a list of DependsOnTargets, and sometimes even those are defined in a property, so overrideing it and adding a condition where they are missing is trivial. Sometimes though you need to do a big cut-and-paste to get it right.

Basically, MSBuild will only maintain the last target defined of any given name, so find where your project is importing the .target file that includes the target you want to override, then put your own .target file import that contains the override after it.

For example, adding a condition to the "CoreBuild" target from Microsoft.Common.targets is as easy as this, while keeping the same behavior otherwise (the Target condition with the $(SkipCoreBuild) property below):

<Target
    Name="CoreBuild"
    Condition="'$(SkipCoreBuild)' != 'true'"
    DependsOnTargets="$(CoreBuildDependsOn)">

    <OnError ExecuteTargets="_TimeStampAfterCompile;PostBuildEvent"
        Condition="'$(RunPostBuildEvent)'=='Always' or
            '$(RunPostBuildEvent)'=='OnOutputUpdated'"
        />
    <OnError ExecuteTargets="_CleanRecordFileWrites" />
</Target>

Placing the above in your own targets file and importing it in a C# project after the standard...

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

... will override the default implementation that doesn't have the condition you need to selectively disable it.

In any case, if you find a target that doesn't allow you to modify the condition, I would file a bug on Connect describing what you are trying to do, the MSBuild folks can be pretty responsive to this sort of thing (over time).

执妄 2024-10-26 15:19:02

这取决于您想要构建的目标。
如果目标是内置的并且具有依赖项(例如“构建”),则您无法删除依赖项。
如果它是您自己的目标,您可以轻松实现它:

<Target Name="YourTargetWithDependencies" 
        DependsOnTargets="YourTargetDependencies;YourTarget">
</Target>

<Target Name="YourTarget" >
</Target>

对于Azure云计算项目中的CorePublish以及任何其他具有可自定义的DependsOn目标,您可以尝试更改项目来修改默认依赖列表:

<Target Name=CallPublish>
      <!-- Modified list of dependencies. Build is skipped here. -->
      <PropertyGroup>
        <CustomCorePublishDependsOn>
          PrepareForPackaging;
          CheckRoleInstanceCount;
          CopyServiceDefinitionAndConfiguration;
          ConfigureWebDeploy;
          IntelliTrace;
        </CustomCorePublishDependsOn>
      </PropertyGroup>

      <MSBuild Projects="$(MSBuildProjectFullPath)"
               Targets="CorePublish"
               Properties="CorePublishDependsOn=$(CustomCorePublishDependsOn);Configuration=$(Configuration);Platform=$(Platform)">
</Target>

It depends on the target you want to build.
If the target is builtin and has dependencies (like "Build") you cann't remove dependencies.
If it's your own target you can easily implement it:

<Target Name="YourTargetWithDependencies" 
        DependsOnTargets="YourTargetDependencies;YourTarget">
</Target>

<Target Name="YourTarget" >
</Target>

For CorePublish in Azure cloud computing project as well as any other with customizable DependsOn targets you can try to change the project to modify default list of dependencies:

<Target Name=CallPublish>
      <!-- Modified list of dependencies. Build is skipped here. -->
      <PropertyGroup>
        <CustomCorePublishDependsOn>
          PrepareForPackaging;
          CheckRoleInstanceCount;
          CopyServiceDefinitionAndConfiguration;
          ConfigureWebDeploy;
          IntelliTrace;
        </CustomCorePublishDependsOn>
      </PropertyGroup>

      <MSBuild Projects="$(MSBuildProjectFullPath)"
               Targets="CorePublish"
               Properties="CorePublishDependsOn=$(CustomCorePublishDependsOn);Configuration=$(Configuration);Platform=$(Platform)">
</Target>
双马尾 2024-10-26 15:19:02

通常目标的依赖项是用属性指定的,例如:

<Target Name="Foo" DependsOnTargets="$(FooDependsOn)">..</Target>

在这种情况下,您可以调用 msbuild as

msbuild bar.proj /p:FooDependsOn=

将该属性设置为空,因此没有依赖项。

Usually a target's dependencies are specified with a property, eg:

<Target Name="Foo" DependsOnTargets="$(FooDependsOn)">..</Target>

In such a case you can invoke msbuild as

msbuild bar.proj /p:FooDependsOn=

which sets that property to nothing, and so no dependencies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文