【问题标题】:MSBuild clean directory stringMSBuild 清理目录字符串
【发布时间】:2010-08-06 15:09:25
【问题描述】:

我在 MSBuild 中有一个属性来表示 MSBuildProjectDirectory 上方的目录:

<PropertyGroup>
    <BuildDir>$(MSBuildProjectDirectory)\..</PRSBuildDir>
</PropertyGroup>

然后我需要使用此属性,但我需要清理目录字符串以使其不包含..。换句话说,我需要评估..,因此如果当前项目文件在C:\Test\Tom\MyDir 中,那么我需要一个包含字符串C:\Test\Tom 的属性。

我问的原因是因为我试图运行这样的命令:

msiexec /passive /i "D:\Build\2.3.84.40394\Deployment\..\Vendor\LogParser.msi"

但它抱怨到 msi 的路径:This installation package could not be opened. Verify that the package exists and that you can access it, or contact the application vendor to verify that this is a valid Windows Installer package.

【问题讨论】:

标签: msbuild relative-path absolute-path


【解决方案1】:

有一个ConvertToAbsolutePath task,有什么用吗?

【讨论】:

  • 是的,绝对有用,但请注意,如果您传入带有 .. 的绝对路径,例如“$(MSBuildProjectDirectory)\..”,任务只会吐出相同的字符串。所以你必须传入一个相对路径才能“清理”它。谢谢
【解决方案2】:

我现在最好的方法如下,但我想知道是否有更好的方法..

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <PropertyGroup>
        <BuildDir>$(MSBuildProjectDirectory)\..</BuildDir>
    </PropertyGroup>

    <Target Name="Test">
        <ItemGroup>
            <CleanBuildDir Include="$(BuildDir)" />
        </ItemGroup>

        <PropertyGroup>
            <BuildDir>%(CleanBuildDir.FullPath)</BuildDir>
        </PropertyGroup>

        <Message Text="$(BuildDir)" />
    </Target>
</Project>

【讨论】:

    【解决方案3】:

    如果您想评估通配符,您应该使用 Item 而不是 Property。

    <ItemGroup>
      <BuildDir Include="$(MSBuildProjectDirectory)\.."/>
    </ItemGroup>
    
    <Target Name="ExecMSIExec">
      <Exec Command="msiexec /passive /i %(BuildDir.FullPath)\Vendor\LogParser.msi"/>
    </Target>
    

    【讨论】:

      【解决方案4】:

      (删除了我的答案,因为我没有看到汤姆的回答完全一样!)

      顺便说一句,为什么不将实际调用 msiexec 的 Exec 任务的“WorkingDirectory”属性设置为 MSI 的位置 - 这样您就不会遇到路径长度问题

      【讨论】:

        猜你喜欢
        • 2011-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-29
        • 2022-01-06
        • 2010-11-17
        相关资源
        最近更新 更多