JARRRRG
JARRRRG

Reputation: 926

Custom MSBuild target not including files in a directory

enter image description here

I've added a custom msbuild target to include all the files from a certain directory however after doing a build they are still not included.

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

Background

(Trying to use bower to get components then include them in the project without having to manually add them)

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

Upvotes: 0

Views: 1698

Answers (2)

Zhanglong Wu - MSFT
Zhanglong Wu - MSFT

Reputation: 1660

Based on your description, it seems that you want to include files into project by using msbuild, as far as I know, if it is the case, I think you need to use custom build task like this:

<?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>

Upvotes: 0

Karthikeyan VK
Karthikeyan VK

Reputation: 6006

First Create a custom Task like below

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

And then add a property group after <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" /> which is available in your csproj file and it will look like below. Key here is CopyAllFilesToSingleFolderForMsdeployDependsOn

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

You should be able to see the files getting copied to the build.

For more details refer https://blog.samstephens.co.nz/2010/10/18/msbuild-including-extra-files-multiple-builds/

Upvotes: 2

Related Questions