VS2010 - Using to share properties betwee

2019-07-20 03:04发布

问题:

Why doesn't it work to <Import /> this file, when it works when I replace the statement with just copy-pasting the three properties?

../../Setup.Version.proj

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

Works:

<?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>

Doesn't work:

<?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>

Here the variables just evaulate to empty strings... :( I'm certain the path to the imported project is correct.


So it seems to work fine when i run msbuild from the command line, but not when I build the project inside Visual Studio. Why in the world?

回答1:

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.



回答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.



回答3:

The Import element is only valid under the Project element. However, the code appears to want to import the values into a <ProjectGroup> element. Try declaring the values as variables in Setup.Version.proj and reference them in your main MSBuild file, such as:

<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>