How to replay events (in the event sourcing handler) of a different aggregate

I have an aggregate (call it B) whose initial state is a copy of the state of another aggregate (call it A) at the moment of B’s creation. Now to build the initial state of aggregate B (using event sourcing), I need all the events that have been applied on the aggregate A (till the time of B’s initialisation) to be applied on B as well.

How can I replay events of aggregate A on aggregate B to build its state? Does Axon provide any out-of-the-box solution? Or is there any workaround to this problem?

You could put all relevant fields into a new BInitializedFromAEvent and send this event after you’ve sent BCreatedEvent
inside a the command handler. The new event only contains state, not the events that lead to A‘s current state.
You can inject the AggregateRepository to get the current state.

This might lead to a lot of fields and might get a point it gets harder to change.
If B should always look like A, then you could play around with multi entity aggregates.