如何使用相对文件路径从另一个 msbuild 项目导入 msbuildcommunitytasks 项目?

发布于 2024-08-19 08:12:46 字数 405 浏览 4 评论 0原文

请放轻松,我是 msbuild 和 msbuildtasks 的新手!

如何设置表示我要导入的目标文件的相对文件路径的属性?我需要相关参考,以便它可以在所有开发机器上运行。但是导入的目标试图在内部使用相对文件路径,这将不起作用,因为它是相对于导入的目标重新评估的!

实际上,我正在尝试解决导入项目的记录的行为

导入的所有相对路径 项目的解释是相对于 导入项目的目录。 因此,如果项目文件是 导入到多个项目文件中 不同地点,相对 导入的项目文件中的路径 将被不同地解释为 每个导入的项目。

Please go easy I am new to msbuild and msbuildtasks!

How can I set a property which represents a relative file path to a targets file which I want to import? I need relative references so it will work on all dev machines. But the target for import is trying to use the relative file path internally, which won't work as it is re-evaluated relative to the imported target!

Effectively I am trying to work around the documented behaviour of imported projects:

All relative paths in imported
projects are interpreted relative to
the directory of the imported project.
Therefore, if a project file is
imported into several project files in
different locations, the relative
paths in the imported project file
will be interpreted differently for
each imported project.

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

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

发布评论

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

评论(6

许你一世情深 2024-08-26 08:12:46

是否可以在不安装的情况下使用 MSBuild 扩展包? 上有类似的问题。这个问题是如何对 MSBuild Extension Pack 执行相同操作,两者在这方面都很相似。对于扩展包,您必须声明属性 ExtensionTasksPath,对于社区任务,您必须声明一个名为 MSBuildCommunityTasksLib 的类似属性。所以在你的情况下它应该看起来像:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MSBuildCommunityTasksLib Condition="'$(MSBuildCommunityTasksLib)' == ''">E:\Data\Development\My Code\Community\MSBuild\CommunityTasks\</MSBuildCommunityTasksLib>
  </PropertyGroup>

  <Import Project="$(MSBuildCommunityTasksLib)MSBuild.Community.Tasks.Targets"/>

  <Target Name="Demo">
    <!-- Use the tasks here -->
  </Target>

</Project>

There was a similar question at Is it possible to use MSBuild Extension Pack without installation?. That question was how to do the same with the MSBuild Extension Pack, both of which are similar in this aspect. For the Extension Pack you have to declare the property ExtensionTasksPath,and for the Community tasks you have to declare a similar property named MSBuildCommunityTasksLib. So in your case it should look like:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MSBuildCommunityTasksLib Condition="'$(MSBuildCommunityTasksLib)' == ''">E:\Data\Development\My Code\Community\MSBuild\CommunityTasks\</MSBuildCommunityTasksLib>
  </PropertyGroup>

  <Import Project="$(MSBuildCommunityTasksLib)MSBuild.Community.Tasks.Targets"/>

  <Target Name="Demo">
    <!-- Use the tasks here -->
  </Target>

</Project>
朕就是辣么酷 2024-08-26 08:12:46

好的,我找到了答案。本质上,您必须将属性 MSBuildCommunityTasksPath 设置为返回原始包含目录的相对路径。

例如,给定如下文件夹结构:

Root---project---Build---{My msbuild project}
           |
           |-Tools---MSBuildCommunityTasks---{Binaries and Targets}

Where :
{My msbuild project} is in Root\Project\Build\
{MSbuildCommunityTasks} is in Root\Project\Tools\MsBuildCommunityTasks

要让目标项目通过属性 MSBuildCommunityTasksPath 引用其二进制文件,它将找到如下任务文件:

<PropertyGroup>
    <MSBuildCommunityTasksPath>..\MSBuildCommunityTasks\</MSBuildCommunityTasksPath> <!--Relative path back to yourself-->
</PropertyGroup>

然后您可以使用另一个相对文件引用导入目标文件:

  <Import Project="..\..\Tools\MSBuildCommunityTasks\MsBuild.Community.Tasks.Targets"/>

Ok, I've found the answer. Essentially you have to set the property MSBuildCommunityTasksPath as a relative path back to the original containing directory.

For example, given a folder structure like this:

Root---project---Build---{My msbuild project}
|
|-Tools---MSBuildCommunityTasks---{Binaries and Targets}

Where :
{My msbuild project} is in Root\Project\Build\
{MSbuildCommunityTasks} is in Root\Project\Tools\MsBuildCommunityTasks

To get the targets project to reference its binaries via the property MSBuildCommunityTasksPath, it will find the tasks file like this:

<PropertyGroup>
    <MSBuildCommunityTasksPath>..\MSBuildCommunityTasks\</MSBuildCommunityTasksPath> <!--Relative path back to yourself-->
</PropertyGroup>

Then you can import the targets file with another relative file reference :

  <Import Project="..\..\Tools\MSBuildCommunityTasks\MsBuild.Community.Tasks.Targets"/>
等风来 2024-08-26 08:12:46

@Sayed Ibrahim Hashimi

谈论 MSBuild4
仅声明 MSBuildCommunityTasksLib 是不够的,因为如果您检查 MSBuild.Community.Tasks.Targets 文件,则属性声明如下

<PropertyGroup>
    <MSBuildCommunityTasksPath Condition="'$(MSBuildCommunityTasksPath)' == ''">$(MSBuildExtensionsPath)\MSBuildCommunityTasks</MSBuildCommunityTasksPath>
    <MSBuildCommunityTasksLib>$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.dll</MSBuildCommunityTasksLib>
  </PropertyGroup>

因此,如果您仅覆盖 MSBuildCommunityTasksLib,它将再次在 MSBuild.Community.Tasks.Targets 文件中被覆盖,如下所示这不是有条件的
因此,您还必须覆盖 MSBuildCommunityTasksPath,以便其属性不是从 MSBuildExtensionsPath 设置,而是从您的自定义路径设置。
如果我错了请纠正我

@Sayed Ibrahim Hashimi

Talkin about MSBuild4
Just declaring the MSBuildCommunityTasksLib wont suffice cause if u check the MSBuild.Community.Tasks.Targets file the properties are declared as follows

<PropertyGroup>
    <MSBuildCommunityTasksPath Condition="'$(MSBuildCommunityTasksPath)' == ''">$(MSBuildExtensionsPath)\MSBuildCommunityTasks</MSBuildCommunityTasksPath>
    <MSBuildCommunityTasksLib>$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.dll</MSBuildCommunityTasksLib>
  </PropertyGroup>

So if U only over ride the MSBuildCommunityTasksLib it will again get over ridden in the MSBuild.Community.Tasks.Targets file as it is not conditional
So u HAVE TO ALSO OVERRIDE MSBuildCommunityTasksPath so that its proerty is NOT SET FROM MSBuildExtensionsPath but from ur custom path.
Correst me if I m wrong

玩套路吗 2024-08-26 08:12:46

这似乎是一个答案:

http://social.msdn.microsoft.com/forums/en-US/msbuild/thread/feb782e3-72ae-4476-9011-617796f217b6

但这(如果我理解正确的话)似乎很荒谬解决方案。为了获得工作路径,我需要更改导入的项目引用吗?如果我想引用另一个文件夹中的第三个项目导入的项目,会发生什么?!?

This appears to be one answer:

http://social.msdn.microsoft.com/forums/en-US/msbuild/thread/feb782e3-72ae-4476-9011-617796f217b6

But this (if I understand it correctly) appears to be a ridiculous solution. To get the paths to work I need to change the imported project references? What would happen if I wanted to reference the imported project from third project in another folder?!?

北音执念 2024-08-26 08:12:46

如果我很诚实的话,我是 msbuild 的菜鸟,但我刚刚解决了我自己的问题。我正在将其中一个目标转变为自己的项目,但它没有找到 msbuild 社区路径的路径。如果您查看原始项目,您可能会发现类似的内容

<PropertyGroup>
    <ExtensionTasksPath>./</ExtensionTasksPath>
    <MSBuildCommunityTasksPath>./</MSBuildCommunityTasksPath>
</PropertyGroup>

<Import Project="MSBuildExtensionPack\MSBuild.ExtensionPack.tasks"/>
<Import Project="MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>

将此代码复制到您的新项目中,它应该可以工作。

I'm a noob at msbuild if I'm quite honest however I've just solved my own problem I had with this. I was turning one of the targets into its own project and it wasn't finding the paths for the msbuild community paths. If you look at your original project you may find something like this

<PropertyGroup>
    <ExtensionTasksPath>./</ExtensionTasksPath>
    <MSBuildCommunityTasksPath>./</MSBuildCommunityTasksPath>
</PropertyGroup>

<Import Project="MSBuildExtensionPack\MSBuild.ExtensionPack.tasks"/>
<Import Project="MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>

Copy this code into your new project and it should work.

柠北森屋 2024-08-26 08:12:46

我只是想补充一点,因为我无法评论(代表),要为您的特定项目指定路径,您可以在属性组上使用 $(SolutionDir),如下所示:

$(SolutionDir)\My Code\Community\MSBuild\CommunityTasks \

这样,它就不会绑定到特定驱动器,并且可以基于项目相对于解决方案目录结构的位置。

还要感谢上面的答案,它通过上面的添加帮助我完成了我的项目。

I just wanted to add, since i cannot comment (rep), that to do a path to your particular project you can use $(SolutionDir) on your property group like so:

$(SolutionDir)\My Code\Community\MSBuild\CommunityTasks\

This way its not tied down to a specific drive and can be based off of the location of the project relative to your solutions directory structure.

Also thanks for the answer above it helped me in my project with the addition above.

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