2

为什么它对这个文件不起作用<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 中构建项目时却不行。为什么在世界上?

4

3 回答 3

2

I've tried to reproduce and everything works:

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>

OUTPUT

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

Run using

msbuild main.proj

UPDATE: You need to reload project for Include'd files to be updated or use workaround.

于 2012-09-10T09:36:47.007 回答
2

The reason it didn't work is that Visual Studio had cached the included file, so that any changes I made to it didn't take effect until I reloaded the solution or restarted Visual Studio. This seems to be a limitation of VS 2010. After reloading VS 2010, everything worked as expected.

于 2012-09-14T07:08:56.393 回答
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>
于 2012-09-08T01:07:08.460 回答