The sentence is part of a longer hadith and it is taken out of its context. As a result, the intended meaning is completely lost or worse, it is almost reversed. This is a perfect example of the disinformation which can be seen regarding some of the verses of Qur’an as well.
The full context as conveyed by Muslim scholars (Bukhari, Jihad 156, 22, 32,112, Hope 8; Muslim, Jihad 20; Ebu Davud, Jihad 98) is as follows:
In one of the days the Prophet encountered the enemy, he waited until the sun descended. Then he stood up and said:
“O people, do not hope to come to face to face with the enemy! Ask for prosperity from Allah. But in case you come to face to face with the enemy, be patient; know that, The Paradise is under the shade of swords.
Then he completed his words:
“O Rabb (Lord); revealing the Book, moving the clouds, defeating the ancient clans! Defeat this clan as well and help us against them”
Taking the whole context into account, clearly, Prophet Muhammad (pbuh) was hoping to not be in a war and suggested the same to his companions. Moreover, he preached his companions to pray for prosperity. In case war would become inevitable and the end could be death, he reminded that, although it would not be desired, it would also be a big sacrifice which would open the doors to paradise; hence something not to be worried about.
We will supply more information about the battles took place during Prophet’s time inshAllah. Then it will be very clear that Muslims were involved in wars either to defend themselves or they were called for help by the people crushed under tyrannical administrations.