Search code examples
asp.net-mvcvisual-studiomsbuildcompass

MSBuild Publish and Compass Generated Sprites


I'm using compass to generate stylesheets and image sprites for my C# MVC .NET project. Mostly this is great and everything works seamlessly. However, I'd like to be able to use the MSBuild "Publish" functionality as part of my automated build. The problem is that compass generated sprites change names all the time, and so I get errors like this one when I try to build:

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.Publishing.targets(2972,5): error : Copying file images\icons-s88f86b4a16.png to obj\Release\Package\PackageTmp\images\icons-s88f86b4a16.png failed. Could not find file 'images\icons-s88f86b4a16.png'.

I'm not sure how to work around this. Is there a way to automatically add new images to the csproject and remove old ones? Has anyone run into something similar?


Solution

  • From my personal experience, Web deploy or Publish from Visual Studio will pick up files that are not part of your solution as long as they as they are part of the web application on the file system.

    For Example:

    MVCSite
    -- images/spirtes.png

    If you are publishing from this copy of the MVC Site, the contents of the images folder will be replicated on your web server even if they are not included in the project file.

    ---Edited

    The above solution will work with a website and not a web application. The following will work with a web application.

    Add this to the end of the Publishing profile (Production.pubxml)

      <Target Name="CustomCollectFiles">
        <ItemGroup>
          <_CustomFiles Include="Test\**\*" />
          <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
            <DestinationRelativePath>%(RecursiveDir)%(Filename)%(Extension)   </DestinationRelativePath>
          </FilesForPackagingFromProject>
        </ItemGroup>
      </Target>
      <PropertyGroup>
        <CopyAllFilesToSingleFolderForPackageDependsOn>
          CustomCollectFiles;
          $(CopyAllFilesToSingleFolderForPackageDependsOn);
        </CopyAllFilesToSingleFolderForPackageDependsOn>
    
        <CopyAllFilesToSingleFolderForMsdeployDependsOn>
          CustomCollectFiles;
          $(CopyAllFilesToSingleFolderForPackageDependsOn);
        </CopyAllFilesToSingleFolderForMsdeployDependsOn>
      </PropertyGroup>
    </Project>
    

    Example Build Script

    echo 'Hello, world.' > "%WORKSPACE%\TestMVC\Test\fo1.txt"
    
    "C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe" "%WORKSPACE%\TestMVC.sln" /p:Configuration=Release /p:Platform="Any CPU" /p:PublishProfile=Production
    

    Output from MSBuild

    Copying Test\fo1.txt to obj\Release\Package\PackageTmp\fo1.txt.
    Copying Test\foo.txt to obj\Release\Package\PackageTmp\foo.txt.