如何获取当前msbuild文件的路径? [英] How can I get the path of the current msbuild file?

查看:1370
本文介绍了如何获取当前msbuild文件的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个msbuild .targets文件,在其中我想使用zip任务来压缩一些我知道相对于.targets文件位置的文件.

I'm writing an msbuild .targets file, and in it I want to use the zip task to zip up some files that I know the locations of relative to the .targets file.

当我直接执行.target文件时,此方法正常.但是,当我在另一个目录的.proj文件中包含.targets文件时,相对路径是相对于.proj文件而不是.targets文件来解析的.

This works ok when I execute the .target file directly. But, when I include the .targets file in an .proj file that's in another directory, the relative paths are resolved relative to the .proj file and not to the .targets file.

有没有一种方法可以指定相对于我写入它们的文件的相对路径?

Is there a way I can specify relative paths that are relative to the file I've written them in?

我已经看到了这个相关问题:

I've seen this related question:

如何在msbuild文件中提供相对路径?

我想到的一种解决方法是执行以下操作.我还没有尝试过.能行吗,有更好的方法吗?

A workaround I've thought of is to do the following. I haven't tried it yet. Will it work, is there a better way?

my.proj

<Import Project="..\Somewhere\my.targets"/>
<PropertyGroup>
   <MyTargetsYouAreHere>$(MSBuildProjectDirectory)\..\Somewhere</MyTargetsYouAreHere>
</PropertyGroup>

my.targets

my.targets

<Message Text="my.targets is here '$(MyTargetsYouAreHere)'" />

推荐答案

MSBuild处理文件时的工作方式是读取所有文件,并在所有这些文件的内存表示中创建一个.这一切都在执行任何目标之前发生.因此,在执行目标时,它没有包含目标文件的概念.基本上,您将无法使用.targets文件中的相对路径.解决这种情况的方法是确保外围的.proj文件(或您使用的任何扩展名)声明一个已知的属性,您的.targets文件将使用该属性来解析共享文件的完整路径.

The way MSBuild works when processing files is to read all files and create one in memory representation of all those files. This all happens before any target is executed. Because of this when a target is executing it has no concept of what file it was contained in. Basically you will not be able to use relative paths inside of .targets files. The way to deal with this situation is to ensure that your outlying .proj files (or whatever extension you use) to declare a known property which your .targets file uses to resolve the full path to the shared files.

例如

样本目标

<Project ...>
    <Target Name="ExecTool">
        <Exec Command="$(YourToolPath)tool.exe" />
    </Target>

</Project>

Build.proj

<Project ...>
    <PropertyGroup>
        <YourToolPath>$(MSBuildProjectDirectory)\..\</YourToolPath>
    </PropertyGroup>

    <Import Project="..\..\..\Sample.targets"/>
</Project>

在相关说明中,我讨论了在一段时间前

In a related note, I discussed validating such "shared" properties on my blog a while back at Elements of Reusable MSBuild Scripts: Validation.

如果您使用的是 MSBuild 4.0 (或更高版本),即Visual Studio 2010/.NET 4.0(也可以定位.NET 2.0/3.0/3.5).然后,您现在具有这些属性,可用于此特定目的:

If you are using MSBuild 4.0 (or above), i.e. Visual Studio 2010/.NET 4.0, (which can target .NET 2.0/3.0/3.5) as well. Then you now have these properties which can be used for this specific purpose:

  • MSBuildThisFile
  • MSBuildThisFileDirectory
  • MSBuildThisFileDirectoryNoRoot
  • MSBuildThisFile
  • MSBuildThisFileDirectory
  • MSBuildThisFileDirectoryNoRoot

这篇关于如何获取当前msbuild文件的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆