3

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!

Community
  • 1
  • 1
Sudheer Kumar
  • 311
  • 4
  • 16
  • 1
    Are you sure VS overrides your changes to project files? I have used VS2012 and 2013 with custom targets in all projects and never had issue with VS removing my targets. – seva titov May 12 '14 at 16:34
  • ^ what Seva says: you're doing something wrong if VS modifies your project files like that. Maybe show how exactly you try to implement your custom target in a project file? – stijn May 12 '14 at 19:58
  • I figured out that Visual Studio wiped out your custom changes, if you are not running in the Admin mode. After I switched over to Admin mode, my changes are now preserved. Still I wish had a solution w/o modifying each individual project in the solution. – Sudheer Kumar May 12 '14 at 21:15

1 Answers1

1

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>
Sudheer Kumar
  • 311
  • 4
  • 16