Sample MSBuild.xml File

** This documentation is out of date and being updated - please download the latest source code and look at the Example web project there **


This is an example MSBuild.xml file, which contains all the settings (both required and optional) to use msbuild.exe to compress and/or minify any casscading style sheets and/or javascript.

IMPORTANT NOTE: Please take careful note of the path locations for the following :-

  • AssemblyFile
  • JavaScriptFiles
  • JavaScriptOutputFile (this can be defined in the xml file OR via the msbuild command line argument)
  • CssFiles
  • CssOutputFile (this can be defined in the xml file OR via the msbuild command line argument)



<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/MsBuild/2003">
    <UsingTask
        TaskName="CompressorTask"
        AssemblyFile="<the path to the dll...>\Yahoo.Yui.Compressor.Build.MsBuild.dlll" />

    <!-- Define the output locations. These can be set via the msbuild command line using
         /p:SourceLocation="$(ProjectDir)"
         /p:CssOutputFile=$(ProjectDir)../whatever...
         /p:JavaScriptOutputFile=$(ProjectDir)../whatever...
         
         If they are not supplied or are empty, then we the value whatever is supplied, below.
    -->
    <PropertyGroup>
        <CssOutputFile Condition=" '$(CssOutputFile)'=='' ">SylesSheetFinal.css</CssOutputFile>
        <JavaScriptOutputFile Condition=" '$(JavaScriptOutputFile)'=='' ">JavaScriptFinal.js</JavaScriptOutputFile>
    </PropertyGroup>
    
    
    <Target Name="MyTaskTarget">
        <!--
            ItemGroup\CssFiles or ItemGroup\JavaScriptFiles: add zero to many files you wish to include in this compression task.
                                                             Don't forget, you can use the wildcard (eg. *.css, *.js) if you feel up to it.
                                                             Finally, at least one item is required - either a css file or a js file.
                                                             
            CssFiles/JavaScriptFiles data format: Please do not touch this.
            DeleteCssFiles: [Optional] True | Yes | Yeah | Yep | True | FoSho | Fo Sho. Default is False. Anything else is False. (eg. blah = false, xxxx111 = false, etc)
            CssCompressionType: YuiStockCompression | MichaelAshsRegexEnhancements | HaveMyCakeAndEatIt or BestOfBothWorlds or Hybrid; Default is YuiStockCompression.
            ObfuscateJavaScript: [Optional] refer to DeleteCssFiles, above.
            PreserveAllSemicolons: [Optional] refer to DeleteCssFiles, above.
            DisableOptimizations: [Optional] refer to DeleteCssFiles, above.
            EncodingType: [Optional] ASCII, BigEndianUnicode, Unicode, UTF32, UTF7, UTF8, Default. Default is 'Default'.
            DeleteJavaScriptFiles: [Optional] refer to DeleteCssFiles, above.
            LineBreakPosition: [Optional] the position where a line feed is appened when the next semicolon is reached. Default is -1 (never add a line break).
                               0 (zero) means add a line break after every semicolon. (This might help with debugging troublesome files).          
            LoggingType: None | ALittleBit | HardcoreBringItOn;  Hardcore also lists javascript verbose warnings, if there are any (and there usually is :P ).
            ThreadCulture: [Optional] the culture you want the thread to run under. Default is 'en-gb'.
            IsEvalIgnored: [Optional] compress any functions that contain 'eval'. Default is False, which means a function that contains
                           'eval' will NOT be compressed. It's deemed risky to compress a function containing 'eval'. That said,
                           if the usages are deemed safe this check can be disabled by setting this value to True.
            PreserveCssComments: [Optional] refer to DeleteCssFiles, above.
        -->
        <ItemGroup>
            <!-- Single files, listed in order of dependency -->
            <CssFiles Include="$(SourceLocation)StylesheetSample1.css"/>
            <CssFiles Include="$(SourceLocation)StylesheetSample2.css"/>
            <CssFiles Include="$(SourceLocation)StylesheetSample3.css"/>
            <CssFiles Include="$(SourceLocation)StylesheetSample4.css"/>
            
            <JavaScriptFiles Include=$(SourceLocation)"jquery-1.3.2.js"/>
            
            <!-- All the files. They will be handled (I assume) in alphabetically. -->
            <!-- <CssFiles Include="$(SourceLocation)*.css" />
            <JavaScriptFiles Include="$(SourceLocation)*.js" />
            -->
            
        </ItemGroup>
        <CompressorTask
            CssFiles="@(CssFiles)"
            DeleteCssFiles="false"
            CssOutputFile="$(CssOutputFile)"
            CssCompressionType="YuiStockCompression"
            JavaScriptFiles="@(JavaScriptFiles)"
            ObfuscateJavaScript="True"
            PreserveAllSemicolons="False"
            DisableOptimizations="Nope"
            EncodingType="Default"
            DeleteJavaScriptFiles="false"
            LineBreakPosition="-1"
            JavaScriptOutputFile="$(JavaScriptOutputFile)"
            LoggingType="ALittleBit"
            ThreadCulture="en-au"
            IsEvalIgnored="false"
            />
    </Target>
</Project>

Last edited Sep 21, 2012 at 6:58 AM by freeranger, version 8

Comments

AngrySpade Sep 24, 2011 at 8:00 AM 
I had a bit of a problem with this when spaces were involved in my solution direction path.
Luckily I was able to figure it out.

http://bit.ly/qwxpiv

daviddesloovere Nov 22, 2010 at 9:38 AM 
To answer James' question, this is to code to 'loop' through all the files in the /css folder and compress them one by one.
(MSBuild Reference: http://msdn.microsoft.com/en-us/library/0k6kkbsd.aspx)

<ItemGroup>
<CssFiles Include="css\*.css" Exclude="**\*.min.css" />
</ItemGroup>
<CompressorTask CssFiles="%(CssFiles.Identity)" CssOutputFile="%(CssFiles.RelativeDir)%(CssFiles.Filename).min.css"
CssCompressionType="YuiStockCompression" PreserveAllSemicolons="False" LoggingType="ALittleBit" />

jamestharpe Jul 15, 2010 at 7:28 PM 
Can we make the output file name dependent on the file being compressed? For example, myfile.js -> myfile-min.js?