简体   繁体   中英

Including a .targets file in .csproj to globally define project & MSBuild variables

I need to have a single place to define variables for a solution that has about 13 projects, each having a varying combination of external dependencies from the same few locations. Right now, it's easy enough to include these as a variable in a PropertyGroup , but if something changes (like a version number), we don't want to have to update each project file with that change.

I tried creating a targets file that has the variables that get used from project to project and included it into the csproj file, just before the assembly references. This appears to have worked beautifully in a website project, but not in a class library project. The references are not found.

How am I supposed to do this in a way that's safe and usable across project types? (No, Nuget is not an option in this case.)

Example of the Global Targets file:

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Product1Version>02.01.01</Product1Version>
    <Product2Version>03.02.01</Product2Version>
    <ReferencesPath>..\..\References</ReferencesPath>
    <Product1ReferencePath>$(ReferencesPath)\Product1\$(Product1Version)</Product1ReferencePath>
    <Product2ReferencePath>$(ReferencesPath)\Product2\$(Product2Version)</Product2ReferencePath>
  </PropertyGroup>
</Project>

Here is an example of how I intend to use this in a csproj file:

  <Import Project="..\..\Build\SolutionReferences.targets" Condition="false" />
  <ItemGroup>
    <Reference Include="Product1">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>$(Product1ReferencePath)\Product1.dll</HintPath>
      <Private>False</Private>
    </Reference>
    <Reference Include="Product2">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>$(Product2ReferencePath)\Product2.dll</HintPath>
      <Private>False</Private>
    </Reference>
  </ItemGroup>

Ugh... All I had to do was remove Condition="false" from the Import command. :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM