During the French invasion of Portugal in 1807, Napoleon made the Mafra Palace headquarters and garrison. Part of the army headed for Peniche and Torres Vedras under orders from General Luison, the remaining forces garrisoned in the Palace and Convent, while the executive staff requisitioned homes in the village. This lasted nine months, until 2 September 1807, when British forces were able to extricate the French from their positions. Meanwhile, Portuguese engineers, allied to British forces constructed a system of fortresses north of Lisbon, to secure the defence of the capital and expel the French. The "Lines of Torres Vedras", as they were known, passed through the municipality of Mafra, and were constructed in 1809 and 1810. Of the 156 fortresses, 48 were located within Mafra municipality, and represented the second line of defence including sites in Malveira, Gradil, Ribamar, Carvoeira, Mafra and Ericeira. In the main only vestiges of these forts and redoubts remain, but those that are still in good condition or have been restored include the forts of Feira, Santa Susana, and Zambujal. Within the Tapada there were forts in Sunível, Milhariça, Juncal and Silvério, while a group of forts followed the left bank of the Ribeira de Safarujo to Ribamar. In the south, the last line of defence, should the French breach the defences, were the forts of Carvoeira, São Julião and Zambujal.