是否可以从 MSBuild 的目标实现中引用目标的元数据?

发布于 2024-08-14 03:25:31 字数 1431 浏览 5 评论 0原文

我的 msbuild 目标文件包含以下部分:

<ItemGroup>
  <Targets Include="T1">
    <Project>A\B.sln"</Project>
    <DependsOnTargets>The targets T1 depends on</DependsOnTargets>
  </Targets>
  <Targets Include="T2">
    <Project>C\D.csproj"</Project>
    <DependsOnTargets>The targets T2 depends on</DependsOnTargets>
  </Targets>
  ...
</ItemGroup>
<Target Name="T1" DependsOnTargets="The targets T1 depends on">
  <MSBuild Projects="A\B.sln" Properties="Configuration=$(Configuration)" />
</Target>
<Target Name="T2" DependsOnTargets="The targets T2 depends on">
  <MSBuild Projects="C\D.csproj" Properties="Configuration=$(Configuration)" />
</Target>

如您所见,A\B.sln 出现两次:

  1. 作为 T1Project 元数据ItemGroup 部分。
  2. Target 语句本身中传递给 MSBuild 任务。

我想知道是否可以删除第二个实例并将其替换为对目标的 Project 元数据的引用,即为 Target 任务指定的名称?

对于 (Targets.DependsOnTargets) 元数据也提出了完全相同的问题。它被提及两次,就像 %(Targets.Project) 元数据一样。

谢谢。

编辑:

我可能应该描述解决方案必须满足的约束:

  1. 我希望能够轻松构建单独的项目。今天,我只需执行 msbuild file.proj /t:T1 即可构建 T1 目标,我希望保留此功能。
  2. 我想强调的是,有些项目依赖于其他项目,因此 DependsOnTargets 属性对它们来说确实是必要的。

My msbuild targets file contains the following section:

<ItemGroup>
  <Targets Include="T1">
    <Project>A\B.sln"</Project>
    <DependsOnTargets>The targets T1 depends on</DependsOnTargets>
  </Targets>
  <Targets Include="T2">
    <Project>C\D.csproj"</Project>
    <DependsOnTargets>The targets T2 depends on</DependsOnTargets>
  </Targets>
  ...
</ItemGroup>
<Target Name="T1" DependsOnTargets="The targets T1 depends on">
  <MSBuild Projects="A\B.sln" Properties="Configuration=$(Configuration)" />
</Target>
<Target Name="T2" DependsOnTargets="The targets T2 depends on">
  <MSBuild Projects="C\D.csproj" Properties="Configuration=$(Configuration)" />
</Target>

As you can see, A\B.sln appears twice:

  1. As Project metadata of T1 in the ItemGroup section.
  2. In the Target statement itself passed to the MSBuild task.

I am wondering whether I can remove the second instance and replace it with the reference to the Project metadata of the target, which name is given to the Target task?

Exactly the same question is asked for the (Targets.DependsOnTargets) metadata. It is mentioned twice much like the %(Targets.Project) metadata.

Thanks.

EDIT:

I should probably describe the constraints, which must be satisfied by the solution:

  1. I want to be able to build individual projects with ease. Today I can simply execute msbuild file.proj /t:T1 to build the T1 target and I wish to keep this ability.
  2. I wish to emphasize, that some projects depend on others, so the DependsOnTargets attribute is really necessary for them.

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

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

发布评论

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

评论(1

水波映月 2024-08-21 03:25:31

目标名称必须是固定值,因此这里的内容不起作用。

另外,我建议不要在 DependsOnTargets 表达式内部使用 批处理表达式 。如果您不完全理解正在发生的事情,这可能会导致奇怪的行为。

在您的情况下,您也许可以创建一个“驱动程序”目标,它使用这些项目来执行构建。唯一困难的部分是您尝试执行的 DependsOnTargets。我不确定你想要做什么的细节,所以不能提出任何建议,但至于其他的,请看看创建一个类似的目标。

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <ItemGroup>
    <Targets Include="T1">
      <Project>A\B.sln"</Project>
      <DependsOnTargets>The targets T1 depends on</DependsOnTargets>
    </Targets>  <Targets Include="T2">
      <Project>C\D.csproj"</Project>
      <DependsOnTargets>The targets T2 depends on</DependsOnTargets>
    </Targets>  ...
  </ItemGroup>

  <Target Name="Build">
    <!-- 
    This will be executed once for every unique value of Project in the 
    Target item group 
    -->
    <MSBuild Projects="%(Targets.Project)"
             Properties="Configuration=$(Configuration)"
  </Target> 
</Project>

Target names must be fixed values, so what you have here wouldn't work.

Also I would recommend not using Batching Expressions inside of the DependsOnTargets expression as well. This could lead to strange behavior if you do not fully understand what is happening.

In your case you may be able to just create a "driver" target which uses those items to perform the build. The only difficult part would be the DependsOnTargets that you are trying to perform. I'm not sure about the details on what you are trying to do with that so cannot make any suggestions but as for the other take a look at creating a target similar to.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <ItemGroup>
    <Targets Include="T1">
      <Project>A\B.sln"</Project>
      <DependsOnTargets>The targets T1 depends on</DependsOnTargets>
    </Targets>  <Targets Include="T2">
      <Project>C\D.csproj"</Project>
      <DependsOnTargets>The targets T2 depends on</DependsOnTargets>
    </Targets>  ...
  </ItemGroup>

  <Target Name="Build">
    <!-- 
    This will be executed once for every unique value of Project in the 
    Target item group 
    -->
    <MSBuild Projects="%(Targets.Project)"
             Properties="Configuration=$(Configuration)"
  </Target> 
</Project>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文