Question

desync-o-tron on Wed, 19 Feb 2020 20:23:17


Howdy! I need to call a different command for build, rebuild & clean in a VS 2017 project.

I was dreaming up something like this code, but I can't find any environment variables that correspond with rebuilding, cleaning & building.

  <ItemDefinitionGroup>
    <PostBuildEvent> <!-- want this to be called on build -->  
      <Command>CALL .\export.bat BUILD $(SOME_ENV_VAR)</Command>
    </PostBuildEvent>
    <PostBuildEvent> <!-- want this to be called on rebuild -->   
      <Command>CALL .\export.bat REBUILD $(SOME_ENV_VAR)</Command>
    </PostBuildEvent>
    <PostBuildEvent> <!-- want this to be called on clean -->  
      <Command>CALL .\export.bat CLEAN $(SOME_ENV_VAR)</Command>
    </PostBuildEvent>
  </ItemDefinitionGroup>
I'd be happy with a solution that does the same thing.

Replies

Dylan Zhu-MSFT on Thu, 20 Feb 2020 07:28:41


Hi desync-o-tron,

Welcome to MSDN forum.

You could create a Directory.Build.targets file into your solution path, then add this sample in it:

<Project>
<ItemDefinitionGroup>
  <PostBuildEvent>
    <Command>SET custompath=$(OutputPath) 
          CALL D:\path.bat
    </Command>
  </PostBuildEvent>
</ItemDefinitionGroup>
  <Target Name="afterrebuild" AfterTargets="ReBuild">
    <Message Text="afterRebuild" />
    <Exec Command="SET custompath=$(OutputPath) 
          CALL D:\path.bat"/>
  </Target>
  <Target Name="afterclean" AfterTargets="Clean">
    <Message Text="afterclean" />
    <Exec Command="SET custompath=$(OutputPath) 
          CALL D:\path.bat"/>
  </Target>

</Project>


Any feedback will be expected.

Best Regards,

Dylan

desync-o-tron on Thu, 20 Feb 2020 20:55:51


Howdy Dylan!

Thank you for your response. However, I am not having any luck with the solution given.
Here are a few things that I am having issues with:

<PostBuildEvent> is called on both build and rebuild, which is a problem, because I need a separate event to be called for both rebuild and build.

This syntax of <Message Text="afterRebuild" /> will give an error in VS2017. It seems that the syntax is now <Message>afterRebuild</Message> I'm not particularly concerned with this syntax change, but -

Nothing runs inside either of the targets you gave me. Also messages are not allowed to be inside targets, you'll get syntax errors.

I've tried putting this code inside both a .targets file and inside the .vcxproj file (for better error checking) with no luck.

Thanks,
Luke


Dylan Zhu-MSFT on Fri, 21 Feb 2020 07:22:52


Hi Luke,

Thank you for feedback.

I tried this on my side, the PstBuildEvent is also called on build and rebuild. I tried to use AfterTargets="build", but it seems that ide can not recognize it. So I report it to product team, please refer more from this link:

https://developercommunity.visualstudio.com/content/problem/925813/directorybuildtargets-cannot-work-when-building-in.html 

About <Message Text= ..>, it could work with VS 2017 on my side. My version: visual studio 2017 enterprise 15.9.19

In addtion, you could use msbuild tool to implement it, and it could work sucessfully on my side. Please follow these steps:

#1, create a "after.[solutionName].sln.targets" file in your solution path

#2, type thses sample:

<Project>
  <Target Name="afterbuild" AfterTargets="Build">
    <Message Text="********************afterbuild********************************" />
    <Exec Command="SET custompath=D:\ 
          CALL C:\Users\Administrator\Desktop\ConsoleApplication1\path.bat"/>
  </Target>
  <Target Name="afterrebuild" AfterTargets="ReBuild">
    <Message Text="***********************************afterRebuild************************************************" />
    <Exec Command="SET custompath=D:\ 
          CALL C:\Users\Administrator\Desktop\ConsoleApplication1\path.bat"/>
  </Target>
  <Target Name="afterclean" AfterTargets="Clean">
    <Message Text="*****************************************afterclean****************************************" />
    <Exec Command="SET custompath=D:\ 
          CALL C:\Users\Administrator\Desktop\ConsoleApplication1\path.bat"/>
  </Target>
</Project>


#3, open Developer Command prompt for VS 2017, and type the msbuild command:

msbuild Name.sln t:build
msbuild Name.sln t:rebuild
msbuild Name.sln t:clean

At last, you could refer the rebuild sample:

Any feedback will be expected.

Best Regards,

Dylan