You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用MSBuild在Jenkins构建项目时NuGet包未生效问题求助

问题

我们正尝试将项目的部署与构建从本地PC迁移至Jenkins服务器,计划通过批处理脚本调用MSBuild完成编译。为此重构了项目,将NuGet引用从package.config迁移至project.csproj中的<PackageReference>节点。

目前MSBuild已能成功下载所需的所有NuGet包,相关包已存入对应目录,且obj文件夹中的NuGet JSON文件也正确指向Jenkins用户的%userprofile%\.nuget\packages路径。

但构建项目时仍大量出现**“类型或命名空间未找到”错误(CS0246)**,表明NuGet包未被识别。而在本地从仓库拉取全新代码后,使用相同版本的MSBuild可正常构建。

错误示例:

c:\workspace\jenkins\jobs\myProject\myClass.cs(14,7): error CS0246: 找不到类型或命名空间名称“ANuGetPackageNameSpace”(是否缺少 using 指令或程序集引用?)。 [c:\workspace\jenkins\jobs\myProject\myProject.csproj]

附项目.csproj文件内容:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Test</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
    <GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
    <ProjectGuid>UUID</ProjectGuid>
    <OutputType>WinExe</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>myProject</RootNamespace>
    <AssemblyName>myProject</AssemblyName>
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <StartupObject>myProject.Program</StartupObject>
    <ApplicationIcon>Icon.ico</ApplicationIcon>
    <IsWebBootstrapper>false</IsWebBootstrapper>
    <ManifestCertificateThumbprint>ManifestThumbPrint</ManifestCertificateThumbprint>
    <ManifestKeyFile>myProject.pfx</ManifestKeyFile>
    <GenerateManifests>true</GenerateManifests>
    <TargetZone>LocalIntranet</TargetZone>
    <SignManifests>false</SignManifests>
    <OldToolsVersion>3.5</OldToolsVersion>
    <ApplicationVersion>5.2.012.38520</ApplicationVersion>
    <UseApplicationTrust>false</UseApplicationTrust>
    <BootstrapperEnabled>true</BootstrapperEnabled>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    <RestoreAdditionalProjectSources>NuGet-Service-Url_for_a_Framework.com</RestoreAdditionalProjectSources>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Test|x64'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Test\x64\</OutputPath>
    <DefineConstants>TRACE;TEST</DefineConstants>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <DebugType>full</DebugType>
    <PlatformTarget>x64</PlatformTarget>
    <LangVersion>7.3</LangVersion>
    <ErrorReport>prompt</ErrorReport>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Test|x86'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Test\x86\</OutputPath>
    <DefineConstants>TRACE;TEST</DefineConstants>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <DebugType>full</DebugType>
    <PlatformTarget>x86</PlatformTarget>
    <LangVersion>7.3</LangVersion>
    <ErrorReport>prompt</ErrorReport>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Prod|x64'">
    <OutputPath>bin\Prod\x64\</OutputPath>
    <DefineConstants>PROD</DefineConstants>
    <Optimize>false</Optimize>
    <DebugType>pdbonly</DebugType>
    <PlatformTarget>x64</PlatformTarget>
    <LangVersion>7.3</LangVersion>
    <ErrorReport>prompt</ErrorReport>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Prod|x86'">
    <OutputPath>bin\Prod\x86\</OutputPath>
    <DefineConstants>PROD</DefineConstants>
    <Optimize>false</Optimize>
    <DebugType>pdbonly</DebugType>
    <PlatformTarget>x86</PlatformTarget>
    <LangVersion>7.3</LangVersion>
    <ErrorReport>prompt</ErrorReport>
  </PropertyGroup>
  <PropertyGroup />
  <PropertyGroup>
    <SignAssembly>false</SignAssembly>
  </PropertyGroup>
  <PropertyGroup>
    <AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
  </PropertyGroup>
  <PropertyGroup>
    <ApplicationManifest>Properties\app.manifest</ApplicationManifest>
  </PropertyGroup>
  <ItemGroup>
    <!--Includes of all file, folder and classes-->
  </ItemGroup>
  <ItemGroup>
    <WebReferences Include="Web References\" />
  </ItemGroup>
  <ItemGroup>
    <WebReferenceUrl Include="MyService.com">
      <WebserviceStuff/>
    </WebReferenceUrl>
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Presenter\" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="NLog" Version="4.6.7" targetFramework="net472" />
    <PackageReference Include="NLog.Schema" Version="6.1.3" targetFramework="net472" />
    <PackageReference Include="NPOI" Version="2.5.1" targetFramework="net472" />
    <PackageReference Include="Portable.BouncyCastle" Version="1.8.6" targetFramework="net472" />
    <PackageReference Include="SharpZipLib" Version="1.2.0" targetFramework="net472" />
    <PackageReference Include="MorePackages" Version="24.2.7" targetFramework="net472" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <ProjectExtensions>
    <VisualStudio>
      <UserProperties BuildVersion_StartDate="2012/9/1" BuildVersion_UpdateFileVersion="True" BuildVersion_UpdateAssemblyVersion="True" BuildVersion_BuildVersioningStyle="None.None.Increment.TimeStamp" />
    </VisualStudio>
  </ProjectExtensions>
</Project>
解决方案
  • MSBuild版本兼容性问题:项目使用ToolsVersion="12.0"(对应VS2013),但PackageReference是VS2017/MSBuild 15.0之后才完全支持的特性。本地可能自动调用了更高版本MSBuild兼容,但Jenkins服务器的MSBuild 12.0无法正确解析PackageReference依赖。解决:在Jenkins服务器安装MSBuild 15.0及以上版本,脚本中指定该版本路径执行构建。
  • 添加显式NuGet还原步骤:旧版MSBuild可能不会自动在构建前完成完整依赖还原。在批处理脚本中先执行:
    nuget restore myProject.csproj
    
    再运行MSBuild构建命令,确保依赖解析完成。
  • 检查.NET Framework开发包:Jenkins服务器需安装.NET Framework 4.7.2开发包,仅安装运行时可能导致编译时无法找到包的程序集。
  • 清理工作空间:旧构建残留的obj、bin文件夹可能干扰依赖解析。构建前删除这些文件夹,从全新状态开始构建。
  • 统一PackageReference的targetFramework格式:项目中targetFramework用了小写net472,部分旧版MSBuild对大小写敏感,尝试改为标准格式v4.7.2,避免解析异常。

内容的提问来源于stack exchange,提问作者Schnabelschweinchen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.03 17:14:51