VS2010 - 使用<import>在安装项目之间共享属性?</import>

时间:2012-09-07 20:39:29

标签: visual-studio-2010 visual-studio msbuild wix

为什么它不能用于<Import />这个文件,当它替换语句时只需复制粘贴三个属性就可以了?

../../ Setup.Version.proj

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <InstallerMajorVersion>7</InstallerMajorVersion>
        <InstallerMinorVersion>7</InstallerMinorVersion>
        <InstallerBuildNumber>7</InstallerBuildNumber>
    </PropertyGroup>
</Project>

使用:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <InstallerMajorVersion>7</InstallerMajorVersion>
        <InstallerMinorVersion>7</InstallerMinorVersion>
        <InstallerBuildNumber>7</InstallerBuildNumber>
        <OutputName>asdf-$(InstallerMajorVersion).$(InstallerMinorVersion).$(InstallerBuildNumber)</OutputName>
        <OutputType>Package</OutputType>

不起作用:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="../../Setup.Version.proj" />
    <PropertyGroup>
        <OutputName>asdf-$(InstallerMajorVersion).$(InstallerMinorVersion).$(InstallerBuildNumber)</OutputName>
        <OutputType>Package</OutputType>

这里的变量只是简单地用空字符串...... :(我确定导入项目的路径是正确的。


所以当我从命令行运行msbuild时似乎工作正常,但是当我在Visual Studio中构建项目时却没有。为什么在这个世界?

3 个答案:

答案 0 :(得分:2)

我试图重现并且一切正常:

<强> importme.proj

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <InstallerMajorVersion>7</InstallerMajorVersion>
        <InstallerMinorVersion>7</InstallerMinorVersion>
        <InstallerBuildNumber>7</InstallerBuildNumber>
    </PropertyGroup>
</Project>

<强> main.proj

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="importme.proj" />
    <PropertyGroup>
        <OutputName>asdf-$(InstallerMajorVersion).$(InstallerMinorVersion).$(InstallerBuildNumber)</OutputName>
        <OutputType>Package</OutputType>
    </PropertyGroup>
    <Target Name="Build">
        <Message Text="$(OutputName)"/>
    </Target>
</Project>

<强>输出

Microsoft (R) Build Engine version 4.0.30319.17626
[Microsoft .NET Framework, version 4.0.30319.17626]
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 10.09.2012 12:35:12.
Project "d:\temp\SO\main.proj" on node 1 (default targets).
Build:
  asdf-7.7.7
Done Building Project "d:\temp\SO\main.proj" (default targets).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.55

使用

运行
msbuild main.proj

更新:您需要为要更新的Include'd文件重新加载项目,或使用workaround

答案 1 :(得分:2)

它没有工作的原因是Visual Studio缓存了包含的文件,因此我对其所做的任何更改都没有生效,直到我重新加载解决方案或重新启动Visual Studio。这似乎是VS 2010的限制。重新加载VS 2010后,一切都按预期工作。

答案 2 :(得分:0)

Import元素仅在Project元素下有效。但是,代码似乎想要将值导入<ProjectGroup>元素。尝试将值声明为Setup.Version.proj中的变量,并在主MSBuild文件中引用它们,例如:

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="../../Setup.Version.proj" /> <!-- Variables declared in here -->
    <PropertyGroup>
        <InstallerMajorVersion>$(MyInstallerMajorVersion)</InstallerMajorVersion>
        <InstallerMinorVersion>$(MyInstallerMinorVersion)</InstallerMinorVersion>
        <InstallerBuildNumber>$(MyInstallerBuildNumber)</InstallerBuildNumber>
        <OutputName>asdf-$(InstallerMajorVersion).$(InstallerMinorVersion).$(InstallerBuildNumber)</OutputName>
        <OutputType>Package</OutputType>