开发者

msbuild custom task

开发者 https://www.devze.com 2022-12-22 15:12 出处:网络
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 an

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)开发者_如何学C" OutputPath="Scripts">
        <Output TaskParameter="CompressedFiles" ItemName="CompressedFiles" />
    </JsCompress>
    <ItemGroup>
        <Content Include="@(CompressedFiles)" />
    </ItemGroup>
</Target>


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!


Change the file properties. Check the Build Action and Copy to Output Directory properties for those files.

0

精彩评论

暂无评论...
验证码 换一张
取 消