无法使用 MSBUILD 构建我们的解决方案,它找不到我们的 .targets 文件

Rob*_*J1M 3 c# msbuild visual-studio-2008

我已经编写了一些 Windows 批处理文件来为我们的产品包含的所有 VS 解决方案调用 msbuild。

除了一个 SLN 之外,它们都可以工作,其中 CSPROJ 文件会导致问题。

这些项目都是在 VS2008 中构建的。

项目文件夹布局

\RootFolder
   \LinqToXsd
     -LinqToXsd.targets
     -Xml.Schema.Linq.dll
   \BL
     -BL.csproj
   \Config
     -Config.csproj
Run Code Online (Sandbox Code Playgroud)

我们已经自定义了 CSPROJ 文件以引入 LinqToXsd:

将此行添加到第一行PropertyGroup

<LinqToXsdBinDir Condition="'$(LinqToXsdBinDir)' == ''">$(SolutionDir)\..\LinqToXsd</LinqToXsdBinDir>
Run Code Online (Sandbox Code Playgroud)

而在Importfor之后的这一行Microsoft.CSharp.targets

<Import Project="$(LinqToXsdBinDir)\LinqToXsd.targets" />
Run Code Online (Sandbox Code Playgroud)

命令行:

C:\Windows\Microsoft.NET\Framework64\v3.5\msbuild.exe /m /target:Build /nologo /property:BuildInParallel=true;OutDir=E:\Projects\BuildMaster\trunk\built\ConfigTool\;Configuration=Release /verbosity:minimal "*snip*\ConfigTool\ConfigTool.sln"
Run Code Online (Sandbox Code Playgroud)

问题是,我们在输出中得到了这个:

*snip*\ConfigTool\ConfigTool.csproj(131,11): error MSB4019: The imported project "E:\LinqToXsd\LinqToXsd.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.
ConfigTool.csproj : Solution file warning MSB4122: Scanning project dependencies for project "ConfigTool.csproj" failed. The imported project "E:\LinqToXsd\LinqToXsd.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.  *snip*\ConfigTool\ConfigTool.csproj
*snip*\BL\BL.csproj(352,11): error MSB4019: The imported project "E:\LinqToXsd\LinqToXsd.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.
..\BL\BL.csproj : Solution file warning MSB4122: Scanning project dependencies for project "..\BL\BL.csproj" failed. The imported project "E:\LinqToXsd\LinqToXsd.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.  *snip*\BL\BL.csproj
C:\Windows\Microsoft.NET\Framework64\v3.5\Microsoft.Common.targets : warning MSB3245: Could not resolve this reference. Could not locate the assembly "Microsoft.Xml.Schema.Linq". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
Run Code Online (Sandbox Code Playgroud)

然而,我还有其他项目使用这些线条并且完美地工作。包括引用相同 CSPROJ 文件的文件(共享 BL)。

我试过删除所有 bin、obj、cache、suo 和用户文件/文件夹,没有区别。

有没有 msbuild 专家可以提供帮助?

谢谢,

J1M。

更新1

如果我替换这一行:

<LinqToXsdBinDir Condition="'$(LinqToXsdBinDir)' == ''">$(SolutionDir)..\LinqToXsd</LinqToXsdBinDir>
Run Code Online (Sandbox Code Playgroud)

有了这条线:

<LinqToXsdBinDir>$(MSBuildProjectDirectory)\..\LinqToXsd</LinqToXsdBinDir>
Run Code Online (Sandbox Code Playgroud)

在 ConfigTool 和 BL 项目文件中,我不再收到有关在编译 BL 时找不到 LinqToXsd 的错误,尽管它找不到已编译的 BL dll。

解决方案

首先,SolutionDir 仅由 VS2008 定义,似乎不是 MSBUILD。您可以在命令行中传递它,但我选择执行以下操作:

<LinqToXsdBinDir>$(MSBuildProjectDirectory)\..\LinqToXsd</LinqToXsdBinDir>
Run Code Online (Sandbox Code Playgroud)

MSBuildProjectDirectory 在 VS 和 MSBUILD 中都定义了。

二、不同的问题导致相同的症状(不知道为什么)

其中一个项目 (BL) 引用了下属 (SL),并且 SL 的 GUID 已更改。这在 VS 中无关紧要,但 MSBUILD真的不喜欢它。

删除并重新创建 refs 修复了第二个解决方案。

我只是在我的随机 csproj 黑客攻击中发现它,突然 MSBUILD 开始报告关于 project {guid} referencing project {guid} which is not available in the SLN file

谢谢,

J1M

Lee*_*Lee 5

MSBuild 未定义该SolutionDir属性,因此您需要手动指定它:

msbuild.exe /p:SolutionDir=... other options
Run Code Online (Sandbox Code Playgroud)