The Gemara in Bava Basra 7b discusses the need for building walls around a settlement. Since walls are for communal protection, all residents have to share in the cost of erecting them. However, the Gemara rules that Torah scholars are exempt from this expense, since they are protected by virtue of the Torah they learn.
Does this apply in a time of war?
R' Zevin in a famous essay says no.
When actual lives are at stake, may we rely on miracles? In 1929 at Hebron... didn't young students of the yeshiva, whose holiness shone like stars in the sky, fall before the malicious enemy? Please, did these martyrs need protection or not?... If you understand that the scholars don't need protection in relatively peaceful times and are exempt from building the protective walls, what consequence has this when compared to a life-and-death struggle, a war which is a mitzvah and in which all are obligated? The defense authorities ordered everyone to cover all windows as protection against shattering glass in case of an air raid. Would anyone think that some rabbis will not do so, claiming, "Rabbis do not need protection?" ...Why did rabbis leave areas under enemy fire along with the rest of the general population? Why did they not rely on this maxim?
R' Aharon Lichtenstein makes a similar statement:
It may be stated... that such a claim (that since rabbis "don't need protection" they should be exempt from military service) raises a very serious moral issue. Can anyone whose life is not otherwise patterned after this degree of trust and bitahon argues for exemption on this ground? Is it possible to worry about one's economic future - in evident disregard of Rabbi Eliezer's statement that "whoever has bread in his basket and says 'What shall I eat tomorrow?' is but of little faith" - and yet not enter the army because one is presumably safe without it?