I know that the latest book out on MSBuild says this is not possible, but I am sure I have seen a property that points to the current file.
我知道关于MSBuild的最新书说这是不可能的,但我确信我已经看到了一个指向当前文件的属性。
Does anyone know a way to find the current file? (ie When the main MSBuild file imports a secondary file. What is the path to the secondary file from inside that file.)
有谁知道找到当前文件的方法? (即当主MSBuild文件导入辅助文件时。该文件中辅助文件的路径是什么。)
I am trying to register my file in HKEY_LOCALMACHINE\Software\Microsoft\VisualStudio\8.0\MSBuild\SafeImports, but the path to the file is not constant on each of my coworkers computers and it is imported from several different projects so doing a hard coded relative path is not a good idea either.
我试图在HKEY_LOCALMACHINE \ Software \ Microsoft \ VisualStudio \ 8.0 \ MSBuild \ SafeImports中注册我的文件,但文件的路径在我的每个同事计算机上都不是常量,而是从几个不同的项目导入,所以做了硬编码相对路径也不是一个好主意。
If there is a way to get the current working directory that will work too (the working directory switches with each import and I think I could save it off)
如果有办法获得当前工作目录也可以工作(工作目录切换每次导入,我想我可以保存它)
2 个解决方案
#1
There is not a reliable means to identify the "current file". When MSBuild processes a file it can import several other files, the end result is a single in memory complete representation of the file. When the targets are executing they do not know which file they were declared in.
没有可靠的方法来识别“当前文件”。当MSBuild处理文件时,它可以导入其他几个文件,最终结果是单个内存完整的文件表示。当目标执行时,他们不知道它们被声明在哪个文件中。
This is why reusable build scripts must be "parameterized" to accept the location to known relative locations. For example if you have a dependency on the location where your folder for 3rd party references are located, the build script which is driving the process must declare that property for you.
这就是为什么可重用的构建脚本必须“参数化”以接受已知相对位置的位置的原因。例如,如果您依赖于第三方引用的文件夹所在的位置,则驱动该进程的构建脚本必须为您声明该属性。
MSBuild 4.0 Only
If you are using MSBuild 4.0, 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:
如果您使用的是MSBuild 4.0,即Visual Studio 2010 / .NET 4.0(也可以是.NET 2.0 / 3.0 / 3.5)。然后,您现在拥有可用于此特定目的的这些属性:
MSBuildThisFile
MSBuildThisFileDirectory
MSBuildThisFileDirectoryNoRoot
Sayed Ibrahim Hashimi
Sayed Ibrahim Hashimi
My Book: Inside the Microsoft Build Engine : Using MSBuild and Team Foundation Build
我的书:Microsoft Build Engine内部:使用MSBuild和Team Foundation Build
#2
In 4.0+ you can use properties like $(MSBuildThisFile) to do exactly this. See here
在4.0+中,您可以使用$(MSBuildThisFile)等属性来完成此操作。看这里
#1
There is not a reliable means to identify the "current file". When MSBuild processes a file it can import several other files, the end result is a single in memory complete representation of the file. When the targets are executing they do not know which file they were declared in.
没有可靠的方法来识别“当前文件”。当MSBuild处理文件时,它可以导入其他几个文件,最终结果是单个内存完整的文件表示。当目标执行时,他们不知道它们被声明在哪个文件中。
This is why reusable build scripts must be "parameterized" to accept the location to known relative locations. For example if you have a dependency on the location where your folder for 3rd party references are located, the build script which is driving the process must declare that property for you.
这就是为什么可重用的构建脚本必须“参数化”以接受已知相对位置的位置的原因。例如,如果您依赖于第三方引用的文件夹所在的位置,则驱动该进程的构建脚本必须为您声明该属性。
MSBuild 4.0 Only
If you are using MSBuild 4.0, 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:
如果您使用的是MSBuild 4.0,即Visual Studio 2010 / .NET 4.0(也可以是.NET 2.0 / 3.0 / 3.5)。然后,您现在拥有可用于此特定目的的这些属性:
MSBuildThisFile
MSBuildThisFileDirectory
MSBuildThisFileDirectoryNoRoot
Sayed Ibrahim Hashimi
Sayed Ibrahim Hashimi
My Book: Inside the Microsoft Build Engine : Using MSBuild and Team Foundation Build
我的书:Microsoft Build Engine内部:使用MSBuild和Team Foundation Build
#2
In 4.0+ you can use properties like $(MSBuildThisFile) to do exactly this. See here
在4.0+中,您可以使用$(MSBuildThisFile)等属性来完成此操作。看这里