Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

msbuild custom task

I have a custom MSBuild task that takes in a set of JavaScript files, minifies them, and outputs them, with the extension .min.js. When I do a normal build through Visual Studio, it works perfectly and the .min.js files are output to the same directory as the original files. When I try to deploy using the Publish feature in Visual Studio, only the original .js files make it to the publish directory.... How can I get the output of my task to be counted as "content" so that it will end up in the published folder?

EDIT: I was able to figure it out by adding the Output tag inside my task and then creating an ItemGroup around that:

<Target Name="AfterBuild">      
    <ItemGroup>
        <JavaScriptFiles Include="Scripts\*.js" Exclude="Scripts\*.min.js" />
    </ItemGroup>
    <JsCompress Files="@(JavaScriptFiles)" OutputPath="Scripts">
        <Output TaskParameter="CompressedFiles" ItemName="CompressedFiles" />
    </JsCompress>
    <ItemGroup>
        <Content Include="@(CompressedFiles)" />
    </ItemGroup>
</Target>
like image 532
Shane Avatar asked Nov 06 '22 15:11

Shane


1 Answers

Build and Publish are separate targets. Add a target to your project, abstract your minification to its own target, then make the AfterBuild and Publish target depend on the minification target. Something like this:

  <Target Name="AfterBuild" DependsOnTargets="Build;Minify">
  </Target>
  <Target Name="Publish" DependsOnTargets="Build;Minify">
  </Target>
  <Target Name="Minify" DependsOnTargets="Build">
    <ItemGroup>
      <JavaScriptFiles Include="Scripts\*.js" Exclude="Scripts\*.min.js" />
    </ItemGroup>
    <JsCompress Files="@(JavaScriptFiles)" OutputPath="Scripts">
      <Output TaskParameter="CompressedFiles" ItemName="CompressedFiles" />
    </JsCompress>
    <ItemGroup>
      <Content Include="@(CompressedFiles)" />
    </ItemGroup>
  </Target>

This snippet, of course, means you have to have a build target, which may or not be the case. For that reason you may need to modify this. Hope this helps!

like image 117
Audie Avatar answered Nov 15 '22 14:11

Audie