In the 19th century, as both countries became independent from Spain, Peru and Chile shared peaceful relations resulting from the formation of economic and political ties that further encouraged good relations. During the War of the Confederation (1836–1839), Chile and dissident Peruvians formed a military alliance to liberate and reunite the republics of South Peru and North Peru from the Peru-Bolivian Confederation. Later, during the Chincha Islands War (1864–1866), Peru and Chile led a united front against the Spanish fleet that occupied the Peruvian Chincha Islands and disrupted commerce in the South Pacific. In the 1870s, during the early conflicts prior to the War of the Pacific, Peru sought to negotiate a peaceful diplomatic solution between Bolivia and Chile. Although Peru had a secret defensive alliance with Bolivia, Peru did not declare war on Chile even after Chile invaded the Bolivian port of Antofagasta. War was not declared formally until Chile declared war on both Peru and Bolivia in 1879. Peru declared war on Chile the following day. The war resulted in a Chilean invasion of Peru and the destruction of various Peruvian buildings, cities, a major raid and a two-year occupation of the capital of Peru, Lima. The ultimate result of the war left a deep scar on the three societies involved, and the relations between Peru and Chile soured for over a century, although relations were stabilized to some extent by the 1929 Treaty of Lima.