From the little I've read, there's a legend that Boddhidarma taught the martial art in the 5th century to monks of the Shaolin monastery, so they could defend themselves from bandits and animals.
There may be some doctrinal justification, for that motive and that behaviour -- I think that the Theravada Vinaya says that if a monk is captured he can "strike a blow, desiring freedom".
There's also an academic essay -- Epigraphy, Buddhist Historiography, and
Fighting Monks: The Case of The Shaolin Monastery -- which says there's epigraphic evidence that monks also participated on the battlefield, I presume with weapons, in aid of this or that claimant to the throne.
Moreover, the events they record are
not mentioned in Buddhist historiography either. The monks that compiled the voluminous section of the Buddhist canon that is devoted to
history were loath to discuss monastic involvement in warfare, for it
contradicted their religion’s prohibition against violence. Buddhist authors preferred to ignore instances, such as those attested at Shaolin,
in which monks resorted to arms. Thus, Shaolin epigraphic sources
shed invaluable light on a topic ignored by Buddhist historiography:
I think that's harder to justify from what I know of Buddhist doctrine.
It was this military and administrative center that the
Shaolin monks conquered, earning them the gratitude of the future
Pei Cui does not allude to a Tang-government request that the
monks confront Wang Shichong. His chronicle suggests that it was
their initiative to attack the Sui rebel. The monks certainly resented
Wang, who had robbed them of their estate. However, strong as their
resentment was, political calculations also contributed to their military action. Pei notes that “monks Zhicao, Huiyang, Tanzong, and the
others examined to which of the contending parties divine grace was
directed.” The Shaolin clerics probably did not debate the respective
spiritual merits of the Tang rulers and Wang Shichong, but rather who
was more likely to win the war. Had they wagered on the wrong party
this would have been detrimental to their monastery. Instead their
choice of the Tang dynasty guaranteed the prosperity of the Shaolin
temple for centuries to come.
Some attempts at doctrinal justification is that the practice develops "energy" (virya), and that it's good to defend the Three Jewels (that is according to an unreferenced sentence on Wikipedia).
There are some related topics on this site (based on other forms of Buddhism), for example