Battery was first used in the electrical sense by the American inventor Benjamin Franklin in 1748. He choose to name his invention a battery, meaning a unit of artillery weapons, because it contained glass Leyden jars grouped in rows like cannons in an artillery unit.
The word emerged in Latin as batteure and made its way into Old French as battre and batterie, meaning 'the action of beating'. Battery appeared in English as battry, meaning 'an unlawful attack or a bruise left from a beating' from the 16th century before it was later used to refer to the bombardment of a city with artillery. The latter is what led to battery being used to refer to a unit of artillery weapons.
Sources: Oxford English Dictionary online: 1748: B. Franklin Let. in Exper. & Observ. Electr. "An electrical battery, consisting of eleven panes of large sash-glass, arm'd with thin leaden plates."