Peter Hain says the then-Prime Minister was “contemptuous” toward the desire of Gibraltar residents to remain under the British flag and told how close Britain came to losing the rocky territory to the Spanish in 2002.

Mr Hain tells in his memoirs, published on Monday, how he wanted to work to “do something about Gibraltar”, which has been a British overseas territory since 1713, as soon as he became Europe minister in June 2001.

This was based on a “gut instinct that it as ridiculous in the modern age for Britain to have a colony on the tip of Spain nearly 2,000 miles away”.

His “African roots” made it easy for him to understand the strong feelings aroused by “a little bit of England trying eccentrically to cling on to Spain”.

Mr Hain developed a plan that would see Britain and Spain share the island’s sovereignty, along with “maximum self-government for Gibraltarians”, with economic assistance from the European Union.

The plan was “enthusiastically” backed by the then-Labour Prime Minister during a flight back from a meeting with Italian premier Silvio Berlusconi in February 2002.

Mr Blair told him on the flight: “It is really important to get a better future for Gibraltar, to secure a better relationship with Spain and to remove it as an obstacle to our relations within Europe.”

Mr Hain continues that Mr Blair “was contemptuous of Gibraltarian attitudes and insistent upon making a deal which could move the whole situation forward.”

In the book, “Outside In”, Mr Hain admits that he became a “hate figure on the Rock, the target of angry posters and speeches, taking me back over 30 years to the time when I was stopping apartheid rugby and cricket tours”.

He accuses residents of Gibraltar of having a “schizophrenic” attitude to their status, wanting a better relationship and new opportunities with Spain but fiercely opposing co-sovereignty. He says: “They remained rigidly wedded to their idea of Britishness in a totally artificial sense.”

Mr Hain says that a deal was done with the Spanish Government on 18 April 2002 to allow the British to keep control of a naval base on the island, while the people of Gibraltar could hold a binding referendum.

He says: “We shook hands not quiet believing our governments had managed to come together on Gibraltar for the first time in 300 years.” However the deal was short-lived and hours after agreeing it, the Madrid government had vetoed it.

A week later Mr Hain was called into Downing Street to see the Prime Minister, who had just had a visit from angry pro-Gibraltar Labour MPs and who was, Mr Hain said, now “relieved” to have been “let off the hook” by the Spanish.

Mr Blair told him: “We are not going to be able to strike a deal at this moment because the Spanish aren’t ready for it. Trying to reopen their historic claim is not on.

“We should just park the agreement, allow things to settle down, allow opinion in Gibraltar to realise that co-sovereignty is the way we are going, and allow Spain to realise that this deal remains on the table.”

Mr Hain says that he and Jack Straw, the-then foreign secretary, “tried to argue with him but to no avail”. Mr Straw went back to Gibraltar a few weeks later where “he was almost violently attacked by a baying mob”.

This reinforced Mr Straw's view that Gibraltar should be run by Britain and Spain, and he made a Commons statement setting out that view as British policy in July 2002.

A month after Mr Hain stood down as Europe minister in October 2002, a referendum was carried out, in which 98 per cent of Gibraltarians voted to remain under British rule.

Mr Hain adds: “I don’t regret what we did or the personal flak that I took in ensuring that, at the very least, co-sovereignty will always remain part of the political architecture for Gibraltar. In time, I believe, serious thinking on the Rock will come to see it not as a threat but as a liberating opportunity.”

Since 2006, Gibraltar has governed its own affairs, although defence and foreign relations matters, are Britain's responsibility.

Last night a spokesman for Mr Blair said: "Tony Blair has never said or thought Gibraltar should be 'run by Spain'. Nor was he 'contemptuous' of it. It is correct he thought the issue should be carefully handled because of relations with Spain - an important ally - but that is all."

"Outside In" by Peter Hain is published today by Biteback Publishing