通过msbuild添加和运行自定义目标 [英] Adding and running custom target by msbuild
问题描述
此刻,我正在努力处理看似非常基本的任务,即将一个新的自定义目标添加到csproj文件中,并使用msbuild从命令行运行它.
I am struggling with seemingly very basic task at the moment, that is addding a new custom target to csproj file and run it from the command line using msbuild.
我在网络上进行了广泛的研究,但没有发现任何有效的解决方案.
I did an extensive research on the net but I found no solution that is actually working.
假设我将以下目标添加到我的csproj文件中:
Let's say that I add the following target to my csproj file:
<Target Name="TeamCity">
<Message Text="I am Running!"/>
</Target>
甚至依赖于Build的东西:
or even something that depends on Build:
<Target Name="TeamCity" DependsOnTargets="Build">
<Message Text="I am Running!"/>
</Target>
这是msbuild文档的建议.
This is what msbuild documentation suggests.
但是运行目标似乎是不可能完成的任务. 虽然我可以在csproj上运行预定义的目标:
But running the target seems to be a mission impossible. While I am able to run predefined target on csproj:
msbuild MySolution.sln /t:MyProject:Rebuild /p:Configuration="Release" /p:Platform="Any CPU"
我无法运行刚刚添加的目标- TeamCity 目标:
I am not able to run the target I just added - that is TeamCity target:
msbuild MySolution.sln /t:MyProject:TeamCity /p:Configuration="Release" /p:Platform="Any CPU"
我总是收到错误MSB4057:项目中不存在目标"TeamCity".
I always get error MSB4057: The target "TeamCity" does not exist in the project.
使这种运行深入人心的秘密是什么?
What is the deeply kept secret to make this running?
PS.请注意,我需要任务在项目级别而不是解决方案上进行.而且我需要运行msbuild MySolution.sln ...不会有太多错误地建议msbuild MyProject.csproj ...
PS. Please note that I need the task to be working on a project level not on a solution. And I need to run msbuild MySolution.sln ... not as many incorrectly suggest msbuild MyProject.csproj ...
推荐答案
秘密很简单-您无法运行它.
The secret is pretty simple - you can't make this running.
因为,msbuild会生成中间项目文件(YourSolution.sln.metproj),但不会包含从YourProject.csproj导入的文件,包括.targets文件.这就是无法识别YourCustomTarget的原因.
Because, msbuild generates intermediate project file (YourSolution.sln.metproj) but that will not have the imports from YourProject.csproj, including the .targets files. That's why YourCustomTarget is not recognized.
您可以尝试使用Before/After Targets
将目标插入预定义的构建流程中.
What you can try is using Before/After Targets
to inject your targets in predefined build flow.
希望这会有所帮助.
这篇关于通过msbuild添加和运行自定义目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!