How do I use msbuild in Visual Studio's post build event?

11.9k views Asked by At

I'm trying to configure the YUICompressor.NET in my Visual Studio project.

As I've understood, I have to create a .proj file and add it to my solution. After that, I need to create a post-build event that will build this .proj file and I'll get my desired output (minified js/css files).

So, I have:

visual studio solution explorer

This .proj contains:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/MsBuild/2003">

  <UsingTask TaskName="CssCompressorTask" AssemblyFile="\..\packages\YUICompressor.NET.MSBuild.2.7.0.0\lib\NET20\Yahoo.Yui.Compressor.Build.MsBuild.dll" />
  <UsingTask TaskName="JavaScriptCompressorTask" AssemblyFile="\..\packages\YUICompressor.NET.MSBuild.2.7.0.0\lib\NET20\Yahoo.Yui.Compressor.Build.MsBuild.dll" />

  <Target Name="Minify">

    <ItemGroup>
      <!-- Single files, listed in order of dependency -->
      <CssFiles Include="Content\*.css"/>
      <JavaScriptFiles Include="Scripts\*.js"/>      
    </ItemGroup>

    <CssCompressorTask
          SourceFiles="@(CssFiles)"
          OutputFile="Content\min.css"
       />

    <JavaScriptCompressorTask
          SourceFiles="@(JavaScriptFiles)"
          OutputFile="Scripts\min.js"
       />

  </Target>
</Project>

I'm trying to build it as the following:

enter image description here

I'm getting the following error:

The command "msbuild C:\Users\Me\Desktop\MvcApplicationExample\MvcApplicationExample\YuiCompressorMsBuild.proj" exited with code 9009.

This error suggests that "msbuild" is not a valid command. So, how should I build this type of project? (I've followed this tutorial: youtube)

Thanks for any help.

3

There are 3 answers

3
vfabre On BEST ANSWER

As you said maybe the visual studio cannot find the MSBuild command, try with the following command instead

"$(MSBuildBinPath)\msbuild.exe"

That uses the complete path to msbuild.

Update (For futures references)

As the comment by Steve Medley, you should not forget the encapsulating quotes.

2
frasnian On

Well, one thing that might be a problem is that you want it as a Post-build event but it's set for the Pre-build command line.

0
Steve Medley On

vfabre is correct to use $(MSBuildBinPath)\msbuild.exe but missing one thing. you should encapsulate that in quotes as there will 99.99% of the time be a space in the file path to msbuild