如何在msbuild脚本中获取当前目录?

时间:2022-09-02 02:14:36

In my msbuild script I need to pass the full directory as a parameter. How can get it?

在我的msbuild脚本中,我需要将整个目录作为参数传递。如何得到它?

Example: I am running the script from C:\dev, I want a relative path temp, so I am after C:\dev\temp

示例:我正在运行C:\dev的脚本,我想要一个相对路径temp,所以我在C:\dev\temp之后。

Note: I don't know from which folder the script will be run.

注意:我不知道脚本将在哪个文件夹中运行。

3 个解决方案

#1


83  

Igor is pretty close. MSBuildProjectDirectory is the property that will give you the full path to the project file which was invoked on the command line. So if you have the following scripts:

伊戈尔是相当接近。MSBuildProjectDirectory属性将为您提供在命令行上调用的项目文件的完整路径。如果你有以下脚本:

  • C:\temp\MyProj.proj
  • C:\ temp \ MyProj.proj
  • C:\shared\shared.targets
  • C:\ \ shared.targets共享

And MyProj.proj imports shared.targets and this is the one passed to msbuild.exe then the value for MSBuildProjectDirectory will always be C:\temp even if you are referencing that inside of shared.targets. If your shared.targets requires path knowledge then those should be declared in known properties. For example C# project files define the value for OutputPath and the shared file Microsoft.Common.targets uses that property.

和MyProj。项目进口共享。这是传递给msbuild的目标。那么MSBuildProjectDirectory的值将始终为C:\temp,即使您正在shared.targets内部引用它。如果你的共享。目标需要路径知识,然后那些应该在已知属性中声明。例如,c#项目文件定义OutputPath和共享文件Microsoft.Common的值。使用该属性的目标。

Edit: MSBuild 4

编辑:MSBuild 4

If you are using MSBuild 4 you can now use the properties for this type of value.

如果您正在使用MSBuild 4,那么您现在可以使用这种类型的值的属性。

  • MSBuildThisFile
  • MSBuildThisFile
  • MSBuildThisFileDirectory
  • MSBuildThisFileDirectory
  • MSBuildThisFileDirectoryNoRoot
  • MSBuildThisFileDirectoryNoRoot
  • MSBuildThisFileExtension
  • MSBuildThisFileExtension
  • MSBuildThisFileFullPath
  • MSBuildThisFileFullPath
  • MSBuildThisFileName
  • MSBuildThisFileName

See http://sedodream.com/2010/03/11/MSBuild40ReservedProperties.aspx.

见http://sedodream.com/2010/03/11/MSBuild40ReservedProperties.aspx。

#2


47  

Here are 3 Targets that are helpful.

以下是三个有用的目标。

WhereAmI is the one I use when trying to figure out my current directory of course. The others are informative as well. (Some beyond the scope of the question).

ami是我在计算当前目录时使用的。其他的也提供信息。(有些超出了问题的范围)。

<Target Name="WhereAmI">
    <Message Text=" Here I Am  " />
    <Exec Command="dir ." />
    <Message Text=" " />
</Target>



<Target Name="ShowReservedProperties" AfterTargets="BeforeBuild">  
    <Message Text=" MSBuildProjectDirectory  = $(MSBuildProjectDirectory)" Importance="high" />     
    <Message Text=" MSBuildProjectFile  = $(MSBuildProjectFile)" Importance="high" />   
    <Message Text=" MSBuildProjectExtension  = $(MSBuildProjectExtension)" Importance="high" />     
    <Message Text=" MSBuildProjectFullPath  = $(MSBuildProjectFullPath)" Importance="high" />   
    <Message Text=" MSBuildProjectName  = $(MSBuildProjectName)" Importance="high" />   
    <Message Text=" MSBuildBinPath  = $(MSBuildBinPath)" Importance="high" />   
    <Message Text=" MSBuildProjectDefaultTargets  = $(MSBuildProjectDefaultTargets)" Importance="high" />   
    <Message Text=" MSBuildExtensionsPath  = $(MSBuildExtensionsPath)" Importance="high" />     
    <Message Text=" MSBuildStartupDirectory  = $(MSBuildStartupDirectory)" Importance="high"/>
</Target>


  <Target Name="ShowOtherProperties">  
    <Message Text="  " />
    <Message Text="  " />
    <Message Text=" Environment (SET) Variables*       " />
    <Message Text=" ---------------------------        " />
    <Message Text=" COMPUTERNAME = *$(COMPUTERNAME)*   " />
    <Message Text=" USERDNSDOMAIN = *$(USERDNSDOMAIN)* " />
    <Message Text=" USERDOMAIN = *$(USERDOMAIN)*       " />
    <Message Text=" USERNAME = *$(USERNAME)*           " />
</Target>

If you're using an "external msbuild file" and need to pass a filename or path to it (because external msbuild files do not like relative files if they are not in the same directory as the calling .msbuild file)....here is a convenient (3.5 and up I believe) Task.

如果您正在使用一个“外部msbuild文件”,需要通过一个文件名或路径(因为外部msbuild文件不喜欢相对文件如果不与调用.msbuild文件相同的目录中)....这里有一个方便的(我认为是3.5)的任务。

    <ConvertToAbsolutePath Paths="..\"> <!-- Some relative path here -->
      <Output TaskParameter="AbsolutePaths" PropertyName="MyAbsolutionPathProperty"/>
    </ConvertToAbsolutePath>            
    <Message Text="'MyAbsolutionPathProperty' = '$(MyAbsolutionPathProperty)'" />   

#3


11  

MSBuild has reserved property called MSBuildProjectDirectory, which is to the absolute path of the directory where you project or script file is located, C:\Dev in your case. Therefore "$(MSBuildProjectDirectory)\temp" is exactly what you're looking for.

MSBuild保留了一个名为MSBuildProjectDirectory的属性,它位于您的项目或脚本文件所在目录的绝对路径中,C:\Dev在您的案例中。因此,“$(MSBuildProjectDirectory)\临时”正是您要寻找的。

#1


83  

Igor is pretty close. MSBuildProjectDirectory is the property that will give you the full path to the project file which was invoked on the command line. So if you have the following scripts:

伊戈尔是相当接近。MSBuildProjectDirectory属性将为您提供在命令行上调用的项目文件的完整路径。如果你有以下脚本:

  • C:\temp\MyProj.proj
  • C:\ temp \ MyProj.proj
  • C:\shared\shared.targets
  • C:\ \ shared.targets共享

And MyProj.proj imports shared.targets and this is the one passed to msbuild.exe then the value for MSBuildProjectDirectory will always be C:\temp even if you are referencing that inside of shared.targets. If your shared.targets requires path knowledge then those should be declared in known properties. For example C# project files define the value for OutputPath and the shared file Microsoft.Common.targets uses that property.

和MyProj。项目进口共享。这是传递给msbuild的目标。那么MSBuildProjectDirectory的值将始终为C:\temp,即使您正在shared.targets内部引用它。如果你的共享。目标需要路径知识,然后那些应该在已知属性中声明。例如,c#项目文件定义OutputPath和共享文件Microsoft.Common的值。使用该属性的目标。

Edit: MSBuild 4

编辑:MSBuild 4

If you are using MSBuild 4 you can now use the properties for this type of value.

如果您正在使用MSBuild 4,那么您现在可以使用这种类型的值的属性。

  • MSBuildThisFile
  • MSBuildThisFile
  • MSBuildThisFileDirectory
  • MSBuildThisFileDirectory
  • MSBuildThisFileDirectoryNoRoot
  • MSBuildThisFileDirectoryNoRoot
  • MSBuildThisFileExtension
  • MSBuildThisFileExtension
  • MSBuildThisFileFullPath
  • MSBuildThisFileFullPath
  • MSBuildThisFileName
  • MSBuildThisFileName

See http://sedodream.com/2010/03/11/MSBuild40ReservedProperties.aspx.

见http://sedodream.com/2010/03/11/MSBuild40ReservedProperties.aspx。

#2


47  

Here are 3 Targets that are helpful.

以下是三个有用的目标。

WhereAmI is the one I use when trying to figure out my current directory of course. The others are informative as well. (Some beyond the scope of the question).

ami是我在计算当前目录时使用的。其他的也提供信息。(有些超出了问题的范围)。

<Target Name="WhereAmI">
    <Message Text=" Here I Am  " />
    <Exec Command="dir ." />
    <Message Text=" " />
</Target>



<Target Name="ShowReservedProperties" AfterTargets="BeforeBuild">  
    <Message Text=" MSBuildProjectDirectory  = $(MSBuildProjectDirectory)" Importance="high" />     
    <Message Text=" MSBuildProjectFile  = $(MSBuildProjectFile)" Importance="high" />   
    <Message Text=" MSBuildProjectExtension  = $(MSBuildProjectExtension)" Importance="high" />     
    <Message Text=" MSBuildProjectFullPath  = $(MSBuildProjectFullPath)" Importance="high" />   
    <Message Text=" MSBuildProjectName  = $(MSBuildProjectName)" Importance="high" />   
    <Message Text=" MSBuildBinPath  = $(MSBuildBinPath)" Importance="high" />   
    <Message Text=" MSBuildProjectDefaultTargets  = $(MSBuildProjectDefaultTargets)" Importance="high" />   
    <Message Text=" MSBuildExtensionsPath  = $(MSBuildExtensionsPath)" Importance="high" />     
    <Message Text=" MSBuildStartupDirectory  = $(MSBuildStartupDirectory)" Importance="high"/>
</Target>


  <Target Name="ShowOtherProperties">  
    <Message Text="  " />
    <Message Text="  " />
    <Message Text=" Environment (SET) Variables*       " />
    <Message Text=" ---------------------------        " />
    <Message Text=" COMPUTERNAME = *$(COMPUTERNAME)*   " />
    <Message Text=" USERDNSDOMAIN = *$(USERDNSDOMAIN)* " />
    <Message Text=" USERDOMAIN = *$(USERDOMAIN)*       " />
    <Message Text=" USERNAME = *$(USERNAME)*           " />
</Target>

If you're using an "external msbuild file" and need to pass a filename or path to it (because external msbuild files do not like relative files if they are not in the same directory as the calling .msbuild file)....here is a convenient (3.5 and up I believe) Task.

如果您正在使用一个“外部msbuild文件”,需要通过一个文件名或路径(因为外部msbuild文件不喜欢相对文件如果不与调用.msbuild文件相同的目录中)....这里有一个方便的(我认为是3.5)的任务。

    <ConvertToAbsolutePath Paths="..\"> <!-- Some relative path here -->
      <Output TaskParameter="AbsolutePaths" PropertyName="MyAbsolutionPathProperty"/>
    </ConvertToAbsolutePath>            
    <Message Text="'MyAbsolutionPathProperty' = '$(MyAbsolutionPathProperty)'" />   

#3


11  

MSBuild has reserved property called MSBuildProjectDirectory, which is to the absolute path of the directory where you project or script file is located, C:\Dev in your case. Therefore "$(MSBuildProjectDirectory)\temp" is exactly what you're looking for.

MSBuild保留了一个名为MSBuildProjectDirectory的属性,它位于您的项目或脚本文件所在目录的绝对路径中,C:\Dev在您的案例中。因此,“$(MSBuildProjectDirectory)\临时”正是您要寻找的。