是否可以从 MSBuild 的目标实现中引用目标的元数据?
我的 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
出现两次:
- 作为
T1
的Project
元数据ItemGroup
部分。 - 在
Target
语句本身中传递给MSBuild
任务。
我想知道是否可以删除第二个实例并将其替换为对目标的 Project
元数据的引用,即为 Target
任务指定的名称?
对于 (Targets.DependsOnTargets)
元数据也提出了完全相同的问题。它被提及两次,就像 %(Targets.Project)
元数据一样。
谢谢。
编辑:
我可能应该描述解决方案必须满足的约束:
- 我希望能够轻松构建单独的项目。今天,我只需执行
msbuild file.proj /t:T1
即可构建 T1 目标,我希望保留此功能。 - 我想强调的是,有些项目依赖于其他项目,因此 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:
- As
Project
metadata ofT1
in theItemGroup
section. - In the
Target
statement itself passed to theMSBuild
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:
- 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. - I wish to emphasize, that some projects depend on others, so the
DependsOnTargets
attribute is really necessary for them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目标名称必须是固定值,因此这里的内容不起作用。
另外,我建议不要在 DependsOnTargets 表达式内部使用 批处理表达式 。如果您不完全理解正在发生的事情,这可能会导致奇怪的行为。
在您的情况下,您也许可以创建一个“驱动程序”目标,它使用这些项目来执行构建。唯一困难的部分是您尝试执行的 DependsOnTargets。我不确定你想要做什么的细节,所以不能提出任何建议,但至于其他的,请看看创建一个类似的目标。
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.