RABAT (Reuters) - A Moroccan court sentenced a former Spanish soldier to eight years in jail on Friday on charges of leading an Islamist network in the kingdom that was plotting terror attacks, the state news agency MAP said.

Seventeen other members of the group, all believed to be Moroccans, were sentenced to between two and 10 years in prison on a variety of charges.

The Spaniard was only identified by his initials -- J.F.

He was arrested in January 2014 and the Interior Ministry said at the time that he had served in Melilla, a tiny Spanish enclave on the Morocco’s Mediterranean coast. He resigned his commission and joined al Qaeda in Afghanistan where he learned guerrilla tactics, it said.

The authorities accused him of forming a criminal network with Moroccans drawn from numerous cities including Fes, Marrakesh and Nador, where he himself was based, near Melilla.

No further details emerged from the trial and it was not clear if his organization was affiliated to any particular Islamist group.

Morocco, a Western ally against Islamist militancy, regularly reports that has broken up radical cells accused of plotting attacks. On Thursday, the Interior Ministry said it had arrested three men seeking to join groups loyal to the Islamic State in Libya.

The North African kingdom has suffered sporadic bomb attacks by suspected Islamist groups, most recently in 2011 in Marrakesh, but militant movements have so far failed to gain any foothold in the country.