<P> The Iberian Union lasted for almost sixty years until 1640, when the Portuguese Restoration War was initiated against Castile, and Portugal reestablished the Portuguese dynasty under the Bragança . </P> <P> During the wars of the 18th century, which were often fought by the major powers to maintain the European balance of power, Spain and Portugal usually found themselves on opposite sides . The Portuguese, courtesy of their long - standing alliance, aligned themselves with Great Britain, while Spain, through the Bourbon Family Compact, allied themselves to France . In 1762, during the Seven Years' War, Castile launched an unsuccessful invasion of Portugal . </P> <P> In 1777, there was a conflict between the two states over the borders of their possessions in South America . </P> <P> In 1807, the king of Castile and his French allies invaded Portugal, using a route that crossed through Spanish territory . However, the French decided to take over both countries, overthrowing the King of Spain and forcing the Portuguese royal family to escape to the Portuguese colony of Brazil . Spain and Portugal subsequently became allies for the first time in centuries and, allied to a British army under Sir Arthur Wellesley, drove the French back across the border in 1813 after a prolonged, brutal conflict known as the Peninsular War . </P>

How did spain and portugal become separate nations