A New York judge is preparing to bring down his gavel on the epic battle between Martha Stewart, Macy’s and JCPenney — and the head that gets hammered could be Martha’s.

State Supreme Court Justice Jeffrey Oing, who gave the trio until this Friday to settle their contract disputes after nearly two years of legal bickering, is ready to rule this week, sources told The Post.

Oing has appeared reluctant to make the long-awaited decision, hoping that the parties could settle their differences out of court, insiders said.

The legal tiff was sparked in December 2011, when ex-Penney CEO Ron Johnson cut a 10-year, $200 million home-goods licensing deal with Stewart, despite the fact the domestic diva already had an exclusive licensing deal with Macy’s.

After Macy’s sued last year, Oing temporarily barred Penney from labeling most of its Stewart-designed goods with Stewart’s name. Thus, Penney has been forced to give those goods the generic “JCP Everyday” moniker.

Some legal experts expect the ban on most Stewart-branded goods at Penney stores will be made permanent if the judge is forced to rule, handing a victory to Macy’s.

In turn, insiders say the real conflict now lies between Penney and Stewart.

As reported by The Post, Penney CEO Mike Ullman, who this spring replaced the ousted Johnson, has moved to ditch the Stewart line, citing disappointing sales and lackluster designs, despite the fact the judge hasn’t ruled.

Stewart, meanwhile, is clinging to the contract and its guaranteed royalties on designs of bedding and bath products, kitchen ware and party supplies.

Penney has said it awaits the judge’s decision, while Martha Stewart Living Omnimedia has insisted that the contract remains in force.