MSBuild无法从导入的项目中运行BuildDependsOn任务

问题描述:

我有一个导入的MSBuild项目(appconfig.transformation.targets),该项目定义了一些任务并将其放在BuildDependsOn属性中.我已将此文件放在解决方案的*文件夹中(Projects \ LibrariesSolution \ appconfig.transformation.targets):

I have an imported MSBuild project (appconfig.transformation.targets) that defines some tasks and puts them in the BuildDependsOn property. I've placed this file in the top level folder for a solution (Projects\LibrariesSolution\appconfig.transformation.targets):

<PropertyGroup>
  <BuildDependsOn>
    TransformWebConfig;
    OverrideAppConfigWithTargetPath;
    $(BuildDependsOn);
    CopyTransformedConfig
  </BuildDependsOn>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />

我将此项目导入子文件夹中解决方案内的每个csproj文件.因此,Projects \ LibrariesSolution \ Project1 \ Project1.csproj具有以下内容:

I import this project in each csproj file inside the solution in subfolders. So Projects\LibrariesSolution\Project1\Project1.csproj has something like this:

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
  <Import Project="..\appconfig.transformation.targets" />
  ... the rest of the csproj stuff ...

我像这样启动构建:

cd LibrariesSolution
c:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe /t:Clean /p:Configuration=Release Project1\Project1.csproj
c:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe /t:Build /p:Configuration=Release Project1\Project1.csproj

但是BuildDependsOn中列出的目标似乎没有被调用.当然,输出太冗长而无法在此处显示,但是"TransformWebConfig"不会出现在输出中的任何位置.没有显示错误.有什么事吗我应该怎么做才能进一步排除故障?

But the targets listed in BuildDependsOn do not appear to be called. Of course the output is way too verbose to show here, but "TransformWebConfig" does not appear anywhere in the output. No errors are shown. What could be wrong? What should I do to troubleshoot the process further?

您遇到导入顺序问题.您正在导入定义属性BuildDependsOn的文件(appconfig.transformation.targets).在项目文件的后面,将导入Microsoft目标,它们重新定义BuildDependsOn并清除您的更改,因为它们的定义在BuildDependsOn中不包含任何现有值.

You have an import order problem. You are importing your file (appconfig.transformation.targets) which defines the property BuildDependsOn. Later in the project file, the Microsoft targets are imported, which redefine BuildDependsOn and wipe out your changes because their definition does not include any existing value in BuildDependsOn.

BuildDependsOn在Microsoft.Common.targets中定义,由Microsoft.CSharp.targets为C#项目导入.您的导入应在导入Microsoft.CSharp.targets之后进行.

BuildDependsOn is defined in Microsoft.Common.targets which is imported by Microsoft.CSharp.targets for a C# project. Your import should go after the import of Microsoft.CSharp.targets.