To undermine Trump.
IIRC Bismark famously noted that in every treaty there is the question who is the horse and who is the rider.
Trump reflects the views of those of us who believe we have let our allies ride us too long and too hard. It's our turn.
Mattis hasn't "vilified" "backstabbed" or "undermined" Trump, as this paragraph conveniently included in the article makes clear:
Mattis refusal to weigh in on Trump directly is one of the aspects of his military background he has maintained even after leaving office. This devoir de reserve, as Mattis put it, the duty of silence when you leave an administration, has frustrated his critics who say he should speak out.