如何在<startarguments> a(cs | vb | fs)proj.user中指定$(TargetPath)?</startarguments>

时间:2012-10-07 20:06:08

标签: visual-studio-2010 msbuild

我有一个Visual Studio项目,其启动操作设置为在PowerShell中加载DLL,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup Condition="'$(Platform)' == 'Debug|x86'">
    <StartAction>Program</StartAction>
    <StartProgram>C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe</StartProgram>
    <StartArguments>-NoProfile -NoExit -Command Add-Type -Path  "Foo.dll" -PassThru | Select FullName</StartArguments>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
    <StartAction>Program</StartAction>
    <StartProgram>c:\windows\system32\windowspowershell\v1.0\powershell.exe</StartProgram>
    <StartArguments>-NoProfile -NoExit -Command Add-Type -Path  "Foo.dll" -PassThru | Select FullName</StartArguments>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
    <StartProgram>C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe</StartProgram>
    <StartArguments>-NoProfile -NoExit -Command Add-Type -Path  "Foo.dll" -PassThru | Select FullName</StartArguments>
    <StartAction>Program</StartAction>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
    <StartProgram>c:\windows\system32\windowspowershell\v1.0\powershell.exe</StartProgram>
    <StartArguments>-NoProfile -NoExit -Command Add-Type -Path  "Foo.dll" -PassThru | Select FullName</StartArguments>
    <StartAction>Program</StartAction>
  </PropertyGroup>
</Project>

我手动将文件编辑为以下内容以删除冗余:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup '$(Platform)' == 'x86'">
    <StartProgram>C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe</StartProgram>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Platform)' == 'AnyCPU' or Condition="'$(Platform)' == 'x64'">
    <StartProgram>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</StartProgram>
  </PropertyGroup>
  <PropertyGroup>
    <StartAction>Program</StartAction>
    <StartArguments>-NoProfile -NoExit -Command Add-Type -Path  "Foo.dll" -PassThru | Select FullName</StartArguments>
  </PropertyGroup>
</Project>

现在我想用“$(TargetPath)”之类的东西替换硬编码的“Foo.dll”。但是,计算结果为空字符串。以下也解析为空字符串:

  • $(的AssemblyName)
  • $(的TargetName)$(TargetExt)
  • $(TargetFileName)

设置值<StartArguments>-NoProfile -NoExit -Command Add-Type -Path "$(Platform)" -PassThru | Select FullName</StartArguments>会创建命令行"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -NoExit -Command Add-Type -Path "x64" -PassThru | Select FullName

1 个答案:

答案 0 :(得分:2)

在所有导入指令(如<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

)之后移动属性声明

$(TargetPath)最初在Microsoft.Common.targets中定义,因此在脚本中导入该文件之前,它不会包含任何值。如果您的属性是在导入之前定义的(并且在导入的属性出现之前) - 在自上而下属性评估阶段,未定义的属性将被解析为空字符串。

注意:我错误地没有注意到你的意思是proj * .user *文件,而不是原始的.proj文件。我不知道视觉工作室何时加载.user文件以及加载期间设置的上下文。 您可以尝试使用原始.targets文件中的定义。这是TargetDir在Microsoft.common.targets中定义的方式: <TargetDir Condition="'$(OutDir)' != ''">$([MSBuild]::Escape($([System.IO.Path]::GetFullPath( $([System.IO.Path] ::结合($(MSBuildProjectDirectory)$(OutDir)))))))</TargetDir>

如果.user文件加载时没有任何以前的构建上下文 - 它仍然无法正确解析$(OutDir)。所以这仍然是你问题的不完整答案=(