Free software licences are not an option. Forbidding specific use of your software goes against the intent of the commonly accepted free software licences — you would run afoul of freedom 0:
> The freedom to run the program as you wish, for any purpose¹
There probably exist other open source (but not free software) licences that do limit the audience, but you probably won't be able to enforce them, and they tend not to be taken seriously. You would on the other hand frustrate users outside of the military who want to legally use your software in ways normally permitted by free software licences — e.g., by reusing parts of it, forking it, combining it with other free software, etc.
Lastly, the absolute sure-fire way of preventing any military use of your software is not releasing it at all.
I don't think it's worth the hassle to add such a clause. If the point is not to actually prevent its use, but to make a statement, then make a statement on the project's homepage and in the documentation. It will have the same effect without resorting to weird software licences, and you can choose a well-known free software or merely open source licence.
1: https://en.wikipedia.org/wiki/The_Free_Software_Definition