16

我正在自定义一个.csproj项目以在主构建之前运行一些自定义任务。但是,我根本无法执行任务。

我取消了文件中的<Target Name="BeforeBuild" />元素的注释.csproj并添加了一个简单的消息任务,但是当我构建时,消息没有出现在我的输出中,所以似乎任务没有运行。所以这个片段不输出消息;

清单 1:没有消息出现

<Target Name="BeforeBuild">
    <Message Text="About to build ORM layer" Importance="normal" />
</Target>

但是,如果我搞砸了某些属性,我可能会.csproj导致根本无法执行;

清单 2:MSBuild 配置错误

<Target Name="BeforeBuild">
    <Message Text="About to build ORM layer" XXImportance="normal" />
</Target>

注意XXImportance属性。我得到的构建错误是

My.csproj(83,46): error MSB4064: The "XXImportance" parameter is not supported by the "Message" task. Verify the parameter exists on the task, and it is a settable public instance property.

这表明正在解析 XML,Message已找到该类,并且正在反映该类以获取可用属性。

为什么任务不执行?

我正在使用 3.5 框架。

更新 1:根据@Martin 的建议,我尝试在控制台上运行 MSBuild,但出现此错误;

c:\path\to\my.csproj(74,11): error MSB4019: The imported
project "C:\Microsoft.CSharp.targets" was not found. Confirm
that the path in the <Import> declaration is correct, and that
the file exists on disk.

第 74 行读取;

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

更新 2:我在 VS2008 中编译,它使用 C#3 编译器,但我正在编译的项目是框架 2.0 项目。从命令行运行时(请参阅更新 1),构建似乎失败了,因为Microsoft.CSharp.targets文件的指定位置存在混淆。

4

2 回答 2

23

Had the same problem today and found the way to make it work.

The BeforeBuild target in your .csproj file is intended to be a redefinition of a target defined (and referenced) in the Microsoft.Common.targets file, which is imported by the Microsoft.CSharp.targets file, which in turn is imported by your .csproj.

Your problem is that the line in your .csproj that imports Microsoft.CSharp.targets is after your definition of the BeforeBuild target. Move the import line to above your BeforeBuild target and everything should work fine.

Hope that helps,

于 2012-06-08T01:15:27.147 回答
16

该事件正在触发,但您可能需要更改 VS 中的设置:

工具->选项->项目和解决方案->构建和运行:

并将 MSBUild 详细程度设置为最小或正常。

Also, if you compile through msbuild in the console you will see the message without having to change the above settings.

于 2010-07-28T11:21:44.420 回答