The earliest known written version of the story dates back to 1843, when James Orchard Halliwell-Phillipps published a version in his book *Nursery Rhymes and Popular Tales*.
However, many variations of the story exist, and it's likely that versions of the tale were written down before 1843.