为什么 MSBuild 不执行多个目标?

发布于 2024-07-26 11:39:48 字数 528 浏览 5 评论 0原文

我在一个 xml 文件中设置了多个目标。 我希望所有目标都能运行,但只有第一个目标被执行。

以下是我正在尝试执行的操作的简化版本:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="T1">
    <Copy SourceFiles="c:\temp\a.txt" DestinationFolder="C:\temp2\" />    
  </Target>
  <Target Name="T2">
    <Copy SourceFiles="c:\temp\b.txt" DestinationFolder="C:\temp2\" />    
  </Target>
</Project>

我正在从 TeamCity CI 服务器运行构建,日志报告进程退出代码:0。

任何人都知道为什么它不运行 T2?

I have set up multiple targets in a single xml file. I expect all targets to run but only the frist target gets executed.

Here is a simplified version of what iam trying to do:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="T1">
    <Copy SourceFiles="c:\temp\a.txt" DestinationFolder="C:\temp2\" />    
  </Target>
  <Target Name="T2">
    <Copy SourceFiles="c:\temp\b.txt" DestinationFolder="C:\temp2\" />    
  </Target>
</Project>

I'am running the build from the TeamCity CI Server and the logs reports Process exit code: 0.

Anyone got any ideas why it does not run T2?

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

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

发布评论

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

评论(4

淡写薰衣草的香 2024-08-02 11:39:49

您需要告诉 MSBuild 您的多个目标

尝试

<Target Name="Build" DependsOnTargets="T1; T2">
</Target>

You need to tell MSBuild about your multiple targets

Try

<Target Name="Build" DependsOnTargets="T1; T2">
</Target>
橘寄 2024-08-02 11:39:49

MSBuild 允许您定义默认目标,并允许您定义目标之间的依赖关系。 如果未定义默认值,则它将运行找到的第一个默认值。 使用 DefaultTargets,您可以调用多个:

<Project DefaultTargets="T1;T2">

一个 Target 应该完成一个明确定义的步骤的所有内容。 例如,清理目标将删除清理项目所需的所有必要文件和文件夹。 编译将编译所有 DLL 等。

您的目标通常应该声明它们自己的依赖项:

<Target Name="CI" DependsOnTargets="T1, T2">
</Target>

否则您的目标应该包含您想要运行的所有单独步骤:

<Target Name="XX">
    <CallTarget Targets="T1"/>
    <CallTarget Targets="T2"/>
</Target>

MSBuild allows you to define a default Target and it allows you to define dependencies among your Targets. If no default is defined, then it runs the first one it finds. Using DefaultTargets you can call multiple:

<Project DefaultTargets="T1;T2">

A Target should accomplish all of one well defined step. For example a Clean target would remove all the necessary files and folders needed to clean the project. Compile would compile all the DLLs, etc.

Your targets should normally declare their own dependencies:

<Target Name="CI" DependsOnTargets="T1, T2">
</Target>

Otherwise your target should contain all the individual steps that you want to run:

<Target Name="XX">
    <CallTarget Targets="T1"/>
    <CallTarget Targets="T2"/>
</Target>
毁虫ゝ 2024-08-02 11:39:49

MSBuild 使用此顺序来确定应执行哪些目标。 一旦找到一个值,它就会停止并开始执行。

  • 使用 msbuild.exe 上的 /t 开关指定的目标
  • Project 元素上的 DefaultTargets 属性中包含的目标
  • 在构建脚本中找到的第一个目标(实际上稍微复杂一点) >)

正如前面的评论者所说,您可以使用 DependsOnTargets 列表让其他目标在该目标之前先执行。

关于您的解决方案,AfterTargets 仅在 MSBuild 4.0 中可用,因此不适用于以前的版本。

Sayed Ibrahim Hashimi

我的书:Microsoft 构建引擎内部:使用 MSBuild 和 Team Foundation Build

MSBuild uses this order to determine what target(s) should be executed. Once a value is found it stops there and begins execution.

  • The target(s) you specify using the /t switch on msbuild.exe
  • The targets(s) contained in the DefaultTargets attribute on the Project element
  • The first target which is found in the build script (slightly morecomplicated actually)

As the previous commenter stated you can use the DependsOnTargets list to have other targets execute first before that target.

About your solution, AfterTargets is only available in MSBuild 4.0 so that will not work with previous versions.

Sayed Ibrahim Hashimi

My Book: Inside the Microsoft Build Engine : Using MSBuild and Team Foundation Build

不乱于心 2024-08-02 11:39:49

或者,您可以创建一个定义所有依赖目标的属性组:

 <PropertyGroup>
        <BuildDependsOn>T1;T2</BuildDependsOn>
 </PropertyGroup>

然后将 BuildDependsOn 的值作为参数传递,如下所示:

<Target Name="Build" DependsOnTargets="@(BuildDependsOn)"/>

此方法允许在此项目文件之外修改依赖列表注入其他所需的步骤。 有关详细信息,请参阅此网站

Alternatively you can create a property group that defines all the dependent targets:

 <PropertyGroup>
        <BuildDependsOn>T1;T2</BuildDependsOn>
 </PropertyGroup>

And then pass the value of BuildDependsOn as a parameter as shown below:

<Target Name="Build" DependsOnTargets="@(BuildDependsOn)"/>

This approach allows the depends on list to be modified outside of this project file to inject other required steps. See this site for more info.

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