MSBUILD:构建包,包括额外的文件

时间:2011-11-21 20:50:15

标签: c# .net msbuild msbuild-4.0

过去一周,我在尝试实施位于http://blog.samstephens.co.nz/2010-10-18/msbuild-including-extra-files-multiple-builds/的解决方案时遇到了一些严峻的挑战。

它基于Sayed的实现:http://sedodream.com/CommentView,guid,803d77d7-a220-4cee-a803-f6291cd4ba71.aspx(效果很好),但是我需要来自多个位置的文件而且Sayed的解决方案不能解释这种情况。

虽然理论上Sam的解决方案提供了完全我需要的东西,但我无法构建它(尽管剪切和粘贴他的解决方案并且仅修改路径以反映我的环境)。几天来,我一直在抨击以下错误:

[15:31:30]: [CopyPipelineFiles] C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852, 5): error MSB4018: The "CopyPipelineFiles" task failed unexpectedly.  
System.ArgumentException: Illegal characters in path.  
at System.IO.Path.CheckInvalidPathChars(String path) at System.IO.Path.Combine(String path1, String path2)  
at Microsoft.Web.Publishing.Tasks.CopyPipelineFiles.CopyPipelineFilesToFolder(TaskLoggingHelper log, ITaskItem[] allpipeLineItems, String sourceFolderName, String targetFolderName, ItemMetadataFilter itemMetadataSkipFilter, Boolean fUpdateItemSpec, Boolean deleteItemsMarkAsExcludeTrue, List`1 updatedPipeLineItems, List`1 failedPipeLineItems)  
at Microsoft.Web.Publishing.Tasks.CopyPipelineFiles.Execute()  
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() 
at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask, Boolean& taskResult)    
[15:31:31]: Process exited with code 1  
[15:31:31]: MSBuild output:  
[15:31:31]: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error MSB4018: The "CopyPipelineFiles" task failed unexpectedly. [<PATH_TO>\MYPROJ.csproj]  
[15:31:31]: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error MSB4018: System.ArgumentException: Illegal characters in path. [<PATH_TO>\MYPROJ.csproj]  
[15:31:31]: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error MSB4018: at System.IO.Path.CheckInvalidPathChars(String path) [<PATH_TO>\MYPROJ.csproj]  
[15:31:31]: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error MSB4018: at System.IO.Path.Combine(String path1, String path2) [<PATH_TO>\MYPROJ.csproj]  
[15:31:31]: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error MSB4018: at Microsoft.Web.Publishing.Tasks.CopyPipelineFiles.CopyPipelineFilesToFolder(TaskLoggingHelper log, ITaskItem[] allpipeLineItems, String sourceFolderName, String targetFolderName, ItemMetadataFilter itemMetadataSkipFilter, Boolean fUpdateItemSpec, Boolean deleteItemsMarkAsExcludeTrue, List`1 updatedPipeLineItems, List`1 failedPipeLineItems) [<PATH_TO>\MYPROJ.csproj]  
[15:31:31]: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error MSB4018: at Microsoft.Web.Publishing.Tasks.CopyPipelineFiles.Execute() [<PATH_TO>\MYPROJ.csproj]  
[15:31:31]: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [<PATH_TO>\MYPROJ.csproj]  
[15:31:31]: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask, Boolean& taskResult) [<PATH_TO>\MYPROJ.csproj]  
[15:31:31]: Done Building Project "<PATH_TO>\MYPROJ.csproj" (Package target(s)) -- FAILED.  
[15:31:31]: Done Building Project "C:\buildAgent\work\8aa6ae640d0f858b\main\scripts\MYPROJ.csproj" (BatchCopyPackage target(s)) -- FAILED.  
[15:31:31]: Build FAILED.  
[15:31:31]: "C:\buildAgent\work\8aa6ae640d0f858b\main\scripts\MYPROJ.csproj" (BatchCopyPackage target) (1) ->  
[15:31:31]: "<PATH_TO>\MYPROJ.csproj" (Package target) (2) ->  
[15:31:31]: (CopyAllFilesToSingleFolderForPackage target) ->  
[15:31:31]: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error MSB4018: The "CopyPipelineFiles" task failed unexpectedly. [<PATH_TO>\MYPROJ.csproj]  
[15:31:31]: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error MSB4018: System.ArgumentException: Illegal characters in path. [<PATH_TO>\MYPROJ.csproj]  
[15:31:31]: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error MSB4018: at System.IO.Path.CheckInvalidPathChars(String path) [<PATH_TO>\MYPROJ.csproj]  
[15:31:31]: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error MSB4018: at System.IO.Path.Combine(String path1, String path2) [<PATH_TO>\MYPROJ.csproj]  
[15:31:31]: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error MSB4018: at Microsoft.Web.Publishing.Tasks.CopyPipelineFiles.CopyPipelineFilesToFolder(TaskLoggingHelper log, ITaskItem[] allpipeLineItems, String sourceFolderName, String targetFolderName, ItemMetadataFilter itemMetadataSkipFilter, Boolean fUpdateItemSpec, Boolean deleteItemsMarkAsExcludeTrue, List`1 updatedPipeLineItems, List`1 failedPipeLineItems) [<PATH_TO>\MYPROJ.csproj]  
[15:31:31]: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error MSB4018: at Microsoft.Web.Publishing.Tasks.CopyPipelineFiles.Execute() [<PATH_TO>\MYPROJ.csproj]  
[15:31:31]: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [<PATH_TO>\MYPROJ.csproj]  
[15:31:31]: C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852,5): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask, Boolean& taskResult) [<PATH_TO>\MYPROJ.csproj]  
[15:31:31]: 0 Warning(s)  
[15:31:31]: 1 Error(s)  
[15:31:31]: Time Elapsed 00:00:23.00  

这是我对Sam目标的实现:

<Target Name="DefineCustomFiles">
    <ItemGroup>
      <CustomFilesToInclude Include="$(workingDir)\main\img\**\*">
        <Dir>img</Dir>
      </CustomFilesToInclude>
      <CustomFilesToInclude Include="$(workingDir)\main\Service References\**\*">
        <Dir>ServiceReferences</Dir>
      </CustomFilesToInclude>
    </ItemGroup>
</Target>

<Target Name="CustomCollectFiles">
  <Message Text="Here is a file list: %(CustomFilesToInclude.Identity)" />
    <ItemGroup>
      <FilesForPackagingFromProject Include="@(CustomFilesToInclude)">
        <DestinationRelativePath>
          %(CustomFilesToInclude.Dir)\%(RecursiveDir)%(Filename)%(Extension)
        </DestinationRelativePath>
      </FilesForPackagingFromProject>
    </ItemGroup>
</Target>

我认为它可能与空格(或通配符)有关,所以这就是我尝试过的:

  • 使用“详细”和“诊断”日志记录级别检查日志

  • 混合双引号和单引号的各种形式

  • 注释掉“服务引用”节点。 (包含imgs的路径没有空格)

  • 指向具有绝对路径的单个文件。因此,消除由于包含空格的路径而导致的通配符和错误。我以为这肯定会解决这个问题,但是上面的错误却失败了。

在第3期之后,我完全失败了。它不喜欢传递给它的任何文件。

任何见解都会非常感激。提前谢谢。

以下是我的目标目前的状态以及相应的错误消息:

<Target Name="DefineCustomFiles">
    <ItemGroup>
      <CustomFilesToInclude Include="$(workingDir)\main\img\file.gif">
        <Dir>img</Dir>
      </CustomFilesToInclude>
    </ItemGroup>
</Target>

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets(1852, 5): error MSB4018: The "CopyPipelineFiles" task failed unexpectedly.  
System.ArgumentException: Illegal characters in path.

4 个答案:

答案 0 :(得分:7)

我找到了你的问题,我担心这是我的错。问题是在DestinationRelativePath的{​​{1}}元素内,空格很重要。我在撰写博客文章时添加了空格,以使XML更易于阅读,而没有意识到它会导致代码失败。

所以如果你试试

FilesForPackagingFromProject

有希望解决您的问题。我已更新博客文章以删除有问题的空白。

请注意,正如Scott Stafford在下面的评论中指出的,CopyAllFilesToSingleFolderForPackageDependsOn已在Visual Studio 2012中重命名为CopyAllFilesToSingleFolderForMsdeployDependsOn。

答案 1 :(得分:3)

我遇到了类似这样的问题,其目标类似于Saustrup的回答中提出的建议。

就我而言,问题实际上是我的目标运行得太晚,所以在构建,打包和部署应用程序之后创建文件。这是因为我使用BeforeTargets="Build"来运行我的目标。我在回答一个更相关的问题时更详细地介绍了这个问题(在解决问题后我才发现here

答案 2 :(得分:2)

这适用于Visual Studio 2013 for Web:

<Target Name="BeforeBuild">
  <ItemGroup>
    <Content Include="bin\**" Exclude="**/.git*" />
    <Content Include=".\Global.asax" />
    <Content Include=".\umbraco\**" />
    <Content Include=".\usercontrols\**" />
    <Content Include=".\umbraco_client\**" />
    <Content Include=".\App_Code\**" />
    <Content Include=".\App_Plugins\**" />
    <Content Include=".\App_Data\*-*-*-*-*\**" />
    <Content Include=".\App_Data\packages\**" />
    <Content Include=".\App_Data\access.*" />
    <Content Include=".\Views\**" />
    <Content Include=".\App_Browsers\**" />
    <Content Include=".\uSync\**" />
    <Content Include=".\media\**" />
  </ItemGroup>
</Target>

这个具体的例子来自Umbraco 4项目。

答案 3 :(得分:0)

正如您所指出的那样,MSBuild确实存在空格问题。你有没有尝试添加双斜杠:

"$(workingDir)\\main\\Service References\\**\\*"