Shaolin kung fu originated as a physical and mental strengthening regimen for the monks, so that they would have the endurance to meditate at length. However, in periods of turmoil, which cropped up every time a Chinese dynasty fell and a new one arose, the Shaolin monks used these practices for self-defense (and at times, even for combat away from the Temple).
The Shaolin Temple and its monks sometimes enjoyed the generous patronage of pious Buddhist emperors and empresses. Many rulers were anti-Buddhist, however, favoring the Confucian system instead. On more than one occasion, the Shaolin monks' fighting prowess was all that ensured their survival in the face of imperial persecution.