title: How to create a MSBuild Target that will only run if necessary
body: <p>I have been experimenting with MSBuild to create custom targets. I am currently attempting to add support for compiling file with the JAWS script compiler. This is what I have so far.</p>
  
  <h1>Scripts.props</h1>
  
  <pre><code>&lt;Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
      &lt;PropertyGroup&gt;
          &lt;DEVDOCS_DIR&gt;$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)..\..\..\..\..\DevDocs'))&lt;/DEVDOCS_DIR&gt;
          &lt;BUILTIN_MASTER&gt;$(DEVDOCS_DIR)\jsd\enu\builtin_master.jsd&lt;/BUILTIN_MASTER&gt;
          &lt;TOOLBOX_DIR&gt;$(DEVDOCS_DIR)\Toolbox&lt;/TOOLBOX_DIR&gt;
          &lt;JAWS_VER&gt;17.0&lt;/JAWS_VER&gt;
          &lt;!-- If perl.exe is not in your path, set the following variable to the full path and file name of perl.exe.
          Optionally you could set the PERL_EXE environment variable. --&gt;
          &lt;PERL_EXE Condition="'$(PERL_EXE)'==''"&gt;perl.exe&lt;/PERL_EXE&gt;
          &lt;!-- If scompile.exe is not in your path, set the following variable to the full path and file name of scompile.exe.
          Optionally you could set the SCOMPILE_EXE environment variable. --&gt;
          &lt;SCOMPILE_EXE Condition="'$(SCOMPILE_EXE)'==''"&gt;scompile.exe&lt;/SCOMPILE_EXE&gt;
      &lt;/PropertyGroup&gt;
  &lt;/Project&gt;
  </code></pre>
  
  <h1>Scripts.targets</h1>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
      &lt;Target Name="GenerateBuiltinJSD"&gt;
          &lt;Exec Command="&amp;quot;$(PERL_EXE)&amp;quot; compilejsd.pl -v Jaws/$(JAWS_VER) -o &amp;quot;$(MSBuildProjectDirectory)\builtin.jsd&amp;quot; &amp;quot;$(BUILTIN_MASTER)&amp;quot;" WorkingDirectory="$(TOOLBOX_DIR)" /&gt;
      &lt;/Target&gt;
      &lt;Target Name="CompileScripts"&gt;
          &lt;Exec Command="&amp;quot;$(SCOMPILE_EXE)&amp;quot; /d &amp;quot;%(ScriptSourceFiles.FullPath)&amp;quot;" Outputs="%(ScriptSourceFiles.RootDir)%(ScriptSourceFiles.Directory)%(ScriptSourceFiles.Filename).jsb" WorkingDirectory="$(MSBuildProjectDirectory)" /&gt;
      &lt;/Target&gt;
      &lt;Target Name="Clean"&gt;
          &lt;ItemGroup&gt;
              &lt;JSBFiles Include="$(MSBuildProjectDirectory)\*.jsb" /&gt;
          &lt;/ItemGroup&gt;
          &lt;Delete Files="@(JSBFiles)" /&gt;
      &lt;/Target&gt;
      &lt;Target Name="Build"&gt;
          &lt;CallTarget Targets="GenerateBuiltinJSD;CompileScripts" /&gt;
      &lt;/Target&gt;
  &lt;/Project&gt;
  </code></pre>
  
  <h1>Scripts.vcxproj</h1>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
    &lt;ItemGroup Label="ProjectConfigurations"&gt;
      &lt;ProjectConfiguration Include="Release|Win32"&gt;
        &lt;Configuration&gt;Release&lt;/Configuration&gt;
        &lt;Platform&gt;Win32&lt;/Platform&gt;
      &lt;/ProjectConfiguration&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;ScriptSourceFiles Include="Default.jss" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;ScriptMessageFiles Include="Default.jsm" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;ScriptHeaderFiles Include="HJConst.jsh" /&gt;
    &lt;/ItemGroup&gt;
    &lt;ItemGroup&gt;
      &lt;ScriptDocumentationFiles Include="default.jsd" /&gt;
    &lt;/ItemGroup&gt;
    &lt;PropertyGroup Label="Globals"&gt;
      &lt;ProjectGuid&gt;{B2C4363D-D228-425D-AB04-38997EA229C0}&lt;/ProjectGuid&gt;
    &lt;/PropertyGroup&gt;
    &lt;Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /&gt;
    &lt;PropertyGroup Label="Configuration"&gt;
      &lt;PlatformToolset&gt;v120&lt;/PlatformToolset&gt;
    &lt;/PropertyGroup&gt;
    &lt;Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /&gt;
    &lt;ImportGroup Label="PropertySheets"&gt;
      &lt;Import Project="Scripts.props" /&gt;
    &lt;/ImportGroup&gt;
    &lt;Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /&gt;
    &lt;Import Project="$(MSBuildProjectDirectory)\Scripts.targets" /&gt;
  &lt;/Project&gt;
  </code></pre>
  
  <p>This works up to a point. The problem is that when I do a build, it rebuilds all the script files, even if the relevant source file has not changed.</p>
  
  <p>My question is how can I modify this so that each file is only built if necessary?</p>