自定义MSBuild目标不包括目录中的文件

时间:2016-10-23 18:57:48

标签: asp.net visual-studio-2015 asp.net-mvc-5 msbuild

enter image description here

我添加了一个自定义的msbuild目标来包含来自某个目录的所有文件,但是在进行构建后,它们仍未包含在内。

  <Target Name="BeforeBuild">
    <ItemGroup>
      <!--<Content Include="Content\lib\**\*.js" />
      <Content Include="Content\lib\**\*.css" />-->
      <Content Include="Content\lib\**" />
    </ItemGroup>
  </Target>

背景

(尝试使用bower获取组件,然后将它们包含在项目中,而无需手动添加它们)

[Asp.net 4.5.2,mvc 5,visual studio 2015(update 3)]

2 个答案:

答案 0 :(得分:2)

首先创建一个自定义任务,如下所示

<Target Name="CustomCollectAngularFiles">
    <ItemGroup>
      <WebFiles Include="Content\lib\**\*" />
      <FilesForPackagingFromProject Include="%(WebFiles.Identity)">
        <DestinationRelativePath>%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
      </FilesForPackagingFromProject>
    </ItemGroup>
 </Target>

然后在csproj文件中提供的<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />之后添加一个属性组,它将如下所示。这里的关键是 CopyAllFilesToSingleFolderForMsdeployDependsOn

  <PropertyGroup>
    <CopyAllFilesToSingleFolderForMsdeployDependsOn>
CustomCollectAngularFiles
    </CopyAllFilesToSingleFolderForMsdeployDependsOn>
  </PropertyGroup>

您应该能够看到文件被复制到构建中。

有关详细信息,请参阅https://blog.samstephens.co.nz/2010/10/18/msbuild-including-extra-files-multiple-builds/

答案 1 :(得分:0)

根据你的描述,似乎你想通过使用msbuild将文件包含到项目中,据我所知,如果是这样的话,我认为你需要使用这样的自定义构建任务:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
  <Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" />
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />

  <UsingTask TaskName="Hello" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
    <ParameterGroup />
    <Task>
      <Reference Include="System.Xml" />
      <Reference Include="Microsoft.Build" />
      <Using Namespace="Microsoft.Build" />
      <Using Namespace="Microsoft.Build.Evaluation" />
      <Using Namespace="Microsoft.Build.Utilities" />
      <Code Type="Fragment" Language="cs">
          <![CDATA[
        string appPath = @"C:\Projects\MsBuild\App2\App2\App2.csproj";
        var project = new Project(appPath);
        var loc = "test";
        project.AddItem("Folder", loc);
        project.Save();
        Log.LogMessage("Hello, world!", MessageImportance.High);
         ]]>
        </Code>
    </Task>
  </UsingTask>

  ……
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
       Other similar extension points exist, see Microsoft.Common.targets.  -->
  <Target Name="BeforeBuild">
    <Hello />
  </Target>
  <!--<Target Name="AfterBuild">
    <Message Text="About to run MsBuildTask" Importance="high" />
    <App2.MsBuildTask/>
  </Target>
  -->
</Project>