LONDON — Three British men convicted of planning a knife and bomb attack on troops or police inspired by Islamic extremism were sentenced Thursday to at least 20 years in prison.

An accomplice received a minimum 15-year-term.

Naweed Ali, Khobaib Hussain, Mohibur Rahman and Tahir Aziz were convicted in a London court on Wednesday of preparing terrorist acts after a trial that was partly held in secret for national security reasons.

Ali, Hussain and Rahman met while serving prison terms for terrorism offenses, and later set up a group called the “Three Musketeers” on a messaging app.

The men were arrested in August 2016 after weapons were found in Ali’s car, including a partial pipe bomb and a meat cleaver with “kaffir” — infidel in Arabic — on the blade. Prosecutors say they intended to attack police or military targets.

Prosecutor Bill Emlyn Jones said the defendants probably intended to use their cars as weapons in an attack, as well as knives and the pipe bomb.

Judge Henry Globe sentenced Ali, Hussain and Rahman to life with no chance of parole for 20 years. He said Aziz, a late recruit to the plot, must serve at least 15 years before being considered for parole.

The judge noted that Britain had experienced four deadly attacks during the four-and-a-half month trial. He said that had the “musketeers” gang not been caught, “there would have been not dissimilar terrorist acts in this country using at the very least the explosives and or one or more bladed weapons.”

The defendants, from central England, denied the charges and accused police of planting evidence. Defense lawyers also criticized the decision to hear from two witnesses in secret as they discussed claims by the defendants that Britain’s domestic spy agency had tried to recruit them.

Gareth Peirce, lawyer for Ali and Hussain, released a statement after sentencing expressing “profound concern that the jury in this case has got it wrong.”