http://www.kby.org/hebrew/torat-yavneh/view.asp?id=4005The Mishna in Sotah 44b mentions groups of people who are told to return home from the battlefield. It concludes, "This is only for a war of 'reshut,' but for a war of 'mitzvah' everyone goes out, even a bridegroom from his chamber and a bride from her chupah." R. Yehuda similarly distinguishes between a war of "mitzvah" and "chova." What is the difference between Tana Kamma and R. Yehuda?
R. Yochanan equates the "reshut" of Rabanan with the "mitzvah" of R. Yehuda, and the "mitzvah" of Rabanan with the "chova" of R. Yehuda. In other words, they both draw a similar distinction and there is only minimal difference between them. Both agree that the wars of Yehoshua to conquer Eretz Yisrael were certainly "chova," whereas the wars of David for expansion (such as Syria) were certainly "reshut." They argue about defensive wars, to weaken the enemy so that Jews should not be attacked. According to Rabanan this is also only "reshut," whereas according to R. Yehuda this is also a war of "mitzvah." The Gemara concludes that the practical ramification of this categorization is for the issue of "osek bemitzvah patur min hamitzvah," whether the soldier in such a war is exempt from other mitzvot at that time. Thus, according to Rabanan, there are only two levels -- "chova/mitzvah" (where everyone goes out) and "reshut" (where there is not even the principle of osek bemitzvah.), whereas according to R. Yehuda there is a third -- "mitzvah" (where not everyone goes out, but has rule of osek bemitzvah).
The Rambam writes (Melachim 5:1) that a king must first fight wars of mitzvah, such as to capture Eretz Yisrael, eradicate Amalek, and defend Israel from their enemies who attack them, and only later can he embark on wars of expansion and glory. This Rambam poses a problem; whom does he rule like? On the one hand, he includes the defense of Israel from their enemies as "mitzvah," which seems to follow R. Yehuda. The Knesset Hagedolah understands this same way. [Based on this, he questions the Lechem Mishneh, who asked why the Rambam omitted the case of defensive wars to weaken the enemy, which is not a mitzvah according to Rabanan? The Knesset Hagedolah understands that the Rambam did quote wars to defend Israel!] On the other hand, the implication of the Gemara is that even R. Yehuda only considered it a mitzvah as far as the issue of "osek bemitzvah," but not as far as not returning from the battlefield. So how could the Rambam include defensive wars together with those to capture Eretz Yisrael?
The Chazon Ish answers that there are two kinds of defensive wars. One is when Israel is already attacked. This the Rambam defines as "help of Israel from the enemy who attacks them," and is certainly a war of "chova" in which everyone must participate. The Gemara, however, is talking about a preemptive war to prevent the enemy from launching an attack. This understanding of the Rambam is not like the Knesset Hagedolah. Rather, the Rambam rules like Rabanan, and the Lechem Mishneh's question is in place, why did the Rambam not explicitly mention a preemptive war?
The Meiri writes that the distinction between Rabanan and R. Yehuda is when we hear that the non-Jews are preparing to attack. This is the same as the Rambam. The Rambam in his Peirush Hamishnayot includes in chova only wars against the seven nations of Canaan and against Amalek. He explains there that the dispute between R. Yehuda and Rabanan is about wars to, "Kill those who are fighting them and to weaken them [based on Rav Kapach translation] so that they will not kill Jews and will not fight in their land."
Where is the source of the Rambam? Why do Rabanan consider preventative wars as only reshut? After all, there is an obligation to save Jews from danger, as the Rambam writes, "Anyone who can save [a fellow Jew] and does not save [him], violates 'You shall not stand aside while your fellow's blood is shed.'" (Hil. Rotzeach 1:14) There is a positive mitzvah to save a fellow Jew (derived from hashavat aveidah), and one who does not do so also violates a lav!
We have to ask, how is one allowed to endanger himself in battle? The Kessef Mishneh writes (based on the Talmud Yerushalmi) that to save a friend from death one is even required to endanger himself somewhat. However, the Rambam and Rif do not quote this Yerushalmi, and even if there is only risk of loss of limb one does not have to endanger himself to save another, like any other mitzvah that one is not obligated in a situation of pikuach nefesh. The Meshech Chochma comments on G-d's statement to Moshe, "Go, return to Egypt, for all the people who seek your life have died" (Shemot 4:19) -- that otherwise Moshe would not have had to endanger himself.
The Minchat Chinuch similarly challenges the Chinuch, who writes that there is no obligation to kill an Amalekite if it is dangerous. Since, by definition, the Torah required war, we cannot apply this logic of potential danger! The answer to this is based on the Brisker Rav's comment on the Haftorah of Devorah, who praises Zevulun and Naftali for having endangered themselves. Why only they? The Brisker Rav explains that the Torah defined annihilation of Amalek as war, and therefore there is no idea of Pikuach Nefesh (even for a war of reshut one is obligated to endanger himself), whereas the Chinuch was talking about an individual Amalekite. Although there is mitzvah to kill him, it is not considered war, so there is no need to endanger oneself to perform this mitzvah. Along the same lines, Devorah only commanded Barak to take men from Zevulun and Naftali, and therefore they were obligated in battle, despite the danger, whereas the other tribes were not obligated other than to help save their fellow Jews.
We can now understand why Rabanan defined going out to a preemptive war to save Jews as a war of reshut, since currently it is not defined as a state of war. It is only an obligation based on the mitzvah of saving Jews, and one is not obligated to endanger himself for this, nor do a chatan and kallah go out to battle. R. Yehuda, however, holds that since saving Jews is mitzvah, and one way of saving them is by going to war -- this war is also considered a war of mitzvah.
Thus, according to Rabanan, if there is a state of war we must go out and even endanger ourselves, but if there is no war and we initiate one -- there is no obligation to endanger ourselves, just an obligation to help other Jews. Thus, it is clear that if goyim attack and there is a state of war, everyone must go out. So why does the Gemara never mention this case? Because this is obviously war, and the Mishna only needs to teach about wars that we initiate, which ones are defined as war because Torah commanded so.
With this can answer question of the Lechem Mishneh. The Chazon Ish, in fact, finds the Rambam contradictory. He first talks about wars to help Israel from their enemy -- where they are already attacked, and contrasts this with wars for expansion, leaving out the middle case of a preemptive war. The Be'er Miryam explains that this is, indeed, a middle case, since the king has an obligation to go out to this kind of war also. The Rambam writes in Hil. Melachim that the king must go out to war for the benefit of Israel and must look after their welfare. Thus, in Hil. Melachim, the Rambam discusses two practical ramifications of being a war of mitzvah: 1) The king must first go out to fight a war of mitzvah (not like David with Syria). 2) The king does not need the approval of Sanhedrin (as mentioned in first Mishna in Sanhedrin) for a war of mitzvah. Thus, if the king sees a potentially dangerous situation he can go out to it first, and does not need Sanhedrin's approval. However, for the people such a war is not a mitzvah (other than from the perspective of a mitzvah to help a fellow Jew) unless they are already attacked, but not if initiated by the king. Therefore, the Rambam omitted it, and alluded to it by writing helping Israel from enemies, implying that lesser case is in-between.
With this we can understand why the Gemara only mentioned "nafka mina" of osek bemitzvah and not whether the king needs authorization, since even according to Rabanan he does not need permission.
Rav Kook writes that every government of Israel has the status of king, and has an obligation to ensure the safety of the people.