This is true for most militaries, of course, some more than others - see the Hague Invasion Act[0].
[0]: https://en.wikipedia.org/wiki/American_Service-Members'_Prot...