关于asp.net mvc:如何解决MSBuild引发错误MSB3030:由于找不到文件” Scripts \\ dist \\ main.bundle.js”,因此无法将其复制

Azure Pipeline项目具有两个Build Agents

  • 建立Angular项目。
  • 成功构建,并将资产,chunk.js文件和bundle.js文件发布为工件

  • 生成ASP.net Web应用程序。
  • 我从以前的构建代理下载工件,并将其提取到$(build.stagingDirectory)/ Scripts / dist目录中,因为它们将由ASP.net项目使用。

    我的提取文件任务ymal为

    1
    2
    3
    4
    5
    6
    steps:
    - task: ExtractFiles@1
      displayName: 'Extract files bundle.zip'
      inputs:
        archiveFilePatterns: '$(Pipeline.Workspace)/dist/$(Build.BuildId)_bundle.zip'
        destinationFolder: '$(build.sourcesDirectory)/Scripts/dist'

    MSBuild构建ASP.net项目时,无法找到##[error]C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\MSBuild\\Microsoft\\VisualStudio\\v15.0\\WebApplications\\Microsoft.WebApplication.targets(182,5): Error MSB3030: Could not copy the file"Scripts\\dist\\main.bundle.js" because it was not found.

    MS Build yaml是:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    steps:
    - task: MSBuild@1
      displayName: 'Build solution NMG.Portal.MVCForAngular'
      inputs:
        solution: NMG.WebApp.MVCForAngular/NMG.WebApp.MVCForAngular.csproj
        msbuildArguments: '/p:ProjectFile=$(build.sourcesDirectory)\
    MG.WebApp.MVCForAngular\
    MG.WebApp.MVCForAngular.csproj /p:DeployOnBuild=true /p:SkipInvalidConfigurations=true /p:OutDir="$(build.stagingDirectory)\"'
        clean: false
        logProjectEvents: true
        createLogFile: true

    我的ASP.NET BundleConfig.cs文件具有以下代码。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    public class BundleConfig
        {
            // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
            public static void RegisterBundles(BundleCollection bundles)
            {
                bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                           "~/Scripts/jquery-{version}.js"));
                // Use the development version of Modernizr to develop with and learn from. Then, when you're
                // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
                bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                           "~/Scripts/modernizr-*"));

                bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                         "~/Scripts/bootstrap.js",
                         "~/Scripts/respond.js"));

                bundles.Add(new StyleBundle("~/Content/css").Include(
                         "~/Content/bootstrap.css",
                         "~/Content/site.css"));

                bundles.Add(new ScriptBundle("~/bundles/nmg").Include(
                   "~/Scripts/dist/inline*",
                   "~/Scripts/dist/polyfills*",
                   "~/Scripts/dist/scripts*",
                   "~/Scripts/dist/vendor*",
                   "~/Scripts/dist/main*"));

                bundles.Add(new StyleBundle("~/Content/nmg").Include(
                    "~/Scripts/dist/styles*"));
            }
        }

    从逻辑上讲这应该起作用。

    我想知道这里缺少什么?为什么MSBuild无法找到我在Scripts / dist中提取的捆绑软件。

    复制到Scripts / dist文件夹的main.bundle.js是main.7515c53aa0f2ef625908.bundle.js,但MSBuild不会考虑。


    实际的问题是,当MSBuild创建发布文件时,它会根据构建定义复制文件。我将.csproj文件编辑为以下文件,该文件复制了相关文件。

    1
    2
    3
    4
    <Content Include="assets\\i18n\\**" />    
    <Content Include="Scripts\\dist\\*.bundle.js" />
    <Content Include="Scripts\\dist\\*.bundle.css" />
    <Content Include="*.chunk.js" />

    之所以可行,是因为在进行MSBuild任务之前,我确实将angular文件复制到了相关的文件夹中。


    同意Thangadurai,这可能是与路径相关的问题。

    检查$(build.sourcesDirectory)/Scripts/dist$(build.sourcesDirectory)通常代表解决方案目录。所以现在您的文件结构是这样的:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    `$(build.sourcesDirectory)`(sln directory)
        |
        Scripts
        |    |
        |    ----dist
        |           |
        |            ----main.xxx.bundle.js
        |      
        xx.sln
        |
        NMG.WebApp.MVCForAngular
             |
             NMG.WebApp.MVCForAngular.csproj
             |
             Scripts

    提取的main.xx.js放在解决方案目录中的Script文件夹中,但是,当msbuild + NMG.WebApp.MVCForAngular.csproj时,msbuild将在Project目录中搜索Script文件夹。

    此外:我建议您检查NMG.WebApp.MVCForAngular.csproj的内容中是否存在对Scripts\\dist\\main.bundle.js的引用,如果存在,还需要确保提取的文件名与项目文件中的引用完全匹配。

    Msbuild不会将main.7515c53aa0f2ef625908.bundle.jsmain.bundle.js视为同一对象,因此,如果解压缩文件的名称与项目文件中的引用不匹配,则也会出现该错误。