views:

1345

answers:

5

I am trying to call msbuild on a .sln with a post build build-event

something like:

xcopy "$(TargetDir)$(TargetName)*" "C:\TEST\" /Y.

Can I do that in just one line??? Basically I want to have something like:

msbuild Solution.sln xcopy "$(TargetDir)$(TargetName)" "C:\TEST\"

How can I do that?

A: 

You can add a post build event in the project being built which will do the copy.

Paul McCann
A: 

No. adding the post build event would be a part of the project file being built. I don't know of a way to add a copy to the end of a command line.

Paul McCann
A: 

Your post build event is part of the namedconfiguration build - so it will be executed as part of the project/config build.

At least that has been my experience running MSBuild from command line.

Maybe I am misunderstanding your question.

Perhaps if you explain what you are trying to do we can come up with a way to get that solved - rather than trying to answer your specific implementation...

Tim
+2  A: 

IMHO you're better off editing the project file directly and add it to the AfterBuild target. Here's an example where we copy assembly DLL, PDB and XML files on a release build:

<Target Name="AfterBuild">
  <CallTarget Targets="CopyFrameworkToTools" 
              Condition="'$(Configuration)' == 'Release'"/>
</Target>
<Target Name="CopyFrameworkToTools">
  <CreateItem Include="$(OutputPath)$(AssemblyName).*">
    <Output ItemName="ReleaseFiles" TaskParameter="Include" />
  </CreateItem>
  <Copy SourceFiles="@(ReleaseFiles)" DestinationFolder="..\Tools\" />
</Target>

You could strip that down to one line using Copy task, but I want to show that integrating into MSBuild gives you more flexibility and control.

Si
+1  A: 

You can't do it this way. There is no way to specify Tasks to execute from the command line. The only thing you can do is specify Targets.

You can either:

  • write small MSBuild project file to the the tasks you need. Drawbacks: you can't reference outputs from other projects in your solution, so you have to explicitly specify any files
  • add a post build event to your project file (through Visual Studio). Drawbacks: You have to modify your project, which might not be always possible. (No, you can't inject that event from the command line) Also, this event will execute only as a part of the regular build.
  • add a separate target to your project file. Same drawbacks as the post-build event. The difference with te post build event is that you can specify that target explicitly to msbuild and it'll execute only its tasks. Drawbacks: same as the post build event.
Franci Penov
"Regular build" what do you mean by this? Any call to msbuild (via VS or cmd line) on a project will execute AfterBuild target. This approach gives you good control.
Si
I wasn't talking about the Afteruild target, but about the post build event. the two are different.
Franci Penov