Creating Visual Studio projects that only contain static files
Posted
by Eilon
on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by Eilon
Published on Thu, 27 May 2010 20:00:00 GMT
Indexed on
2010/05/27
20:12 UTC
Read the original article
Hit count: 598
Have you ever wanted to create a Visual Studio project that only contained static files and didn’t contain any code?
While working on ASP.NET MVC we had a need for exactly this type of project. Most of the projects in the ASP.NET MVC solution contain code, such as managed code (C#), unit test libraries (C#), and Script# code for generating our JavaScript code.
However, one of the projects, MvcFuturesFiles, contains no code at all. It only contains static files that get copied to the build output folder:
As you may well know, adding static files to an existing Visual Studio project is easy. Just add the file to the project and in the property grid set its Build Action to “Content” and the Copy to Output Directory to “Copy if newer.” This works great if you have just a few static files that go along with other code that gets compiled into an executable (EXE, DLL, etc.).
But this solution does not work well if the projects only contains static files and has no compiled code. If you create a new project in Visual Studio and add static files to it you’ll still get an EXE or DLL copied to the output folder, despite not having any actual code. We wanted to avoid having a teeny little DLL generated in the output folder.
In ASP.NET MVC 2 we came up with a simple solution to this problem. We started out with a regular C# Class Library project but then edited the project file to alter how it gets built. The critical part to get this to work is to define the MSBuild targets for Build, Clean, and Rebuild to perform custom tasks instead of running the compiler. The Build, Clean, and Rebuild targets are the three main targets that Visual Studio requires in every project so that the normal UI functions properly. If they are not defined then running certain commands in Visual Studio’s Build menu will cause errors.
Once you create the class library projects there are a few easy steps to change it into a static file project:
The first step in editing the csproj file is to remove the reference to the Microsoft.CSharp.targets file because the project doesn’t contain any C# code:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
The second step is to define the new Build, Clean, and Rebuild targets to delete and then copy the content files:
<Target Name="Build"> <Copy SourceFiles="@(Content)" DestinationFiles="@(Content->'$(OutputPath)%(RelativeDir)%(Filename)%(Extension)')" /> </Target> <Target Name="Clean"> <Exec Command="rd /s /q $(OutputPath)" Condition="Exists($(OutputPath))" /> </Target> <Target Name="Rebuild" DependsOnTargets="Clean;Build"> </Target>
The third and last step is to add all the files to the project as normal Content files (as you would do in any project type).
To see how we did this in the ASP.NET MVC 2 project you can download the source code and inspect the MvcFutureFules.csproj project file.
If you’re working on a project that contains many static files I hope this solution helps you out!
© ASP.net Weblogs or respective owner