Question

I have custom MSBuild Tasks to execute after the : AfterBuild event for each project in a solution. I don't want to modify each Project file as: Visual Studio wipes out all the Custom Changes done to the project file once i modify the project in Visual Studio (VS 2012 Ultimate), say add or remove a file/reference.

I don't want to use "CustomAfterMicrosoftCommonTargets" as mentioned here as there is no way to pass this command line argument while building from Visual Studio :

msbuild.exe app.proj /property:CustomAfterMicrosoftCommonTargets=custom.target

I found a solution here, but I didn't quite get it. Can anybody please elaborate on it or help me figure out a better solution?

Update 5/12/2014: I figured out that Visual Studio doesn't wipe out the custom changes if I am running the Visual Studio in the Administrator mode. I can now think of having a Custom import file that has got the required overrides, but still I have to do this for each project in the solution. If somebody adds a new project, they have to remember to add this customization. I don't like this, but probably I can live with for now.

I tried to use the "CustomAfterMicrosoftCommonTargets" approach, but I was not able to set this property from the Pre-build event of Visual Studio, even running as Administrator didn't help. I was trying to set an environment variable with same name from the Pre-Build event, but I never got the new value while MSBuild executes.

Thanks!

Was it helpful?

Solution

Finally I found an option where in I don't have to edit the individual project file. The idea is to invoke your custom common targets file in the "AfterBuild" event that Visual Studio exposes.

IF "$(BuildingInsideVisualStudio)"=="true" (
$(MSBuildBinPath)\msbuild.exe "$(ProjectDir)CustomMSBuild.targets" /p:Configuration="$(Configuration)"/property:"ProjectUnderCompilation=$(MSBuildThisFileDirectory)$(MSBuildThisFile)"
)

So I am passing the project under compilation as a property and import that project file. If I throw an exception in the custom task, it appears as as Compilation error on the parent project. This worked amazingly and I am able to perform any validations on the project that was passed.

The only downside I see is that I am spawning another MSBuild.exe and I don't see any impact of that in the compilation time as of now.

Please let me know your thoughts on this implementation.

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="MyTarget">
<UsingTask AssemblyFile="$(ProjectDir)\bin\TaskLibrary.dll" TaskName="CheckProjectReferences" />
<PropertyGroup>
    <ProjectUnderCompilation></ProjectUnderCompilation>
</PropertyGroup>
<Target Name="MyTarget">
    <Message Text="Inside MyTarget" Importance="High" />
    <CheckProjectReferences/>
</Target>
<Import Project="$(ProjectUnderCompilation)" />
</Project>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top