If it becomes obvious that another civ will attack you (units in your territory), put your tax rate at 100% and ask to trade their technologies with only money and in the 'credit mode', nothing directly . After that, ask to move their troops out of your territory. They will refuse and declare war in the same turn and trade stop during wars...