War song
Li Cunxu: Li Cunxu was a Chinese king who made war songs for his troops.

Haka: It is not known when the Māori first saw fit to compose the combination song and dance known as the Haka. The haka is generally composed by a chief, or high-ranking warrior to build up the spirits of warriors and their tribe, whilst striking fear into the enemy. The most famous Haka was composed by Te Rauparaha, the chief of Ngāti Toa, a tribe based in the lower North Island. Te Rauparaha was known to Māori and early Europeans as "The Maori Napoleon," and his Haka, Ka Mate, was a dance and ode to both the living and the dead. It is still in use today, and is regularly performed by international sports teams from New Zealand, including the All Blacks rugby union team.

Early military campaigns: In England songs about military and naval subjects were a major part of the output of ballad writers from the 16th century onwards. Most of these fell into two groups, those that lamented the cost of war for the participants, and which can therefore be seen as early protest songs and those that were generally patriotic in nature and often veered into propaganda. Narrative descriptions, which had an important function in distributing news before the development of modern newspapers, have not into both camps, occasionally at the same time.
One of the earliest British ballads is "The Ballad of Chevy Chase", which exists in several versions and deals, somewhat inaccurately, with the events of the Scottish victory of the Battle of Otterburn in 1388 and may have been written in the early 14th century, but the earliest surviving version is from the mid-16th century. Stress is put on bravery, honour, revenge and the costs of war. This last factor is even more evident in an early 17th-century version that notes that 'the next day did many widows come/Their husbands to bewail.'
The conflicts between England and Spain in the later 16th and early 17th centuries produced a number of ballads describing events, particularly naval conflicts like those of the Spanish Armada. The English Civil War (1642–1653) produced a subgenre of "Cavalier ballads", including "When the King Home in Peace Again", while their parliamentarian opponents were generally happier singing metrical psalms. Many of these were adapted and reused by Jacobites in England and Scotland after the 'Glorious Revolution' of 1688, a tradition built on by Robert Burns and Sir Walter Scott.
The Anglo-French Wars of the 17th and 18th centuries saw more descriptive works, usually couched in patriotic terms, but some, like 'Captain Death' (1757) dealt with loss and defeat. As regimental identities emerged songs were adopted for marching, like 'The British Grenadiers', based on a dance tune and with enthusiastic lyrics from at least the mid-18th century. Both sides make extensive use of ballads as propaganda in the American Revolutionary War (1775–83), but they became a flood during the French Revolutionary and Napoleonic Wars (1797–1815). The same period saw numerous patriotic war songs, like 'Heart of Oak' and the emergence of a stereotype of the English seaman as 'Jolly Jack Tar', who appeared in many ballads.
The American Civil War saw huge numbers of ballads produced as recruitment propaganda and morale boosting songs on both sides, including 'We are coming father Abraham', rapidly written in response to Abraham Lincoln's call to arms in 1862. Most successful on the Union side was 'The Battle Hymn of the Republic', written by Julia Ward Howe in 1862, using the existing tune that had already been used as a hymn and soldier's song, with its rousing chorus of 'Glory, glory hallelujah'. Some songs like 'Weeping Sad and Lonely, or When This Cruel War is Over' (1863), were sung on both sides, much to the consternation of the commanders.  As for the Confederacy, their most successful song was "I Wish I Was in Dixie".  During South Carolina's secession convention, the song "I Wish I Was in Dixie" was played each time a delegate voted to secede.  It was also played at the inauguration ceremony of President Jefferson Davis in Montgomery, Alabama.  Another extremely popular song of the Confederacy was "The Bonnie Blue Flag".  While these songs are popular Confederate recruiting songs and popular marching songs, some songs are written for soldiers to sing while marching, to help boost morale.  One great example is "Richmond is a Hard Road to Travel", making fun of the Union failures to take Richmond from the Battle of First Manassas to the Battle of Fredericksburg.
The name had probably been around in the 18th century, but it would not be until the late 19th century that British land forces received an equivalent to Jack Tar in 'Tommy Atkins', in Rudyard Kipling's poems and in many music hall songs. The Boer War saw a large number of songs, often aimed at praising the bravery of particular groups (such as Irish troops) or soldiers in general. From this period we know that some songs were widely sung by the troops themselves, including particularly leave taking songs, of which probably the most famous is 'Goodbye, Dolly Grey'.

World War I songs: Leading up to 1914, and throughout the war there were many patriotic or jingoistic songs, but it is notable that soldiers themselves tended to prefer songs that were resigned in tone, like 'Pack Up Your Troubles in Your Old Kit-Bag' (1915), or that reminded them of home, rather than how to fight. 'Goodbye, Dolly Grey' was still popular along with songs that were adopted by soldiers like 'It's a Long Way to Tipperary'. "The Conscientious Objector's Lament" (1917) was intended to ridicule pacifists, but it ended up being sung by soldiers longing to go home (See "When This Bloody War is Over" by Max Arthur, page 42). "Good-bye-ee!" (1917) pokes fun at the well educated soldiers who cannot stop themselves from using public school language such as "chin-chin!". "Oh It's a Lovely War" (1917) was the inspiration for the film "Oh! What a Lovely War". The singer is overly enthusiastic about mud and soldiers' food rations. It is an anti-war song disguised as a recruiting song. "Hanging on the Old Barbed Wire" concerns the search for a Sergeant (lying on the floor), a quartermaster (behind the line) and the privates (hanging on the old barbed wire). Officers tried to prevent privates from singing the last verse, but were usually unsuccessful.
Other examples are:

"Keep the Home Fires Burning" (1914)
"Mademoiselle from Armentières"
"The Hearse Song"
"Over There"  (later featured in the film This Is the Army)
"Roses of Picardy"

World War II songs: Lili Marleen became one of the most popular songs of the Second World War among both German and British troops. Originally sung in German by Lale Andersen, it was sung in many English versions, notably by Marlene Dietrich.
Irving Berlin wrote "This is the Army, Mr. Jones" (1942) for the revue This is the Army that was remade as a 1943 American wartime musical comedy film of the same name. It mocks the attitudes of middle class soldiers, forced to undergo the rigors of life in the barracks.
"Kiss Me Goodnight, Sergeant Major" is a British soldier's song, mocking their officers.

Songs of the Spanish Civil War: 

List of songs about the Vietnam War: 

Anti-war song: An anti-war song is a musical composition that either states anti-war sentiments directly, or one which is perceived (by the public and/or critics) as having an anti-war theme.  Some show the negative aspects of war, while others satirize war.  Most promote peace, in some form or another, while others speak out against certain specific armed conflicts.  Many of these songs are considered protest songs, and some have been embraced by various peace movements.

See also: Battle cry
Military music
Music and political warfare

Notes: 

Further reading: Tunebooks

Oh! It's a Lovely War: Songs Ballads and Parodies of the Great War, by EMI Music Publishing (1978)
Mud, Songs and Blighty: A Scrapbook of the First World War by Colin Walsh (1975)
Textbooks

When This Bloody War is Over: Soldiers' Songs of the First World War by Max Arthur (1943)
Dark Laughter: War in Song and Popular Culture by Les Cleveland (1994)
Goodnight Sweetheart: Songs and Memories of the Second World War by Frank E Huggett (1810)
Academic paper "Propaganda and dissent in British popular song in the Great War"