copyright from: shareddev.blogspot.com
Publishing of WSPs in Visual Studio is very simple and straightforward. But this process includes absolutely everything into WSP (aspx, ascx, css, js, etc), even if we don’t need them. For example, if we have minified versions of javascript files, we definitely don’t need debug versions in production. SharePoint tooling doesn’t really help us when we need to customize building process for our solutions. But luckily we have MsBuild! I’ll come up with simple solution on how it’s possible to exclude some files from Layouts directory. Everything that will be described below has been tested in Visual Studio 2012 and SharePoint 2010.
When we click Publish button from VS menu, it executes the following MSBuild file:
A closer look to this file shows that we can officially override two targets here: BeforeLayout and AfterLayout. We need to do two things - exclude file from WSP and exclude it from manifest.xml. Excluding from WSP is simple enough. According to Microsoft.VisualStudio.SharePoint.targets file there is EnumeratedFiles ItemGroup created before BeforeLayout target, so we can exclude some files in this target like that:
Publishing of WSPs in Visual Studio is very simple and straightforward. But this process includes absolutely everything into WSP (aspx, ascx, css, js, etc), even if we don’t need them. For example, if we have minified versions of javascript files, we definitely don’t need debug versions in production. SharePoint tooling doesn’t really help us when we need to customize building process for our solutions. But luckily we have MsBuild! I’ll come up with simple solution on how it’s possible to exclude some files from Layouts directory. Everything that will be described below has been tested in Visual Studio 2012 and SharePoint 2010.
When we click Publish button from VS menu, it executes the following MSBuild file:
c:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\SharePointTools\Microsoft.VisualStudio.SharePoint.targets
A closer look to this file shows that we can officially override two targets here: BeforeLayout and AfterLayout. We need to do two things - exclude file from WSP and exclude it from manifest.xml. Excluding from WSP is simple enough. According to Microsoft.VisualStudio.SharePoint.targets file there is EnumeratedFiles ItemGroup created before BeforeLayout target, so we can exclude some files in this target like that:
12345678 |
|
12345678910111213141516171819202122232425262728293031323334353637383940414243 |
|
Type="Fragment" Language="cs">
<![CDATA[
try
{
var filesToFind = Files.Select(item => item.GetMetadata("FullPath")).ToArray();
Result = SourceFiles
.Where(file => filesToFind.Contains(file.ItemSpec))
.ToArray();
return true;
}
catch (Exception ex)
{
Log.LogErrorFromException(ex);
return false;
}
]]>
123 |
|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
|
Type="Fragment" Language="cs">
<![CDATA[
try
{
var excludedFiles = Files.Select(item => item.ItemSpec).ToArray();
var manifestPath = ManifestPath.TrimEnd(new[] { '\\' }) + @"\manifest.xml";
var manifestXml = File.ReadAllText(manifestPath);
var manifest = XElement.Parse(manifestXml);
var excludedElements =
manifest.Descendants().Where(element => element.Name.LocalName == "TemplateFile")
.Where(element => excludedFiles.Contains((string) element.Attribute("Location")));
excludedElements.Remove();
manifest.Save(manifestPath);
return true;
}
catch (Exception ex)
{
Log.LogErrorFromException(ex);
return false;
}
]]>
Condition="'$(Configuration)'=='Release'"
condition to both targets, so only Release version of WSP will not contain excluded files, Debug version should stay untouched for development mode of course.Here is the link to the whole build file:
https://gist.github.com/vadimi/5572446
You can just include it in your project, import it through
and modify ScriptsDir, ManifestPath, ExcludedFiles variables according to your project needs.