Warfare plays an important role in the fantasy genre. Many of its most iconic works embrace military themes, either as a central plot element or as a disruptive force that's more in the background but still creating obstacles and conflict for the protagonists.
Sometimes the military is front and center, as in Elizabeth Moon's Deed of Paksennarion novels or Django Wexler's Shadow Campaign. Other times battles occur intermittently throughout the series where much of the focus is on something else, as in Robin Hobb's Farseer Trilogy, and other times, war is completely in the background but still influencing the story, as in Carol Berg's Lighthouse Duology.
I've wondered why this is. Many people look at the genre as escapist, and war is one of life's less pleasant realities. Few of us really want to live in its shadow as civilians or soldiers, and painstaking descriptions of military training, tactics, camp life, not to mention indignities such as dysentery, can be quite dull. Conflict, change, and stakes are necessary plot elements in any story, and war can certainly create these, yet other genres of fiction find ways to test their characters and introduce change without a focus on sieges, battles and military strategies or tactics. It's possible to create stories with compelling stakes that have nothing to do with war. This is the norm in many genres
When I put this question to some of my fellow fantasy readers and writers, one explanation was that historical epics, such as Gilgamesh and The Iliad focused heavily on battles and wars. Fantasy as a genre often attempts to recreate or evoke the tropes presented in these traditional tales, which often contained magical or supernatural elements.
The success of Tolkien's work is also a possible explanation. While he didn't dwell on the nitty gritty day-to-day life of soldiers, or even on military tactics, no one can argue that war was an important part of both The Hobbit and Lord of the Rings.
Given Tolkien's influence on the genre, it's hardly surprising that many writers adopted his approach. A focus on enemies who are unilaterally evil and under the control of a dark figure that must be stopped certainly does turn war into something that's more appealing from an escapist perspective.
I can't help but wonder how much of fantasy's sometimes idealistic portrayal of war was shaped by the two world wars in particular. Tolkien fought in WWI and lived through WWII as well. He claimed that his work was not meant to be allegorical, but it's hard not to see parallels between fascism and the minions of Sauron.
And Tolkien aside, those of us who were born and came of age in the decades following WWII had our consciousness shaped by the cultural memory of a war that really was against an implacable evil that could have destroyed civilization as we know it. Few would argue in hindsight that a more pacifistic approach would have been the best one in that particular instance. Could the warlike nature of late twentieth and early twenty-first century fantasy represent a collective desire to escape to a world where moral ambiguity was (in hindsight, at least) less than it is today? This is certainly possible, though I can't think of a way to test, or falsify such a hypothesis.
Another thing to consider is that war stories are often anti war stories at their heart. All Quiet on the Western Front is an iconic example here, but there are plenty of others. Fantasy novels are notably absent from this list, but I don't think fantasy writers always portray war in a positive light. TH White's The Once and Future King is a retelling of Mallory's version of the Arthurian legend, a heroic epic, yet some feel it was inspired by his own pacifism and misgivings about nationalism as he came to terms with a war (WWII) where the enemy clearly had to be stopped.
Other fantasy writers like Moorcock, and more recently Abercrombie, have also sought to deconstruct some of the tropes popularized by Tolkien and other fantasy writers from the earlier part of the 20th century. Moorcock's Elric saga, and the Abercrombie's novels are very violent and war-centered, yet neither glorifies war or presents it as something that improves the world.
And there are plenty of other explanations why war is so commonly portrayed in fantasy novels. It's often stated that in the early-to-mid 20th century at least, fantasy was traditionally aimed (with some noteworthy exceptions) at young, male readers. This is a demographic that is often drawn to tales of adventure, heroics, and self-sacrifice, and while war isn't necessary for stories to embrace such themes, it certainly lends itself well to them.
It's also possible to overstate the prevalence of war in fantasy, as there are a number of popular and iconic fantasy tales, many very recent, but some quite old, that have no military, battle, or war in them at all, or if they do, it's so much in the background that it has little to no impact on the plot. One thing that's really hard to do, given the thousands of trade-published titles (not to mention all the self published work that's become available more recently), is to actually tally up the books published during a given period to see if there are certain plot elements or themes which become more or less prevalent.
The diversity of the genre itself becomes problematic here. How does one define a war-focused story to begin with? Are Rowling's Harry Potter books war focused because the last book in the series had the siege of Hogwarts in it? I'd say it really isn't, but some might not agree.
A case in point, is that I never really thought of McCaffrey's Dragonriders of Pern books as militaristic, since it's unthinkable in that world to pit dragon against dragon or go to war over land when the whole planet must cooperate to survive. To me, these books always felt more like a long disaster relief effort with coming of age stuff, romance, interpersonal drama, and cloak and dagger plots, but a friend recently told me that they always made him think of one unending Battle of Britain, but with mindless spores instead of enemy planes and bombs. He has a point.