E39 M54 oil pan is a little bit different but fits in E46 anyway. What does not fit is oil level sensor connector - E46 has a key different to E39 connector. But E46 oil level sensor fits in E39 oil pan anyway, so problem solved. Head/coolant temperature sensor may have different connector, but that differs by year, not by car model. The threaded hole in the head is the same, so simply swap the sensor.
You need E46 engine harness, all 3 parts, because E46 has ECU compartment at opposite side to E39, so cable length is different.
Of course engine mounts are different.
But you can take all these things from your 2.5L engine. It's all same except the 3.0 airbox has larger opening for bigger MAF.
If you have manual gearbox, you need clutch disc with spline count matching your gearbox shaft. 2004 2.5L is likely to have 22 splines. No idea if 2002 3.0 still has 10 splines. You may want to replace clutch anyway if the old one is half way worn out.
If you have auto gearbox, you need entire different gearbox as 3.0 has more friction discs in some clutch packs and also different TC.
If you want to swap entire engine-gearbox combo, you need shorter driveshaft, different shift linkage and gearbox support/mount for manual ZF 320z gearbox. You can't use those from E39 as these are much longer.